【问题标题】:cannot recieve data from c# tcp server无法从 c# tcp 服务器接收数据
【发布时间】:2015-05-18 10:39:21
【问题描述】:

我用 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();     
            }
         }

【问题讨论】:

  • 这是一个连接问题。检查服务器是否正在运行并检查您的防火墙以确保相关端口已打开。还要检查您是否尝试连接到正确的 ip/端口,因为可能在同一台机器上运行的其他东西不理解您的传入请求。
  • 如果您正在监听localhost 地址,那么来自其他主机的连接将被拒绝。您是在同一台机器连接吗?
  • 嗨,我确实是从同一台机器连接的。奇怪的是,我将我的代码复制到控制台应用程序,服务器工作得很好,但是当我在 wpf 应用程序中运行它时,它拒绝连接

标签: c# android python tcp


【解决方案1】:

您确定没有阻止传入连接的防火墙(因为它是 TCP)?

此外,如果您的服务器正在测试中,您应该将一些输出写入日志文件,甚至只是写入控制台。至少你知道发生了什么。

【讨论】:

    【解决方案2】:

    server = new TcpListener(localAddr, port); 改成 server = new TcpListener(port);

    【讨论】:

    • 请解释你的答案。
    猜你喜欢
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多