簡體   English   中英

無法從C#TCP服務器接收數據

[英]cannot recieve data from c# tcp server

我已經在C#中編寫了一個簡單的Tcp服務器:(當它與服務器無關時,我已經替換了“做一些事情”中的一些代碼部分。現在,當我嘗試與服務器聯系時我從python客戶端或android客戶端收到諸如“另一方主動拒絕連接”之類的錯誤,我該怎么辦?我的C#代碼中有問題嗎,或者我可能沒有正確聯系它?您。

public bool ListenLoop(Int32 port, IPAddress localAddr)
        {
            try
            {
              server = new TcpListener(localAddr, port);

              // Start listening for client requests.
              server.Start();
              // Buffer for reading data
              Byte[] bytes = new Byte[256];
              String data = null;

              // Enter the listening loop. 
              while(true) 
              {
                //Waiting for a connection

                // Perform a blocking call to accept requests. 
                TcpClient client = server.AcceptTcpClient();            
                //connected!


                // Get a stream object for reading and writing
                NetworkStream stream = client.GetStream();
                int i;

                // Loop to receive all the data sent by the client. 
                while((i = stream.Read(bytes, 0, bytes.Length))!=0) 
                {   
                  // Translate data bytes to a ASCII string. 
                  data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); 
                  //handling opCodes
                  if(data[0] == '0') //log in
                  {
                      //do some stuff
                      byte[] msg = System.Text.Encoding.ASCII.GetBytes(response);
                      // Send back a response.
                      stream.Write(msg, 0, msg.Length);
                      //sent    
                  }
                  else if (data[0] == '1') //download tune names
                  {
                      //do some stuff 
                      byte[] msg = System.Text.Encoding.ASCII.GetBytes(response); //response is the names
                      // Send back a response.
                      stream.Write(msg, 0, msg.Length);
                      //sent  
                  }
                  else if (data[0] == '2') //changing choice
                  {
                      //do some stuff
                      byte[] msg = System.Text.Encoding.ASCII.GetBytes(response);
                      // Send back a response.
                      stream.Write(msg, 0, msg.Length);
                      //sent   

                  }

                }

                // Shutdown and end connection
                client.Close();
              }
            }
            catch(SocketException)
            {
                return false;
            }

            finally
            {
               // Stop listening for new clients.
               server.Stop();     
            }
         }

您確定沒有防火牆阻止傳入連接(因為它是TCP)?

另外,如果您的服務器正在測試中,則應將一些輸出寫入日志文件,甚至只是寫入控制台。 至少你知道發生了什么。

服務器=新的TcpListener(localAddr,端口); 更改為server = new TcpListener(port);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM