2

I'm running a TCP server in c#. The program seems to run and hold for new clients (it stop on TcpClient client = this.tcpListener.AcceptTcpClient();) waiting for new connections. However if I check the network (using the netstat command) the server is not listening, wich means is not running. I also tryied with different ports, but I guess than port 80 should be good for testing (I also tried with other ports and none of them worked). What is wrong in my code? Maybe the OS is blocking the server?

namespace TCPServer
{
    class TestClass
    {
        static void Main(string[] args)
        {
            Server TCPServer = new Server();
            // Display the number of command line arguments:
            System.Console.WriteLine(args.Length);
        }
    }

    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();
            System.Console.WriteLine("Server started");
        }

        //starts the tcp listener and accept connections
        private void ListenForClients()
        {
            this.tcpListener.Start();
            System.Console.WriteLine("Listener started");

            while (true)
            {
                System.Console.WriteLine("Accepting Clients");
                //blocks until a client has connected to the server
                TcpClient client = this.tcpListener.AcceptTcpClient();
                System.Console.WriteLine("Client connected");

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

        //Read the data from the client
        private void HandleClientComm(object client)
        {
            TcpClient tcpClient = (TcpClient)client; //start the client
            NetworkStream clientStream = tcpClient.GetStream(); //get the stream of data for network access

            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) //if we receive 0 bytes
                {
                    //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));

                //Reply
                byte[] buffer = encoder.GetBytes("Hello Client!");
                clientStream.Write(buffer, 0, buffer.Length);
                clientStream.Flush();
            }

            tcpClient.Close();
        }

    }
}

Update: I configure the app to get a firewall exemption. Im running in windows7. I also checked with the port 3000 and nothing listening on that port. I use the netstat output to determine if its listening or not.

Kimmax
  • 1,658
  • 21
  • 34
nabrugir
  • 1,839
  • 2
  • 22
  • 38
  • See this my sample code (.NET45): https://github.com/gsscoder/owinhttplistener/blob/master/src/Owin.Listener/OwinHttpListener.cs – jay Mar 28 '13 at 20:19
  • Or this one -not mine- (.NET40): https://github.com/markrendle/Flux/blob/master/Flux/Server.cs – jay Mar 28 '13 at 20:20
  • I checked both examples but I can't find what's wrong in my code. I tried to run the second example but, what's the class instance (var instance = new Instance(socket, _app)? I cant build and run the example to know if its my code or the os that is blocking. – nabrugir Mar 28 '13 at 20:42
  • 80 is a terrible port to default to, as it's commonly used by other applications. Did you configure your app to get a firewall exemption? You're not running this in Win8 Metro, are you? – EricLaw Mar 28 '13 at 20:56
  • How are you determining that the server isn't running? Is _nothing_ listening on port 80 in netstat's output? – 500 - Internal Server Error Mar 28 '13 at 21:02
  • use .net trace listener – NickD Mar 28 '13 at 21:15
  • yes, I configure the app to get a firewall exemption. Im running in windows7. I also checked with the port 3000 and nothing listening on that port. I use the netstat output to determine if its listening or not. – nabrugir Mar 28 '13 at 21:18
  • remember in windows port 80 is owned by the system (http.sys) – pm100 Mar 28 '13 at 21:48
  • How do you actually know that the server is not accepting clients? Do you have a corresponding client application that attempts to connect to the server that is unable to do so? – Jesse Carter Mar 19 '14 at 13:54
  • The code runs on my machine (if I change the port to an unused one). Of course when the port _is_ in use, an exception will be thrown on the listener thread, with no handlers to catch it. If I were you I'd move the `Start()` to the main thread, so you can handle that specific issue there. – C.Evenhuis Mar 19 '14 at 14:56

0 Answers0