I am creating a little game client that will end up connecting to a server to gather some information on available games to play, how many players are playing, and all kinds of other stuff that you can imagine it ought to do.
My difficulties come in finding an effective way in dealing with the connect/retry connect sequence upon first loading.
I imagined my client would follow this process in trying to connect:
- Client application executed
- Try to establish connection
- If connection successful gather information - If not successful proceed to step 4
- Display a new dialog/form that prompts the user that a connection is trying to be established
- Loop till a connection has been established
I have been questioning my methods in trying to follow this sequence. I question if it is the right/most effective way to connect as well as to why my form I display in step 4 isn't working?
try
{
sock.Connect(authenServerEP);
// Once connected show our main client window
this.Show();
// Create the LoginForm once a connection has been established and display
LoginForm loginForm = new LoginForm();
loginForm.ShowDialog();
if (false == loginForm.Visible)
{
loginForm.Dispose();
}
}
catch (SocketException firstConnectException)
{
// Load retrying connection form
EstablishingConnectionForm establishingConnectionForm = new EstablishingConnectionForm();
establishingConnectionForm.Show();
bool connected = false;
// Loop until we are connected
while (!connected)
{
try
{
sock.Connect(authenServerEP);
connected = true;
establishingConnectionForm.Dispose();
}
catch (SocketException retryConnectException)
{
// Pass and retry connection
}
}
} // end catch (SocketException firstConnectException)
As you can see I am catching the SocketException that is raised when there is a problem connecting to the server (such as the server isn't running). I then go on to try to continuously loop till a connection is established. I dunno if I ought to be doing it this way. Are there better ways to do this?
Also when I display establishingConnectionForm with Show() it doesn't look like it all of the forms/tools initialize (initialize could be misleading). The Label that is on the form is just shaded out in white as opposed to having its text displayed. Not only that but it seems like I can't select the form/dialog and actually move it around. It sits there with the "Thinking/Working" mouse icon. Now I presume this is because I am looping trying to reconnect and its blocking because of this (I could be wrong on the blocking?). Can this problem be solved with multithreading? If so do I need to multithread? Is there an easier way to show my form/dialog and be able to interact (IE movie it around and close it with the 'X' in the upper right corner) with it while I still try to reconnect?
Thanks a bunch. I really appreciate you reading this post and am grateful for this community. :D