[Mono-list] Simple Threading+Net program still not working

Candide Kemmler candide@urbanium.tv
Fri, 19 Apr 2002 17:47:59 +0200


Hi,

Some recent messages were dealing with the resolution of a Threading 
problem. I originally sent a message asking for help for a program 
malfunction of which the source code was very similar to what follows 
here. The problem was identified as boiling down to the libc not being 
compiled with threading-enabled. Simpler tests show that this particular 
issue is now solved. However, the original error still occurs, like the 
following program should make evident. I apologize in advance if the 
error lies in a bug in the program itself, as I am a perfect beginner 
with C#.

Candide

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Text;

class JxtaDelegate {

   public static void Main () {
     new MainThreadHandler ();
   }

}

public class MainThreadHandler {
   private TcpListener myTcpListener;

   public MainThreadHandler() {
     myTcpListener = new TcpListener(9705);
     myTcpListener.Start();
     Console.WriteLine("Listener started. Press Ctrl+Break to stop.");

     while (true) {
       try {
         Socket s = myTcpListener.AcceptSocket ();
         WorkerThreadHandler myWorkerThreadHandler = new 
WorkerThreadHandler();
         myWorkerThreadHandler.mySocket = s;
         ThreadStart myThreadStart = new 
ThreadStart(myWorkerThreadHandler.HandleThread);
         Thread myWorkerThread = new Thread(myThreadStart);
         myWorkerThread.Name = "Created at " + DateTime.Now.ToString();
         myWorkerThread.Start();
       } catch ( Exception e ) {
         Console.WriteLine ( "Problem launching thread" );
       }
     }
   }
}

class WorkerThreadHandler {
   public Socket mySocket;

   public void HandleThread() {
     try {
       string greeting = "yes, I'm there";
       ASCIIEncoding AE = new ASCIIEncoding ();
       Console.WriteLine ( "sending greeting" );
       mySocket.Send(AE.GetBytes(greeting));
       Console.WriteLine("Closing connection with client.");
       mySocket.Close();
     } catch ( Exception e ) {
       Console.WriteLine ( "Exception is " + e );
     } finally {
       mySocket.Close ();
     }
   }
}