Socket shuts down unexpectedly

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SBC

    Socket shuts down unexpectedly

    Hello,

    I have developed a windows service using C#, TcpListener class and its only
    function is to listen for clients to connect and send back a respsonse. I
    have a customer who states that everyday they need to restart the service so
    that the application starts listening on the sockets again.

    I have not been able to recreate this situation and don't know why the
    socket would shutdown. Can anyone point me to a reason why sockets would
    shutdown unexpectedly?

    Within the constructor I create a new TcpListener object...

    <start of code snippet>
    //start listing on the given port
    port = Convert.ToInt32 (sPort);
    _tcpListener = new TcpListener(ipA ddress, port) ;
    _tcpListener.St art();

    //start the thread which calls the method 'StartListen'
    m_thread = new Thread(new ThreadStart(Sta rtListen));
    m_thread.Start( ) ;
    <end of code snippet>


    in my next snippet for "StartListe n" I wait until a client connects...
    <Start Snippet>
    StartListen()
    {
    // variables here...
    while(true)
    {
    // Accept a new connection
    Socket _clientSocket = _tcpListener.Ac ceptSocket();
    if (_clientSocket. Connected)
    {
    // At this point we have another application that does logging since the
    windows
    // service does not have a user interface...
    TcpClient _tcpClient = new TcpClient();
    try
    {
    _tcpClient.Conn ect(ipAddress, port)
    NetworkStream ns = _tcpClient.GetS tream();
    BinaryWriter bw = new BinaryWriter(ns );
    bw.Write(sb.ToS tring());
    _tcpClient.Clos e();
    sb.Remove(0, sb.Length); // sb is a StringBuilder
    }
    catch(Exception ex)
    {
    // stuff
    }
    }
    }


    One of the things I have recently read is that I need to close the
    NetworkStream also, do you think this would cause the sockets to shutdown?
    So far I don't receive any types of error messages, the socket just seems to
    stop communicating and the windows service needs to be rebooted...

    Thank you for your time and help.

Working...