Home ProgrammingC# Visual C#: listen – utility for checking TCP sockets

Visual C#: listen – utility for checking TCP sockets

by Kliment Andreev
0 comment
Reading Time: 1 minute

I needed an utility that acts as a TCP server on a random port and if a client telnets to that port, the server will have to return something to that client. There are probably a tons of utilities like this, ranging from PowerShell scripts to nc (netcat), but I’ve decided to make one in Visual C# 2015 using .NET 3.5.
My utility is based on this MSDN article with slight modifications.
So, launch the utility with the port in mind, e.g. listen -p 25000 and the program will wait for someone to connect to that port. Once you do telnet 25000, you’ll be greeted with a banned from the utility. Type any key and you’ll get a response with the upper version of that key. Type q and it will terminate the session. The utility is useful to check TCP ports across networks. If you try to run the utility on a port that’s already in use, e.g. port 80 on a web server, the utility will throw an exception. Also, if you try to listen on port 443 and then go to https://server_ip, the utility will also throw an exception. Nevertheless, this means you have a connection.
So, here is the source and link to github repo.
An image of listen in action.

using System;
using System.Net;
using System.Net.Sockets;

namespace listen
{
    class Program
    {
        static void usage()
        {
            Console.WriteLine(new string('=', 42));
            Console.WriteLine("listen v1.0, Kliment Andreev 2016");
            Console.WriteLine("Usage: listen -p <port>");
            Console.WriteLine("Opens a TCP socket on a given port");
            Console.WriteLine("CTRL + C to exit or type q from the client");
            Console.WriteLine(new string('=', 42));
        }

        static void Main(string[] args)
        {
            if ((args.Length == 0) || (args.Length != 2))
            {
                usage();
                return;
            }

            if (args[0]!="-p")
            {
                Console.WriteLine("Unknown switch {0}", args[0]);
                usage();
                return;
            }

            int intTemp;
            bool bisNumeric = int.TryParse(args[1], out intTemp);
            if (!bisNumeric)
            {
                usage();
                return;
            }
           
            TcpListener server = null;
            try
            {
                // Set the TcpListener on port intTemp.
                Int32 intPort = intTemp;
                IPAddress localAddr = IPAddress.Parse("0.0.0.0");
                // TcpListener server = new TcpListener(port);
                server = new TcpListener(localAddr, intPort);
                // Start listening for client requests.
                server.Start();
                // Buffer for reading data
                Byte[] bytes = new Byte[4096];
                
                String data = null;
                // Enter the listening loop.
                while (true)
                {
                    usage();
                    Console.WriteLine("I am listening on {0}... ", intPort);
                    // Perform a blocking call to accept requests.
                    // You could also user server.AcceptSocket() here.
                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("=====> Connected to {0}", client.Client.RemoteEndPoint);
                    data = null;
                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream();
                    byte[] banner = System.Text.Encoding.ASCII.GetBytes(
                        "listen v1.0, Kliment Andreev 2016\r\nType q to exit\r\n" 
                        + "Connected to " + client.Client.LocalEndPoint + "\r\n");
                    stream.Write(banner, 0, banner.Length);
                    int intLoop;
                    // Loop to receive all the data sent by the client.
                    while ((intLoop = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        // Translate data bytes to a ASCII string.
                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, intLoop);
                        Console.WriteLine("Received: {0} from {1}", data, client.Client.RemoteEndPoint);
                        // Process the data sent by the client.
                        data = data.ToUpper();
                        if (data == "Q") return;
                        byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                        // Send back a response.                        
                        stream.Write(msg, 0, msg.Length);                        
                        Console.WriteLine("Sent: {0} to {1}", data, client.Client.RemoteEndPoint);
                    }

                    // Shutdown and end connection
                    client.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                //server.Stop();
            }
        }
    }
}
0 comment
0

Related Articles

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More