Writing a windows service with a socket interface.

From: Peter Strĝiman (blah_at_blahblahblah)
Date: 02/17/05


Date: Thu, 17 Feb 2005 11:04:41 +0100

Hi.

I'm writing a windows service that is accessible via sockets. I have tried
to find literature on the subject but I couldn't.

Basically my service, when asked to start, starts up a new thread that just
waits for connection. See bottom of this mail.

The StreamParser class is the facade that parses data sent to my service,
and sends a response back to the client.

I'm left with a couple of questions.

A: Are there any good resources on writing windows services with socket
interfaces anywhere?

B: Is my main code robust. E.g. if some idiot accidentally pulled the
network cable out of the server, would _listener.AcceptTcpClient() throw an
exception. Or perhaps if the IP address of the server was modified while the
service was running.

C: When a client connects and sends a request and thereafter gets a
response, should I close the socket, or keep the socket open. I'm writing
the class that handles the client's communication with the server as well,
so I can control these things. If the best option is to leave the connection
open, I would implement a sort of connection pool of course.

D: How should I detect if clients are disconnected? My StreamParser
implementation simply calls the blocking Stream.Read() method, waiting for
data to arrive. Can I count on that the stream throws an IOException if the
client is disconnected? E.g. if the network cable is unplugged (either from
the server or client), someone changes the IP address of the server/client,
or the client process suddenly terminates. Of course I would implement a
"close" command in my communication protocal that would manually close a
connection.

Thanks in advance,
Peter Strĝiman

// service main thread
try
{
  _listener = new TcpListener( _ipEndPoint );
  _listener.Start();
  _state = TcpInterfaceState.Running;
  while( true )
  {
  TcpClient client = _listener.AcceptTcpClient();
  if ( client != null )
  {
    Stream stream = client.GetStream();
    StreamParser parser = new StreamParser( stream );
    Thread thread = new Thread( new ThreadStart( parser.Run ) );
    thread.ApartmentState = ApartmentState.STA;
    thread.Start();
  }
}
}
catch( Exception e )
{
  // error handling code
}



Relevant Pages

  • Re: Socket switch delay
    ... both at the client and at the server (and why ... would you set the send buffer size to zero on a non-overlapped ... One glaring error is your client does ... So when you use a single socket, ...
    (microsoft.public.win32.programmer.networks)
  • Re: Locking on async calls
    ... you must synchronize the entire SendMessage routine as an atomic ... operation to prevent mixed messages from being transmitted to the server. ... You are correct that read and write on the socket do not interfere with each ... If you want to handle multiple client connections from one server object ...
    (microsoft.public.dotnet.general)
  • Re: TCP server stop receiving new connections
    ... reset the event mask of your listening socket each time you ... I have a strange problem in my class library used by all our client ... server applications. ... incomming connections, but keeps current connections. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Design issue with WinSock/GetQueuedCompletionStatus
    ... delegate that to a shutdown routine called after all worker threads ... The application I've created is a server accepting connections on a few ... different TCP/IP ports and then lets the client run different commands. ... a TCP/IP socket can be closed for 2 different reasons: ...
    (microsoft.public.win32.programmer.networks)
  • Re: socket communication: socket doesnt connect
    ... Microsoft MVP, MCSD ... As far as your server code goes, ... accept the listening socket. ... Client client = new Client; ...
    (microsoft.public.vc.language)