synchronous socket connection closed error

G

Guest

I got the following error when revieving data from a server.

hostSystem.Net.Sockets.SocketException: An existing connection was forcibly
closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[]
buffer, Int32 offset, Int32 size, SocketFlags socketFlags)

This error only happens when the long time recieving data, in this case 50
seconds. It looks to me like a time out error.

I have an j++ application which does the same thing but can revieve data
without any problem.

Code:
createSockConnection();
sock.Blocking = true;
sock.NoDelay = true;
sock.ReceiveTimeout = 10000000;

byte[] readBytes = new byte[1024];
Int32 sizeReceived = 0;

WebHeaderCollection Headers = new WebHeaderCollection();

while ((sizeReceived = sock.Receive(readBytes, readBytes.Length,
SocketFlags.None)) > 0)
{
nTotalBytes += sizeReceived;
ResponseText.Write(readBytes, 0, sizeReceived);
}

Please help.

thanks

Kevin Yang
 
W

William Stacey [MVP]

IIRC, you will get that error when the remote host closes the socket on
timeout or other. Is the client Receive not being posted for a long period?
If you own the server, you could increase the send/receive timeout to find
the right mix. But it sounds more like you have erroneous delays as the
client side.

--
William Stacey [MVP]

|I got the following error when revieving data from a server.
|
| hostSystem.Net.Sockets.SocketException: An existing connection was
forcibly
| closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[]
| buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
|
| This error only happens when the long time recieving data, in this case 50
| seconds. It looks to me like a time out error.
|
| I have an j++ application which does the same thing but can revieve data
| without any problem.
|
| Code:
| createSockConnection();
| sock.Blocking = true;
| sock.NoDelay = true;
| sock.ReceiveTimeout = 10000000;
|
| byte[] readBytes = new byte[1024];
| Int32 sizeReceived = 0;
|
| WebHeaderCollection Headers = new WebHeaderCollection();
|
| while ((sizeReceived = sock.Receive(readBytes, readBytes.Length,
| SocketFlags.None)) > 0)
| {
| nTotalBytes += sizeReceived;
| ResponseText.Write(readBytes, 0, sizeReceived);
| }
|
| Please help.
|
| thanks
|
| Kevin Yang
|
|
|
 
G

Guest

I connect to a slow server which I don't have control. Is that possible the
server closes it's connection after it send out the whole data, but client
side have not received it yet?

The server is slow but it still works. I have a socket based J++ application
which works perfectly for 3 years. Now I want to upgrade it to c# and I got
this problem.

The exception was triggered when I received about half of the data, and this
happens every time.

I do believe the server does not close its connection otherwise the j++ code
will not work as well.

Other than sock.ReceiveTimeout, anywhere else I can set tcp time out property?

Thanks

Kevin Yang
 
G

Guest

I found the cause.

After I removed line "sock.Shutdown(SocketShutdown.Send);", everything works
fine.

I seems the server have problem to handle this shutdown send.

thanks for your reply.

Kevin Yang
 
W

William Stacey [MVP]

That would seem to be a bug on the server side. I assume the server is
async so when it reads a shutdown, it closes the socket before all sends are
finished. On the other hand, this may be the protocol the server expects.
Glad you found it.

--
William Stacey [MVP]

|I found the cause.
|
| After I removed line "sock.Shutdown(SocketShutdown.Send);", everything
works
| fine.
|
| I seems the server have problem to handle this shutdown send.
|
| thanks for your reply.
|
| Kevin Yang
|
|
| "William Stacey [MVP]" wrote:
|
| > IIRC, you will get that error when the remote host closes the socket on
| > timeout or other. Is the client Receive not being posted for a long
period?
| > If you own the server, you could increase the send/receive timeout to
find
| > the right mix. But it sounds more like you have erroneous delays as the
| > client side.
| >
| > --
| > William Stacey [MVP]
| >
| > | > |I got the following error when revieving data from a server.
| > |
| > | hostSystem.Net.Sockets.SocketException: An existing connection was
| > forcibly
| > | closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[]
| > | buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
| > |
| > | This error only happens when the long time recieving data, in this
case 50
| > | seconds. It looks to me like a time out error.
| > |
| > | I have an j++ application which does the same thing but can revieve
data
| > | without any problem.
| > |
| > | Code:
| > | createSockConnection();
| > | sock.Blocking = true;
| > | sock.NoDelay = true;
| > | sock.ReceiveTimeout = 10000000;
| > |
| > | byte[] readBytes = new byte[1024];
| > | Int32 sizeReceived = 0;
| > |
| > | WebHeaderCollection Headers = new WebHeaderCollection();
| > |
| > | while ((sizeReceived = sock.Receive(readBytes, readBytes.Length,
| > | SocketFlags.None)) > 0)
| > | {
| > | nTotalBytes += sizeReceived;
| > | ResponseText.Write(readBytes, 0, sizeReceived);
| > | }
| > |
| > | Please help.
| > |
| > | thanks
| > |
| > | Kevin Yang
| > |
| > |
| > |
| >
| >
| >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top