Visual C# Developer Center > Visual C# Forums > Visual C# General > TcpListener/writing data back to client
Ask a questionAsk a question
 

AnswerTcpListener/writing data back to client

  • Tuesday, November 03, 2009 4:43 PMJim McKinley Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I wrote an app that uses TcpListner to listen for data from a client (which I also wrote). These apps work fine... except for one situation.

    I attempt to send a response to the client using a network stream:

    // Send back a response.
    stream.Write(msg, 0, msg.Length);

    The Listener is in a loop... listens for a connection, deals with the connection (which includes sending a response to  the client), then goes back looking for another client connection.

    The problem occurs if the client aborts early. I'm testing to see what my server does if the client does not wait for the response back. What happens is that an error is thrown and goes into my try/catch/finally blocks and ends... therefore, if the client does not wait for the response, the server errs and shuts down. How do I prevent this from happening? I would like the server to log an error in the event log and then go back to listening for connections.

    Thanks!

Answers

All Replies