gpt4 book ai didi

C# 套接字连接错误

转载 作者:行者123 更新时间:2023-12-03 11:58:35 25 4
gpt4 key购买 nike

我正在使用 C# 中的套接字,但出现以下错误:

A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied



这是我正在执行的代码:
    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 协议(protocol)上工作,因为一些消息将 > 1500 字节

谢谢!

最佳答案

您需要 .BeginAccept() 才能收到。

Here's a link with a sinple Asynchronous Socket Server example

关于C# 套接字连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5582464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com