2016-03-10 62 views
0

我創建了兩個項目,一個與客戶端,另一個與服務器交換兩者之間的文本;在同一臺計算機上,我運行這些exe。 我的客戶端連接代碼連接看了:客戶端和服務器使用C#的Socket連接

using (SocketClient sa = new SocketClient(host, port)) 
    { 
    sa.Connect(); 
    Console.WriteLine(sa.SendReceive("Message #" + i.ToString())); 
    } 
    sa.Disconnect();  

而socketclient是我的類,它包含這些方法和構造函數:

internal SocketClient(String hostName, Int32 port) 
{ 
IPHostEntry host = Dns.GetHostEntry(hostName);  
IPAddress[] addressList = host.AddressList;  
this.hostEndPoint = new IPEndPoint(addressList[addressList.Length - 1], port);  
this.clientSocket = new Socket(this.hostEndPoint.AddressFamily,  SocketType.Stream, ProtocolType.Tcp);  
}  
internal void Connect()  
{  
SocketAsyncEventArgs connectArgs = new SocketAsyncEventArgs();  
connectArgs.UserToken = this.clientSocket;  
connectArgs.RemoteEndPoint = this.hostEndPoint;  
connectArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnConnect);  
clientSocket.ConnectAsync(connectArgs);  
autoConnectEvent.WaitOne();  
SocketError errorCode = connectArgs.SocketError;  
if (errorCode != SocketError.Success)  
{ 
throw new SocketException((Int32)errorCode);  
}  
}  
internal void Disconnect()  
{  
clientSocket.Disconnect(false);  
}  
private void OnConnect(object sender, SocketAsyncEventArgs e)  
{  
autoConnectEvent.Set();  
this.connected = (e.SocketError == SocketError.Success);  
}  
internal String SendReceive(String message)  
{  
if (this.connected)  
{  
Byte[] sendBuffer = Encoding.ASCII.GetBytes(message);  
SocketAsyncEventArgs completeArgs = new SocketAsyncEventArgs();  
completeArgs.SetBuffer(sendBuffer, 0, sendBuffer.Length); 
completeArgs.UserToken = this.clientSocket; 
    completeArgs.RemoteEndPoint = this.hostEndPoint; 
    completeArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnSend);  
clientSocket.SendAsync(completeArgs);  
    AutoResetEvent.WaitAll(autoSendReceiveEvents);  
return Encoding.ASCII.GetString(completeArgs.Buffer, completeArgs.Offset,completeArgs.BytesTransferred);  
} 
else  
{  
throw new SocketException((Int32)SocketError.NotConnected);  
}  
} 

,而在服務器端代碼看起來像這樣:

SocketListener sl = new SocketListener(numConnections, bufferSize);  
sl.Start(port);  
Console.WriteLine("Server listening on port {0}. 
Press any key to terminate the server process...", port);  
Console.Read();  
sl.Stop();  

套接字監聽器是我的類,它包含此方法和構造函數:

internal SocketListener(Int32 numConnections, Int32 bufferSize)  
{  
    this.numConnectedSockets = 0; 
    this.numConnections = numConnections; 
this.bufferSize = bufferSize;  
this.readWritePool = new SocketAsyncEventArgsPool(numConnections);  
this.semaphoreAcceptedClients = new Semaphore(numConnections, numConnections);  
for (Int32 i = 0; i < this.numConnections; i++)  
{  
SocketAsyncEventArgs readWriteEventArg = new SocketAsyncEventArgs();  
readWriteEventArg.Completed += new EventHandler<SocketAsyncEventArgs> (OnIOCompleted);  
readWriteEventArg.SetBuffer(new Byte[this.bufferSize], 0, this.bufferSize);  
this.readWritePool.Push(readWriteEventArg);  
}  
} 
internal void Start(Int32 port) 
{  
IPAddress[] addressList = Dns.GetHostEntry(Environment.MachineName).AddressList;  
IPEndPoint localEndPoint = new IPEndPoint(addressList[addressList.Length - 1], port);  
this.listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);  
this.listenSocket.ReceiveBufferSize = this.bufferSize;  
      this.listenSocket.SendBufferSize = this.bufferSize;  
if (localEndPoint.AddressFamily == AddressFamily.InterNetworkV6)  
{  
this.listenSocket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false);  
this.listenSocket.Bind(new IPEndPoint(IPAddress.IPv6Any, localEndPoint.Port));  
}  
else  
{  
this.listenSocket.Bind(localEndPoint);  
}  
this.listenSocket.Listen(this.numConnections);  
this.StartAccept(null);  
mutex.WaitOne();  
}  

我已經端口轉發我的路由器,因爲服務器端EXE沒有監聽沒有端口轉發。
它工作正常與發送和接收相同的電腦和同一端口在家裏。
雖然當我嘗試到我的辦公室的電腦在它下面的行拋出異常的exe上同時運行代碼:

Exception thrown by socket

可以在任何一個指導我什麼問題,如何解決呢?
謝謝

回答

0

您是否嘗試過臨時禁用Windows防火牆?

+0

我不能因爲在我的辦公室有一臺服務器,由網絡工程師操作,我不能告訴他這樣做,因爲這會是安全問題。我也可能認爲這是防火牆阻止問題,我不能解決它通過有一些代碼邏輯...任何來源...謝謝 –

+0

你可以嘗試端口80,而不是自定義的,看看他們是否是開放的。 – Panda

+0

我嘗試使用該端口,但它響應該端口已被使用 –

相關問題