【问题标题】:Calling async handler method causes: Socket must be bound to a local endpoint to perform this operation调用异步处理程序方法导致:套接字必须绑定到本地端点才能执行此操作
【发布时间】:2014-03-04 10:07:12
【问题描述】:

我创建了等待数据的服务器。当我调用AcceptAsync 时,处理方法connectArgs_Accepted 被调用,即使方法为空 iniside 也会导致异常和崩溃。如何处理?

   Thread thread = new Thread(new ThreadStart(connect));
        thread.Start();
    }

    Socket connection;
    SocketAsyncEventArgs connectArgs;
    string ip = "192.168.0.13";
    int port = 13001;
    private void connect() {
        connectArgs = new SocketAsyncEventArgs { RemoteEndPoint = new DnsEndPoint(ip, port) };

        connectArgs.Completed += connectArgs_Accepted;
        connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                  connection.AcceptAsync(connectArgs);
    }

    void connectArgs_Accepted(object sender, SocketAsyncEventArgs e) {//that causes exception
     /*   if (e.SocketError == SocketError.Success) {
            // data sent
            try {

                Receive();

            } catch (Exception ex) { Console.WriteLine(ex); }
        } else {
            // error
        }*/
    }

输出:

   System.InvalidOperationException: Socket must be bound to a local endpoint to perform this operation.
   at System.Net.Sockets.Socket.AcceptAsync(SocketAsyncEventArgs e)
   at HomeSecurityClient.JPGServer.connect()
The thread 0x458 has exited with code 259 (0x103).

。 客户端(发送方)

namespace HomeSecurity {
    //singleton
    public class JPGClient {
        IPAddress ip;
        int port;
        public static JPGClient JPG_CLIENT = null;

        public JPGClient(IPAddress ip, int port) {
            this.ip = ip;
            this.port = port + 1;
            JPG_CLIENT = this;

            Thread thread = new Thread(new ThreadStart(sendjpg));
            thread.Start();

        }
        public void sendjpg() {
            TcpClient client = new TcpClient();
            try {
                System.Diagnostics.Debug.WriteLine(ip  + " X " + 13001);
                client.Connect(ip, 13001);
                System.Diagnostics.Debug.WriteLine(ip + " polaczzyl X " + 13001);


                // Retrieve the network stream.  
                NetworkStream stream = client.GetStream();
                ImageBrush imageBrush = new ImageBrush();
                imageBrush.ImageSource = new BitmapImage(new Uri(@"C:\a.jpg", UriKind.Absolute));
                MessageData data = new MessageData(imageBrush);

                IFormatter formatter = new BinaryFormatter();

                while (true) {
                    System.Diagnostics.Debug.WriteLine("SLEEEEEEEEEEEEEEE");
                    formatter.Serialize(stream, data);
                    Thread.Sleep(1000);
                    // data.GetNewImage();
                }
            } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e); }
        }
    }
}

【问题讨论】:

    标签: c# .net tcp network-programming windows-phone


    【解决方案1】:

    服务器套接字(接受客户端连接)应首先绑定到本地网络接口和端口。 在调用 AcceptAsync 之前,您应该将套接字绑定到本地端点。

    看看Socket.Bind信息(http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.bind(v=vs.110).aspx)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      相关资源
      最近更新 更多