[Mono-list] TCPListener problem

Lee lee at datatrakpos.com
Wed Jun 21 10:47:59 EDT 2006


 

Hello, 

I have done what Martin suggested which was the following:

Specifically use localhost 127.0.0.1 as the local address and check netstat.
It appears that the socket does not start because it is not showing up in
netstat.

As I mentioned in a prior post, I have placed logging routines at various
points in the code:

OnStart event
OnStop event
Beginning of the StartServ() method

When I used:
SockServer = new TcpListener(IPAddress.Any, port);

Both the OnStart and OnStop events were firing without the socket server
listing.

When I changed to:
IPAddress addr = IPAddress.Parse("127.0.0.1");
SockServer = new TcpListener(addr, port);
...only the OnStart event appears to fire.

Again, If I run the socket server (both in and out of VS IDE) on Windows the
server works fine with no problems.  Could it be that there is a difference
in the implementation of sockets namespace in Mono has a difference that I
need to account for?

Thanks again,

Lee


> 
> 
> Hi, I have written a prototype TCPListerner service.  It 
> works fine on my local XP dev box (isn't that always the 
> case), but not on the linux box.  
> 
> I started the service with:
> 
> $ Mono-service myservice.exe
> 
> Checked with ps and ps -eaf and it says that its running.  So 
> at this point, I'm not sure if it's the linux box or 
> something in my code that does not run well in mono.
> 
> Is there anything in particular I should try?  I have 
> attached the code that starts the service below.
> 
> Thanks,
> 
> --
> Lee
> 
> protected override void OnStart(string[] args) { 
> System.Threading.Thread thread = new System.Threading.Thread(
>   new System.Threading.ThreadStart(this.StartServe));
> thread.Start();
> }
> 
> private void StartServe()
>     {
>       TcpListener SockServer = null;
>       try
>       {
> 
>         // Setup socket server
>         Int32 port = 4567;
>         SockServer = new TcpListener(IPAddress.Any, port);
>         SockServer.Start();
>         
>         // create buffer to read data
>         Byte[] bytes = new Byte[256];
>         string data = null;
> 
>         while (true)
>         {
>           // check to see if request is pending
>           if (SockServer.Pending())
>           {
>             TcpClient client = SockServer.AcceptTcpClient();
> 
>             data = null;
> 
>             NetworkStream stream = client.GetStream();
> 
>             int i;
> 
>             // flag for loop
>             bool Alldone = false;
>             // End of Transmission character 
>             char theChar = '\u0004';
>             string ch = theChar.ToString();
> 
>             while (!Alldone)
>             {
>               i = stream.Read(bytes, 0, bytes.Length);
>               // translate stream to string data;
>               data += System.Text.Encoding.ASCII.GetString(bytes);
>               if ((data.Contains(ch)) || (i == 0))
>                 Alldone = true;
>             }
> 
>             int POS = data.IndexOf(ch);
>             data = data.Substring(0, POS);
>             // strip off EOT character
> 
> 
>             // create requestprocessing object here and hand off
>             RequestHandler handler = new RequestHandler(data);
>             string response = handler.ProcessRequest() + ch;
> 
>             // send back result
>             byte[] ret = 
> System.Text.Encoding.ASCII.GetBytes(response);
>             stream.Write(ret, 0, ret.Length);
> 
>           }
>         }
> 
>       }
>       catch (SocketException e)
>       {
>       }
>       finally
>       {
>         SockServer.Stop();
>       }
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com 
> http://lists.ximian.com/mailman/listinfo/mono-list



More information about the Mono-list mailing list