Using TCP Listener In C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RealZombie
    New Member
    • Mar 2008
    • 2

    Using TCP Listener In C#

    I am working to write a client server program, that relies on threads.
    because of the way the program works the clients that connect to the server connect synchronously, but when I try to reopen the TCP Listener after using the close command on the server. Is there a way to force the port to empty when i close it, or a different way of calling it i guess. I have tried all I think, if I'm missing something obvious id be glad of the help (id rather not have to use asynchronous sockets if i don't have to.)

    Code:
    class ThreadedTcpSrvr
        {
            private TcpListener client = new TcpListener(9050);
                    
            public ThreadedTcpSrvr(string yesno)
            {
                client.Start(10);
    
                if (yesno == "no")
                {
                    Console.WriteLine("Waiting for clients...");
                    
                    while (!client.Pending())
                    {
                        Thread.Sleep(1000);
                    }
                    
                    ConnectionThread newconnection = new ConnectionThread();
                    newconnection.threadListener = this.client;
                    Thread newthread = new Thread(new ThreadStart(newconnection.HandleConnection));
                    newthread.Start();
                    
                }
                    else if (yesno == "yes")
                    {
                        Console.WriteLine("thread aborted");
                        client.Stop();
                    }
            }
    
            public static void StartServer()
            {
                ConnectionThread.yesno = "no";
                ThreadedTcpSrvr server = new ThreadedTcpSrvr(ConnectionThread.yesno);
            }
            public static void Main()
           {
                Console.WriteLine("Hi press enter to continue");
                Console.ReadLine();
                StartServer();
            }
        }
  • RealZombie
    New Member
    • Mar 2008
    • 2

    #2
    Ok my own bad, the close never got called so it never closed :)

    Comment

    Working...