亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 439 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號