CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2008
    Posts
    18

    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?

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    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
  •  





Click Here to Expand Forum to Full Width

Featured