Hi.
I'm creating the LAN messenger (Client/Server) application using Async Socket Method based on MSDN Examples.
First I've created a Common Library including two Classes: Server and Client (because of some similar parts in both of them I preferred to create a Single Shared Class). Then made two windows form applications Client and Server,includin g that Library. Server starts listening and in the Client Main form when clicking on the "Connect Button",it connects successfully to the Server. Then with clicking on Disconnect Button, the client disconnected successfully. But My Problem Begins when I try to connect again, by throwing this Exception: "Only one usage of each socket address (protocol/network address/port) is normally permitted".
The point is that when I try to debug the client step by step slowly, it never throws that exception but when I step faster or Run without debugging, that error appears!!!
After tracing through the code, I've found that the error rises when calling "socket.EndConn ect()"
Please Help Me.
Here is My Snippet Codes:
I'm creating the LAN messenger (Client/Server) application using Async Socket Method based on MSDN Examples.
First I've created a Common Library including two Classes: Server and Client (because of some similar parts in both of them I preferred to create a Single Shared Class). Then made two windows form applications Client and Server,includin g that Library. Server starts listening and in the Client Main form when clicking on the "Connect Button",it connects successfully to the Server. Then with clicking on Disconnect Button, the client disconnected successfully. But My Problem Begins when I try to connect again, by throwing this Exception: "Only one usage of each socket address (protocol/network address/port) is normally permitted".
The point is that when I try to debug the client step by step slowly, it never throws that exception but when I step faster or Run without debugging, that error appears!!!
After tracing through the code, I've found that the error rises when calling "socket.EndConn ect()"
Please Help Me.
Here is My Snippet Codes:
Code:
(Client Class):
class ClientSocket{
...
public bool IsConnected(Socket client, int PollingTime)
{
return !(client.Poll(PollingTime, SelectMode.SelectRead) &&client.Available == 0);
}
public void Connect(EndPoint _remoteEP)
{
if (!IsConnected(this.client_Socket,pollingTime))
{
try{
client_Socket.BeginConnect(_remoteEP, newAsyncCallback(ConnectCallBack), client_Socket);
connectDone.WaitOne();
}
catch{
client_Socket.Shutdown(SocketShutdown.Both);
client_Socket.Disconnect(true);
connectDone.Reset();
}
}
else{…}
}
private void ConnectCallBack(IAsyncResultar){
StateObject state = newStateObject();
try{
Socket client = (Socket)ar.AsyncState;
state.workSocket = client;
client.EndConnect(ar);
client.BeginReceive(state.buffer,0,StateObject.BufferSize,SocketFlags.None,newAsyncCallback(ReceiveCallback),state);
connectDone.Set();
}
catch (Exception ex){
...
}
finally {... }
}
public void Disconnect()
{
try{
disconnectDone.Reset();
try {
client_Socket.Shutdown(SocketShutdown.Both);
client_Socket.BeginDisconnect(true, newAsyncCallback(DisconnectCallback), client_Socket);
ShowMessage("Disconnecting.....");
disconnectDone.WaitOne();
if (!client_Socket.Connected) {
ShowMessage("Disconnected!");
}
}
catch{}
}
private void DisconnectCallback(IAsyncResultar)
{
Socket client = (Socket)ar.AsyncState;
client.EndDisconnect(ar);
disconnectDone.Set();
}
...
}
=============================
In Main Client Form Application:
{
...
ClientSocket cilentSock=new ClientSocket();
private void buttonConnect_Click(object sender, EventArgs e)
{
buttonConnect.Enabled = false;
buttonDisconnect.Enabled = true;
int port = Int32.Parse(textBoxRemotePort.Text);
ServerEP = newIPEndPoint(IPAddress.Parse(textBoxRemoteAddress.Text), port);
clientSock.SetRemoteEP(ServerEP);
try{
clientSock.Connect(ServerEP);
}
catch {…}
finally{…}
}
private void buttonDisconnect_Click(object sender, EventArgs e)
{
try{
clientSock.Disconnect();
}
catch(Exception ex){…}
}
...
}
Comment