I write host-client prorject using C#. It has been working well when I run the host on Windows XP and client on Windows 7. But then I always get connection error when I run both host and client on Windows XP. It throws exception on this function Socket.BeginReceive() and the exception error is "An existing connection was forcibly closed by the remote host"
Below is the host code:
private Socket gServerSocket;
private Socket gWorkerSocket;
private MainformGUI mainFormGuiObj;
private byte[] byteData;
public Dictionary<string, string> clientMessageDictionary;
public void DisplayDebugMsg(bool error, string msg)
{
try
{
if (error == false)
System.Diagnostics.Debug.WriteLine(msg);
else
mainFormGuiObj.DelegateMsgBoxErrorText(msg);
}
catch (Exception err)
{
mainFormGuiObj.DelegateMsgBoxErrorText(err.Message);
}
}
public void BeginConnection()
{
DisplayDebugMsg(false, "inside BeginConnection()");
//create a TCP/IP socket
gServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//IPEndPoint represents a network computer in IP address and port number
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 3000);
//bind the gSocketListener to the port
gServerSocket.Bind(ipLocal);
//place a Socket in a listening state
gServerSocket.Listen(4);
gServerSocket.BeginAccept(new AsyncCallback(OnConnectedCallback), gServerSocket);
mainFormGuiObj.gDelegateDisplayMsgConnected("TestClient not connect", true);
}
private void OnConnectedCallback(IAsyncResult asyn)
{
try
{
DisplayDebugMsg(false, "inside OnConnectedCallback()");
Socket socket = (Socket)asyn.AsyncState;
gWorkerSocket = socket.EndAccept(asyn);
if (asyn.AsyncState == null)
DisplayDebugMsg(false, "asyn.AsyncState == null");
else
DisplayDebugMsg(false, "asyn.AsyncState not null");
if (gWorkerSocket.Connected)
{
mainFormGuiObj.gDelegateDisplayMsgConnected("TestClient connected", false);
DisplayDebugMsg(false, "client connected");
WaitForData(gWorkerSocket);
}
else
{
DisplayDebugMsg(false, "connection fail");
mainFormGuiObj.gDelegateDisplayMsgConnected("TestClient disconnected", true);
}
}
catch (SocketException error)
{
DisplayDebugMsg(true, "OnConnectedCallback(), " + error.Message);
}
catch (Exception exception)
{
DisplayDebugMsg(true, "OnConnectedCallback(), " + exception.Message);
}
}
private void WaitForData(Socket socket)
{
try
{
DisplayDebugMsg(false, "inside WaitForData()");
AsyncCallback asyncCallback = new AsyncCallback(DataReceivedCallBack);
socket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, asyncCallback, socket);
}
catch (Exception exception)
{
DisplayDebugMsg(true, "WaitForData(), " + exception.Message);
}
}
private void DataReceivedCallBack(IAsyncResult asyn)
{
try
{
DisplayDebugMsg(false, "inside DataReceivedCallBack()");
//do something to data
}
catch (Exception exception)
{
//handling errors
}
}
When I run host then after I run client program, it calls this socket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, asyncCallback, socket) and that is when exception thrown, "An existing connection was forcibly closed by the remote host"
I have tried both secured and nonsecured wireless connections.
Please give any advice is possible, thanks.

New Topic/Question
Reply




MultiQuote



|