Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 套接字无法从服务器接收消息_C#_C# 4.0_Websocket_C# 3.0 - Fatal编程技术网

C# 套接字无法从服务器接收消息

C# 套接字无法从服务器接收消息,c#,c#-4.0,websocket,c#-3.0,C#,C# 4.0,Websocket,C# 3.0,我正在尝试使用套接字连接到应用程序 应用程序使用端口6100进行通信。 我能够向应用程序发送消息,但无法接收任何消息 这是我的代码。如果我做错了什么,请告诉我 public void Connect2(string host, int port, string cmd) { byte[] bytes = new byte[10024]; IPAddress[] IPs = Dns.GetHostAddresses(host); Socket s = new Socket

我正在尝试使用套接字连接到应用程序

应用程序使用端口6100进行通信。 我能够向应用程序发送消息,但无法接收任何消息

这是我的代码。如果我做错了什么,请告诉我

public void Connect2(string host, int port, string cmd)
{
    byte[] bytes = new byte[10024];
    IPAddress[] IPs = Dns.GetHostAddresses(host);

    Socket s = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream,
    ProtocolType.Tcp);
    s.SendTimeout = 100;
Console.WriteLine("Establishing Connection to {0}",
    host);
    try
    {
        s.Connect(IPs[0], port);
    }

    catch (Exception e)
    {

        MessageBox.Show(e.Message);
    }



    byte[] sendBites = System.Text.Encoding.ASCII.GetBytes(cmd);
    int bytesSent = s.Send(sendBites);
    int bytesRec = s.Receive(bytes);
    s.ReceiveFrom(bytes, ref tmpRemote);
    MessageBox.Show(s.ReceiveBufferSize.ToString());
    MessageBox.Show(Encoding.ASCII.GetString(bytes, 0, bytesRec));
    s.Shutdown(SocketShutdown.Both);
    s.Close();

}

首先,当连接失败时,您仍在尝试发送/接收数据。将发送和接收放在try/catch中


我可以看到您呼叫receive两次:

int bytesRec = s.Receive(bytes);
s.ReceiveFrom(bytes, ref tmpRemote);
可能发生的情况是:

  • 您正在发送命令
  • 您正在等待响应“int bytesRec=s.Receive(字节);
  • 然后,您再次等待更多的数据。。s.ReceiveFrom(字节,参考tmpRemote);
但服务器可能不会发送额外的数据,因此您将“永远”等待。

再次尝试删除
s.ReceiveFrom(字节,参考tmpRemote)



Socket.ReceiveFrom()
通常用于接收UDP广播。

这段代码工作正常

     public string SocketSendReceive(string server, int port, string cmd)
    {
        byte[] recvBuffer = new byte[1024];
        string host = "127.0.0.1";
        TcpClient tcpClient = new TcpClient();
        try
        {
            tcpClient.Connect(host, 6100);
        }
        catch (SocketException /*e*/)
        {
            tcpClient = null;
        }


        if (tcpClient != null && tcpClient.Connected)
        {

            tcpClient.Client.Send(Encoding.UTF8.GetBytes(cmd));
            tcpClient.Client.Receive(recvBuffer);
            //    port = Convert.ToInt16(Encoding.UTF8.GetString(recvBuffer).Substring(2));
            tcpClient.Close();

        }

        return Encoding.ASCII.GetString(recvBuffer, 0, recvBuffer.Length);
    }

我已经更改了代码,但我的应用程序仍然挂起,并且没有收到任何错误消息。请尝试{s.Connect(IPs[0],端口);byte[]sendBites=System.Text.Encoding.ASCII.GetBytes(cmd);int bytesSent=s.Send(sendBites);int bytesRec=0;do{bytesRec=s.Receive(bytes,bytes.Length,0);}while(bytesRec>0);}catch(异常e){MessageBox.Show(e.Message);}此代码已损坏。它无法检查来自
Receive
的返回值,该返回值告诉您缓冲区中已放置了多少字节。TCP是字节流。如果您想要“消息传递”,则由您自己在TCP之上实现消息帧,或者移动到已提供消息传递的更高级别抽象。