为了账号安全,请及时绑定邮箱和手机立即绑定

TcpListener 客户端保持连接发送多条消息,但服务器只接收或处理第一条消息

TcpListener 客户端保持连接发送多条消息,但服务器只接收或处理第一条消息

C#
撒科打诨 2022-07-23 18:03:42
我正在设置一个服务器来使用 TcpListener 读取一些网络客户端。客户端发送一些数据,我验证该数据并发送对该数据的响应,客户端保持连接并发送第二个响应,然后我验证该数据并发送回响应,就像登录服务器两次一样。第一次登录被发送回客户端就好了,但是客户端第二次响应服务器并没有显示它从客户端收到了更多的数据。我已经通过设置一个虚拟客户端(真正的客户端是基于手机的 ODB2)对其进行了测试。设置了虚拟客户端后,我确实验证了第一次握手发生了,但是当客户端发送第二组文本时,它没有显示在服务器上。class Program{    static private TcpListener listener = null;    static private TcpClient client = null;    static private NetworkStream stream = null;    static private int iCount = 0;    static Int32 port = 8090;    static IPAddress localAddr = IPAddress.Parse("192.168.1.17");    static void Main(string[] args)    {        listener = new TcpListener(localAddr, port);        listener.Start();        while (true)        {            try            {                client = listener.AcceptTcpClient();                ThreadPool.QueueUserWorkItem(ThreadProc, client);            }            catch (IOException ioex)            {                RestartStream();            }        }    }        private static void ThreadProc(object obj)        {        var client = (TcpClient)obj;        Byte[] bytes = new Byte[client.ReceiveBufferSize];        stream = client.GetStream();        try        {            int bytesRead = stream.Read(bytes, 0, (int)client.ReceiveBufferSize);            string returndata = Encoding.ASCII.GetString(bytes, 0, bytesRead).Replace("-", "");            byte[] sendBytes;            if (returndata.ToLower().StartsWith("7e") && returndata.ToLower().EndsWith("7e"))            {             //… do stuff with the data and send it back to the client              sendBytes = Encoding.Default.GetBytes(login1);             stream.Write(sendBytes, 0, sendBytes.Length);             stream.Flush();                              }                              else                {                    SaveStream(returndata);                }            }我需要发生的是我的理解是客户端始终保持连接并一遍又一遍地发送数据,我的系统似乎接受它一次然后停止接收它,我需要它继续接收客户端数据并处理它。
查看完整描述

1 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

好的,所以我想通了,线程中应该有第二个 while 循环。


static void Main(string[] args)

    {

        listener = new TcpListener(localAddr, port);

        var clientSocket = default(TcpClient);

        listener.Start();

        var counter = 0;

        while (true)

        {

            clientSocket = listener.AcceptTcpClient();

            var client = new ConnectedDevice();

            client.startClient(clientSocket, counter.ToString(), sqlConnString);

        }

    }

连接设备类:


class ConnectedDevice

{

    private TcpClient _clientSocket;

    private string _clientNumber;

    private string _sqlConnString;


    public void startClient(TcpClient clientSocket, string clientNumber, string sqlConnString)

    {

        _clientSocket = clientSocket;

        _clientNumber = clientNumber;

        _sqlConnString = sqlConnString;


        var ctThread = new Thread(ProcessClient);

        ctThread.Start();

    }

    private void ProcessClient()

    {

        while (_clientSocket.Connected)

        {

            try

            {

                Byte[] bytes = new Byte[_clientSocket.ReceiveBufferSize];

                var networkStream = _clientSocket.GetStream();

                networkStream.ReadTimeout = 10000;

                int i;

                while ((i = networkStream.Read(bytes, 0, bytes.Length)) != 0)

                {

                    var data = System.Text.Encoding.ASCII.GetString(bytes, 0, i).Replace("-", "");

                    byte[] sendBytes;

                    Console.WriteLine(data);

                    string sLogin1 = "7E81000013014185000008000000000054523230313731303138303930303137497E";

                    sendBytes = Encoding.ASCII.GetBytes(sLogin1);

                    networkStream.Write(sendBytes, 0, sendBytes.Length);

                    networkStream.Flush();

                }

            }

            catch (Exception ex)

            {


            }

        }

    }


查看完整回答
反对 回复 2022-07-23
  • 1 回答
  • 0 关注
  • 421 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号