Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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# udp客户端绑定_C#_Sockets_Udp_Client_Bind - Fatal编程技术网

C# udp客户端绑定

C# udp客户端绑定,c#,sockets,udp,client,bind,C#,Sockets,Udp,Client,Bind,我有UDP客户端聊天,它向服务器发送消息并返回响应。我有两个线程,一个用于发送,一个用于接收消息。我得到ReceiveFrom()方法的异常:“在执行此操作之前必须调用Bind方法”。但这是一个客户,我不想绑定任何东西。例如,此客户端工作正常: byte[] data = new byte[30]; string input, stringData; IPEndPoint servIPEP = new IPEndPoint( IPAddress.P

我有UDP客户端聊天,它向服务器发送消息并返回响应。我有两个线程,一个用于发送,一个用于接收消息。我得到ReceiveFrom()方法的异常:“在执行此操作之前必须调用Bind方法”。但这是一个客户,我不想绑定任何东西。例如,此客户端工作正常:

    byte[] data = new byte[30];
    string input, stringData;
    IPEndPoint servIPEP = new IPEndPoint(
            IPAddress.Parse("127.0.0.1"), 9050);
    EndPoint servEP = (EndPoint)servIPEP;
    Socket client = new Socket(AddressFamily.InterNetwork,
            SocketType.Dgram, ProtocolType.Udp);
    string welcome = "Hello, are you there?";
    data = Encoding.ASCII.GetBytes(welcome);
    client.SendTo(data, data.Length, SocketFlags.None, servEP);
    data = new byte[30];
    int recv = client.ReceiveFrom(data, ref servEP);    //works fine!
接受是没有约束的。但当我创建两个线程时,会抛出错误:

 public ChatClient()
        {
            clientSock = new Socket(AddressFamily.InterNetwork, 
                               SocketType.Dgram, ProtocolType.Udp);
            servIPEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 32000);
            servEP = (EndPoint)servIPEP;
        }

         public void ReceiveThread()
        {
            Thread receiveThread = new Thread(new ThreadStart(ReceiveData));
            receiveThread.Start();
        }

        public void ReceiveData()
        {
           while(true){
                clientSock.ReceiveFrom(buf, ref servEP); //Here I get ERROR
                string msg = Encoding.ASCII.GetString(buf).Trim();
                Console.WriteLine("New message: {0}",msg);
                }

        }

        public void SendThread()
        { 
            Thread sendThread = new Thread(new ThreadStart(SendData));
            sendThread.Start();
        }

        public void SendData()
        {
            while (true)
            {
                Console.WriteLine("Enter message to send: ");
                string msg = Console.ReadLine();
                buf = Encoding.UTF8.GetBytes(msg);
                clientSock.SendTo(buf, servEP);
            }
        }
    }


    static void Main(string[] args)
    {

        ChatClient client = new ChatClient();
        client.SendThread();
        client.ReceiveThread();
    }
}
谢谢您的时间。

显然SendTo()执行隐式绑定,ReceiveFrom()不执行


在没有预先绑定的情况下开始接收(至少设置您通过的端口号)没有多大意义。否则,您希望如何获取任何数据?

我发现只有当SendTo()是第一个时,它才能工作,否则当ReceiveFrom()是第一个时,它就不工作了。有人能解释一下为什么它只能这样工作吗?谢谢,我不知道SendTo()做隐式绑定,现在它有了意义:)