Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.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

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#套接字连接错误_C#_Sockets_Networking - Fatal编程技术网

C#套接字连接错误

C#套接字连接错误,c#,sockets,networking,C#,Sockets,Networking,我正在使用C#中的套接字,并得到以下错误: 发送或接收数据的请求失败 不允许,因为套接字不可用 已连接且(当通过网络发送时) 数据报套接字(使用sendto调用) 没有提供地址 以下是我正在执行的代码: private void HostSubscriberService() { IPAddress ipV4 = PublisherService.ReturnMachineIP(); var localEP = new IPEndPoint(ip

我正在使用C#中的套接字,并得到以下错误:

发送或接收数据的请求失败 不允许,因为套接字不可用 已连接且(当通过网络发送时) 数据报套接字(使用sendto调用) 没有提供地址

以下是我正在执行的代码:

    private void HostSubscriberService()
    {
        IPAddress ipV4 = PublisherService.ReturnMachineIP();

        var localEP = new IPEndPoint(ipV4, _port);
        var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        server.Bind(localEP);

        StartListening(server);
    }

    private void StartListening(Socket server)
    {
        EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
        while (true)
        {
            var data = new byte[1024];
            int recv = server.ReceiveFrom(data, ref remoteEP);
            string messageSendFromClient = Encoding.ASCII.GetString(data, 0, recv);

            MessageBox.Show(messageSendFromClient);
        }
    }
错误发生@
int recv=server.ReceiveFrom(data,ref remoteEP)

我只需要侦听新的传入连接,然后打印从新客户端发送的消息

我需要它在TCP协议上工作,因为有些消息将>1500字节

谢谢

您需要先执行.beginacept(),然后才能接收


ReceiveFrom()表示UDP,使用Accept()和Receive()表示TCP

Dude,ReceiveFrom()表示UDP,使用Accept()和Receive()表示TCPAh!好的,
ReceiveFrom
是UDP。有没有办法使用TCP从连接的客户端获取
端点