使用 C# 连接/重试连接的最有效方法?

发布于 2024-08-15 16:25:12 字数 1833 浏览 4 评论 0原文

我正在创建一个小游戏客户端,它最终将连接到服务器以收集有关可玩游戏的一些信息、正在玩的玩家数量以及您可以想象它应该做的所有其他内容。

我的困难在于找到一种有效的方法来处理首次加载时的连接/重试连接序列。

我想象我的客户端在尝试连接时会遵循以下过程:

  1. 执行客户端应用程序
  2. 尝试建立连接
  3. 如果连接成功则收集信息 - 如果不成功则继续执行步骤 4
  4. 显示一个新的对话框/表单,提示用户正在尝试建立连接建立
  5. 循环直到建立连接

我一直在质疑我尝试遵循此顺序的方法。我质疑这是否是正确/最有效的连接方式以及为什么我在步骤 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:

  1. Client application executed
  2. Try to establish connection
  3. If connection successful gather information - If not successful proceed to step 4
  4. Display a new dialog/form that prompts the user that a connection is trying to be established
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心病无药医 2024-08-22 16:25:12

下面只是一个示例,我将在其中处理 catch 中的任何继续逻辑,并在 while 循环内进行中断或继续。

安德鲁

while (!connected)
{
    try
    {
        sock.Connect(authenServerEP);
        connected = true;
        establishingConnectionForm.Dispose();
    }
    catch (SocketException retryConnectException)
    {
        //Optional - add some wait time may be 5 seconds i.e. "trying again in 5 seconds"
        //System.Threading.Thread.Sleep(5000);
        //Here check the number of attempts and if exceeded:
        if(numberOfTimes == 5)
        {
            break;
        }
        else
        {
            numberOfTimes++; 
            continue;
        }
    }
}

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

while (!connected)
{
    try
    {
        sock.Connect(authenServerEP);
        connected = true;
        establishingConnectionForm.Dispose();
    }
    catch (SocketException retryConnectException)
    {
        //Optional - add some wait time may be 5 seconds i.e. "trying again in 5 seconds"
        //System.Threading.Thread.Sleep(5000);
        //Here check the number of attempts and if exceeded:
        if(numberOfTimes == 5)
        {
            break;
        }
        else
        {
            numberOfTimes++; 
            continue;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文