-
March 15th, 2009, 01:40 PM
#1
TCP Client/Server close connection
I'm trying to write an server application. When the server has an error (ie the client sent invalid data), I want to send a signal back to the client and then close the connections.
Code:
//server error handling code
stream.WriteByte(SERVER_FAIL);
stream.Flush();
client.Close();
//wait for next tcp connection
However on the client side, when I try to read this status byte, I am getting and error saying that the connection has been closed by the host. Should I only be closing the network stream and client on one side of the connection?
-
March 15th, 2009, 02:00 PM
#2
Re: TCP Client/Server close connection
Use Socket.Shutdown rather than calling Close. This will allow the remote side to read the last bits of data. If you call Close, the socket is terminated immediately and the last bits of data might not be transmitted to the remote host.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|