2013-04-23 37 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... 
    } 
} 
//... 
//... 
在我不找相關的``==爲