0

I have been asked to create a TCPListener console app to monitor incoming data in a specific port. I have searched a while and i have the following code which i run through LINQPad:

void Main()
{
    IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

    Console.WriteLine("Starting TCP listener...");

    TcpListener listener = new TcpListener(ipAddress, 1005);
    try
    {

        listener.Start();
        while (true)
        {
            Socket client = listener.AcceptSocket();
            client.ReceiveTimeout.Dump();
            Console.WriteLine("Connection accepted.");

            var childSocketThread = new Thread(() =>
            {
                Byte[] bytes = new Byte[256];
                String data = null;
                int i;
                while((i = client.Receive(bytes))!=0 && data != "55AA") 
                {   
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                }
                Console.WriteLine("connection closing");
                client.Close();

                //throw new Exception();
            });
            childSocketThread.Start();
        }

        listener.Stop();
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: " + e.StackTrace);
        Console.ReadLine();
    }

}

This is actually a slightly modified code of a millions of examples in Google. My problem is that being in LINQPad i usually press the run button and then open a command prompt and telnet to 127.0.0.1 port 1005 and then i write something all good.

If i press the stop button in LINQPad and then press Run again, i end up with the following error:

Error: at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.Bind(EndPoint localEP) at System.Net.Sockets.TcpListener.Start(Int32 backlog) at System.Net.Sockets.TcpListener.Start() at UserQuery.Main() in c:\Users\giannis\AppData\Local\Temp\LINQPad_jncifzyi\query_lgowqr.cs:line 40

Now, i understand that you may have only one client listening or using a specific port, but my issue is that 1st of all i want to play with LINQPad before going to the actual solution. I know that i could make the TCPListener variable a global and just handle it when exiting the program. But what if it crashes or something? Will i end up with not being able to connect to the port until log off log on?

3 Answers 3

1

Probably, the GC has not cleaned up the listener when you restart the program in LINQPad. In a real program all TCP ports are cleaned up when the process exits. Restart LINQPad and you'll find the port to be free again.

I don't see why using a Visual Studio project would be significantly more burdensome when it comes to quickly testing code. Just press F5. The dumping function is not there, though.

6
  • To be honest, while waiting for an answer here i did exactly that and started a project, and it seems that all is ok :-). But usually when i want to just play around and not start developing right away, LINQPad is my friend. Maybe not in that case though. Commented Oct 24, 2014 at 15:34
  • I also just checked and indeed i do not need to handle the closing of the application in order to stop the listener. Commented Oct 24, 2014 at 15:35
  • You should wrap resources in using anyway. I suspect that will rescue you here as well.
    – usr
    Commented Oct 24, 2014 at 15:44
  • i tried, but TCPListener doesn't seem to implement IDisposable, so using seems to be out of the question Commented Oct 24, 2014 at 15:46
  • I am marking this as an answer, as it seems i shouldn't be using LINQPad for this, and vs is coping pretty well. Thanks for your time. Commented Oct 24, 2014 at 15:48
0

It is important to properly clean up your TcpListener, but you could add this before your Try statement: listener.ExclusiveAddressUse = false;

Reference: http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.exclusiveaddressuse%28v=vs.110%29.aspx

0
0

My problem was, that LINQPad keeps the process running after you cancel it.

Either use "Cancel All Threads and Reset" menu option (ctrl+shift+F5) or manually kill all "LINQPad.UserQuery.exe" processes in task manager.

The port should be free again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.