RE: Client connection gets forcibly closed by Server
- From: calton@xxxxxxxxxxxxxxxxxxxx (Chris Alton [MSFT])
- Date: Mon, 15 Oct 2007 18:37:57 GMT
What OS and Service Pack are you using on both sides of the connection?
Also, what NIC Card Brand/Model are you using on both ends?
-------------------------------------
Chris Alton, Microsoft Corp.
SQL Server Developer Support Engineer
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Robin" <diilbert.atlantis@xxxxxxxxx>using
Newsgroups: microsoft.public.dotnet.languages.csharp
Subject: Client connection gets forcibly closed by Server
Date: Sat, 13 Oct 2007 15:39:23 -0300
Here is the run down. It is a Client/Server application where the client
requests data from the server and the request is fufilled as a reply
the same connection.track
The "An existing connection was forcibly closed by the remote host"
exception rises on the Client under strange circumstances, since if I
the server's output it shows that the data was send to the Clients.Send(Encoding.UTF8.GetBytes(sData),Encoding.UTF8.GetBytes(sData).Length,So
successfully (all 217 bytes for example), but on the Client side it gets
this exception and 0 bytes are read.
Here is how the Server is started and Reads the Socket if this helps:
protected void InitializeServer(int nPort)
{
tcpLsn = new TcpListener(nPort);
tcpLsn.Start();
tcpThd = new Thread(new ThreadStart(WaitingForClient));
tcpThd.Start();
}
public void WaitingForClient()
{
ClientData CData;
while(true)
{ /* Accept will block until someone connects */
CData.structSocket = tcpLsn.AcceptSocket();
Interlocked.Increment(ref connectId);
CData.structThread = new Thread(new ThreadStart(ReadSocket));
lock(this)
{ // it is used to keep connected Sockets and active thread
dataHolder.Add(connectId, CData);
}
CData.structThread.Start();
}
}
public void ReadSocket()
{
/* realId will be not changed for each thread, but connectId is
* changed. it can't be used to delete object from Hashtable*/
long realId = connectId;
Byte[] receive;
ClientData cd = (ClientData)dataHolder[realId];
Socket s = cd.structSocket;
int ret = 0;
string sData = null;
string[] sReceived;
bool bExit = false; // Exit on Success
while (!bExit)
{
if(s.Connected)
{
receive = new Byte[100] ;
try
{
ret = s.Receive(receive,receive.Length,0);
// Do stuff with received data AND send back a response
int sentBytes =
cketFlags.None);
Threaded
}
catch (Exception e)
{
if( !s.Connected )
{
break;
}
else
cwp.mobile.utils.DisplayError(e);
}
bExit = true;
}
}
CloseTheThread(realId);
}
private void CloseTheThread(long realId)
{
try
{
ClientData clientData = (ClientData)dataHolder[realId];
clientData.structThread.Abort();
}
catch(Exception )
{ lock(this)
{
dataHolder.Remove(realId);
}
}
}
And here is how the client essentially works, it is a simple Single
connection. And just a foot note about the code below I use it in 2the
different projects, one is a wired client running on a Win32 machine and
other is running on a Wireless handheld running Windows Mobile, and bothare
getting the exact same problem. At first I thought it was communicationthe
(wireless network problems) but I have my doubts.
// Pass the function data as a string and TRUE boolean value to wait for
reply (blocking)IPEndPoint(IPAddress.Parse(this.config.sIpAddress),
private string SendServerData(string sData, bool bReadWait)
{
try
{
tcpclnt = new TcpClient();
tcpclnt.Connect(new
this.config.nPort));
stm = tcpclnt.GetStream();
const int byteCount = 300;
int readBytes = 0;
writeToServer(sData);
if(bReadWait)
{
bool exit = false;
while(!exit)
{
try
{ readBuffer = new Byte[byteCount];
readBytes = stm.Read(readBuffer,0,byteCount);
if(Encoding.ASCII.GetString(readBuffer,0,readBytes).Length > 0)
exit = true;
}
catch (Exception){break;}
}
stm.Close();
tcpclnt.Close();
return Encoding.ASCII.GetString(readBuffer,0,readBytes);
}
else
return "";
}
catch(SocketException sockexcep)
{
cwp.mobile.utils.DisplayError(sockexcep);
return "-1";
}
catch(Exception e)
{
cwp.mobile.utils.DisplayError(e);
return "-1";
}
}
private void writeToServer(string sData)
{
ASCIIEncoding encord = new ASCIIEncoding();
writeBuffer = encord.GetBytes(sData);
if(stm != null)
stm.Write(writeBuffer,0,writeBuffer.Length);
}
Any thoughts at all on how to improve either of these would be awesome
because I spend 2 hours googling this and have come up with nothing
substantial.
Thanks
.
- Follow-Ups:
- References:
- Client connection gets forcibly closed by Server
- From: Robin
- Client connection gets forcibly closed by Server
- Prev by Date: Re: simple managementobject question
- Next by Date: Re: Type convertsion from string
- Previous by thread: Client connection gets forcibly closed by Server
- Next by thread: Re: Client connection gets forcibly closed by Server
- Index(es):
Relevant Pages
|