By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,992 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,992 IT Pros & Developers. It's quick & easy.

synchronous socket connection closed error

P: n/a
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

May 16 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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]

"Kevin" <Ke***@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
|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
|
|
|
May 16 '06 #2

P: n/a
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

"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]

"Kevin" <Ke***@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
|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
|
|
|

May 16 '06 #3

P: n/a
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]

"Kevin" <Ke***@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
|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
|
|
|

May 16 '06 #4

P: n/a
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]

"Kevin" <Ke***@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
|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]
| >
| > "Kevin" <Ke***@discussions.microsoft.com> wrote in message
| > news:3D**********************************@microsof t.com...
| > |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
| > |
| > |
| > |
| >
| >
| >
May 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.