C# 客户端/服务器线程连接

本文关键字:线程 连接 服务器 客户端 | 更新日期: 2023-09-27 18:33:52

我在这部分代码中遇到了麻烦。实际上,我想设置一个客户端/服务器应用程序。在客户端部分,我启动了一个线程,该功能只是每次检查它是否连接到服务器(如果仍然建立与服务器的连接(TraceLog 是一个使用其 Info(( 方法写入文件的类。这是客户端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace ClientApp
{
    class ClientOpenConnection
    {
        private static Thread threadConnect;
        static TcpClient myClient = new TcpClient();
        static String host = "";
        static Int32 port = 0;
        //Function that makes the client runs
        public static void RunClient(String hostname, Int32 hostport)
        {
            host = hostname;
            port = hostport;
            int _tryAgain = 0;
            while (!myClient.Connected) {
                try
                {   //I start the connection
                    myClient.Connect(host, port);
                }
                catch {
                }
                _tryAgain += 10;
                if (_tryAgain == 1000)
                    break;
                 //_tryAgain allows me to define how long will the client try to connect to the server.
            }
            TraceLog.Info("Out of the while ", ""); // This is to know where am I
            if (_tryAgain != 1000)
            {    //If I get out because _tryAgain is less than 1000. It means that I am already connected to the server
                //Here I start a Thread to be sure that I am always connected to the server
                threadConnect = new Thread(isConnected);
                threadConnect.Start();
                TraceLog.Info("Launch the thread","");
            }
            //While threadConnect is executing parallely I continue my program
        }
        private static void isConnected() { 
            //I keep my eyes on the network connection
            while (myClient.Connected) { 
                //Nothing is done
            }
            TraceLog.Info("The connection has been lost","");
            RunClient(host,port);
        }
    }
}

遇到的问题,当我在服务器之前启动客户端时,我进入第一个 WHILE 循环。 在这个级别上是可以的。当我启动服务器之后,我启动了threadConnect,但问题是,如果现在我停止服务器,通常我应该在日志文件中显示"连接已丢失",但我什么都没有。这部分代码有什么问题?你过去做过这样的事情吗?

进行了修改,但仍然无法获得我想要的东西,即即使服务器停止,客户端仍然会尝试联系服务器。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace ClientApp
{
    class ClientOpenConnection
    {
        private static Thread threadConnect;
        static TcpClient myClient = new TcpClient();
        static String host = "";
        static Int32 port = 0;
        //Function that makes the client runs
        public static void RunClient(String hostname, Int32 hostport)
        {
            host = hostname;
            port = hostport;
            TraceLog.Info(" -> "+myClient.Connected,"");
            while (!myClient.Connected) {
                try
                {
                    myClient.Connect(host, port);
                    TraceLog.Info(" <-> " + myClient.Connected, "");
                }
                catch {
                    TraceLog.Info("Trying to contact the server","");
                }
            }
            TraceLog.Info("I am connected ", "");
            //Here I start a Thread to be sure that I am always connected to the server
            threadConnect = new Thread(isConnected);
            threadConnect.Start();
            TraceLog.Info("Launch the thread to be sure I am constantly online","");
        }
        private static void isConnected() { 
            //I keep my eyes on the network connection
            TraceLog.Info("->>"+myClient.Connected,"");
            while (myClient.Connected) {
                Thread.Sleep(500);
                try
                {
                    NetworkStream stream = myClient.GetStream();
                    ASCIIEncoding ascii = new ASCIIEncoding();
                    byte[] _incomingMsg = new byte[1024];
                    stream.Read(_incomingMsg, 0, _incomingMsg.Length);
                    String strToGet = System.Text.Encoding.ASCII.GetString(_incomingMsg);
                    strToGet = strToGet.Trim();
                    if (!strToGet.Equals("ONLINE"))
                        if (strToGet.Equals(""))
                        {
                            TraceLog.Info("The message receive is empty","");
                            break;
                        }
                }
                catch {
                    break;
                }
            }          
            TraceLog.Info("The connection has been lost", "");
            RunClient(host, port);
        }
    }
}

但是当我在isConnected((函数中调用RunClient((时,它会在WHILE中执行并输出 TraceLog.Info("试图联系服务器","(;即使我再次启动服务器,客户端也会保持在while循环中,根本不连接。

C# 客户端/服务器线程连接

来自 MSDN:

属性获取客户端套接字的连接状态 截至上次 I/O 操作。当它返回 false 时,客户端套接字 从未连接过,或者不再连接。

因为 已连接 属性仅反映 连接从最近的操作开始,您应该尝试发送 或接收消息以确定当前状态。消息后 发送失败,此属性不再返回 true。请注意,这 行为是设计使然。您无法可靠地测试 连接,因为在测试和发送/接收之间的时间内, 连接可能已丢失。您的代码应假定 套接字已连接,并正常处理失败的传输。

换句话说,为了检查您是否仍然连接,您需要发送或接收一些数据,然后检查连接状态。

由于代码在建立连接后不会发送任何数据包,因此 connected 属性始终返回 true,并且循环永远不会退出。