使用 C# 连接/重试连接的最有效方法?
我正在创建一个小游戏客户端,它最终将连接到服务器以收集有关可玩游戏的一些信息、正在玩的玩家数量以及您可以想象它应该做的所有其他内容。
我的困难在于找到一种有效的方法来处理首次加载时的连接/重试连接序列。
我想象我的客户端在尝试连接时会遵循以下过程:
- 执行客户端应用程序
- 尝试建立连接
- 如果连接成功则收集信息 - 如果不成功则继续执行步骤 4
- 显示一个新的对话框/表单,提示用户正在尝试建立连接建立
- 循环直到建立连接
我一直在质疑我尝试遵循此顺序的方法。我质疑这是否是正确/最有效的连接方式以及为什么我在步骤 4 中显示的表单不起作用?
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)
正如您所看到的,我捕获了当连接到服务器出现问题(例如服务器未运行)时引发的 SocketException。然后我继续尝试连续循环,直到建立连接。我不知道我是否应该这样做。有更好的方法来做到这一点吗?
此外,当我使用 Show() 显示建立ConnectionForm 时,它看起来并不像所有表单/工具都初始化(初始化可能会产生误导)。表单上的标签只是以白色阴影显示,而不是显示其文本。不仅如此,我似乎无法选择表单/对话框并实际移动它。它与“思考/工作”鼠标图标一起坐在那里。现在我认为这是因为我正在循环尝试重新连接并因此而阻塞(我在阻塞方面可能是错误的?)。这个问题可以用多线程来解决吗?如果是这样我需要多线程吗?有没有一种更简单的方法来显示我的表单/对话框并能够在我仍然尝试重新连接的同时与其进行交互(IE 将其拍摄并用右上角的“X”将其关闭)?
非常感谢。我真的很感谢您阅读这篇文章,并对这个社区表示感谢。 :D
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面只是一个示例,我将在其中处理 catch 中的任何继续逻辑,并在 while 循环内进行中断或继续。
安德鲁
Just an example below where I would handle any continuation logic in the catch and either break out or continue inside the while loop.
Andrew