[Mono-dev] Listening in on a port (was the daemon thingy a week or so back)

PFJ pjohnson1 at uclan.ac.uk
Tue Jan 12 08:08:02 EST 2010


Hi,

I'm making some progress with my daemon in C# to listen in on port 80 and
monitor how much time my son is spending on line, but I've hit a snag.

Currently, my code looks like this 

  class Server
  {
    private TcpListener tcpListener;
    private Thread listenThread;

    public Server()
    {
      this.tcpListener = new TcpListener(IPAddress.Any, 80);
      this.listenThread = new Thread(new ThreadStart(ListenForClients));
      this.listenThread.Start();
    }
  


private void ListenForClients()
{
  this.tcpListener.Start();

  while (true)
  {
    //blocks until a client has connected to the server
    TcpClient client = this.tcpListener.AcceptTcpClient();

    //create a thread to handle communication
    //with connected client
    Thread clientThread = new Thread(new
ParameterizedThreadStart(HandleClientComm));
    clientThread.Start(client);
  }
}

private void HandleClientComm(object client)
{
  TcpClient tcpClient = (TcpClient)client;
  NetworkStream clientStream = tcpClient.GetStream();

  byte[] message = new byte[4096];
  int bytesRead;

  while (true)
  {
    bytesRead = 0;

    try
    {
      //blocks until a client sends a message
      bytesRead = clientStream.Read(message, 0, 4096);
    }
    catch
    {
      //a socket error has occured
      break;
    }

    if (bytesRead == 0)
    {
      //the client has disconnected from the server
      break;
    }

    //message has successfully been received
    ASCIIEncoding encoder = new ASCIIEncoding();
    System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0,
bytesRead));
  }

  tcpClient.Close();
}

public static void Main()
{
	Server s = new Server();
}

Works fine until I start a browser. Then it aborts with

System.Net.Sockets.SocketException: Only one usage of each socket address
(protocol/network address/port) is normally permitted
   at System.Net.Sockets.Socket.DoBind
   at System.Net.Sockets.Socket.Bind
   at System.Net.Sockets.TcpListener.Start
   at System.Net.Sockets.TcpListener.Start
   at TCPServerTutorial.Server.ListenForClients in
\\ceg.local\uclan\mydocs\pjohnson\SharpDevelop
Projects\tcpmonitor\tcpmonitor\Program.cs:line 34
   at System.Threading.ThreadHelper.ThreadStart_Context
   at System.Threading.ExecutionContext.Run
   at System.Threading.ThreadHelper.ThreadStart

which looks to me like two apps can't be listening in on port at the same
time. I guess I'm doing something wrong here, but can't see what it is - I'm
guessing that I've coded a server instead of a client, but then I'm not sure
if the software should be a client rather than a server! I'll admit now that
this is the first time I've done anything in C# with networking ports.

The idea is that once I get this running, the trivial matter of timing
(using either a thread timer or System.Timer) will be easy to put in and all
will be good with the world.

Any help on this would be good.

TTFN

Paul
-- 
View this message in context: http://old.nabble.com/Listening-in-on-a-port-%28was-the-daemon-thingy-a-week-or-so-back%29-tp27127261p27127261.html
Sent from the Mono - Dev mailing list archive at Nabble.com.



More information about the Mono-devel-list mailing list