2013-04-23 39 views
2

我一直在不断收到此错误,并且我相信我已将自己的问题缩小到了我在64位计算机上的using a 32-bit侦听器。有没有什么办法让它在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'

+0

,AddressFamily是一个枚举。尝试'== AddressFamily.InterNetwork'而不是== == InterNetwork“' – 2013-04-23 13:10:39

+0

在你的相关问题得到了代码中的错误的任何代码 – Geoff 2013-04-23 13:10:41

回答

3

我想你是错过了Socket Binding,之前一个套接字可以在端口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); 

P.S. :对于侦听端口,始终使用大于4000的值!

+0

这就是它。谢谢 :) – Kermit 2013-04-23 13:23:34

0

你检查MaxConnections财产作为the documentation讨论?

该文档还介绍了如何检查ErrorCode以获取更多详细信息。也许你应该探索这个选项。

0

也许下面的示例也会帮助你。

Socket clientSocket; 
System.Net.IPEndPoint clientEndPoint; 
System.Net.Sockets.NetworkStream networkStream; 
IAsyncResult beginAcceptAsyncResult; 
System.Threading.WaitHandle []waitHandleArray; 
int waitHandleSignalled; 

System.Net.IPEndPoint listenEndpoint = new System.Net.IPEndPoint("127.0.0.1", 45000); //make sure port is free (check with cmd: netstat -an) 
System.Net.Sockets.TcpListener listener = new System.Net.Sockets.TcpListener(listenEndpoint); 

waitHandleArray = new System.Threading.WaitHandle[1]; 

listener.Start(); 

do 
{ 
    try 
    beginAcceptAsyncResult = listener.BeginAcceptSocket(null, null); 
    waitHandleArray[0] = beginAcceptAsyncResult.AsyncWaitHandle; 
    waitHandleSignalled = System.Threading.WaitHandle.WaitAny(waitHandleArray); 

    if (waitHandleSignalled != 0) 
    { 
     clientSocket = _TCPListener.EndAcceptSocket(BeginAcceptAsyncResult); 

     clientEndPoint = clientSocket.RemoteEndPoint as System.Net.IPEndPoint; 

     networkStream = new System.Net.Sockets.NetworkStream(clientSocket, true); 

     //do more with NetworkStream... 
    } 
} 
//... 
//... 
在我不找相关的``==为