gpt4 book ai didi

c# - 传递给套接字监听器的参数无效

转载 作者:行者123 更新时间:2023-11-30 22:18:21 25 4
gpt4 key购买 nike

我一直不断收到此错误,我相信我已将我的问题缩小为我是 using a 32-bit 64 位机器上的监听器。有什么方法可以让它在 64 位系统上运行吗?

18      Socket sListener;
...
34 permission = new SocketPermission(NetworkAccess.Accept, TransportType.Tcp, "", SocketPermission.AllPorts);

36 //Listening Socket object
37 sListener = null;

39 //Ensure the code has permission to access the Socket
40 permission.Demand();

42 IPHostEntry ipHost = Dns.GetHostEntry("");
43 IPAddress ipAddress = ipHost.AddressList[2];
44 ipEndPoint = new IPEndPoint(ipAddress, 4510);

46 sListener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
...
71 sListener.Listen(10);
72
73 //Begins an asynchronous operation to accept an attempt
74 AsyncCallback aCallback = new AsyncCallback(AcceptCallback);
75 sListener.BeginAccept(aCallback, sListener);

我试着按照这个 relevant 中的代码问题,但它给了我错误:

Operator '==' cannot be applied to operands of type 'System.Net.Sockets.AddressFamily' and 'string'

最佳答案

我想你错过了 Socket Binding , 在 Socket 可以之前 Listen在港口。

根据 MSDN:

Use the Bind method if you need to use a specific local endpoint. You must call Bind before you can call the Listen method. You do not need to call Bind before using the Connect method unless you need to use a specific local endpoint. You can use the Bind method on both connectionless and connection-oriented protocols.

Before calling Bind, you must first create the local IPEndPoint from which you intend to communicate data. If you do not care which local address is assigned, you can create an IPEndPoint using IPAddress.Any as the address parameter, and the underlying service provider will assign the most appropriate network address. This might help simplify your application if you have multiple network interfaces. If you do not care which local port is used, you can create an IPEndPoint using 0 for the port number. In this case, the service provider will assign an available port number between 1024 and 5000.

70:    sListen.Bind(your IP end point)
71: sListener.Listen(10);

附言: 始终使用大于 4000 的值作为监听端口!

关于c# - 传递给套接字监听器的参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16170058/

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