<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>tcp &#8211; Blog of Kliment Andreev &#8211; A place so I won&#039;t forget things</title>
	<atom:link href="https://blog.andreev.it/tag/tcp/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.andreev.it</link>
	<description></description>
	<lastBuildDate>Sun, 11 Oct 2020 14:21:26 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>Visual C#: listen &#8211; utility for checking TCP sockets</title>
		<link>https://blog.andreev.it/2016/12/102-visual-c-listen-utility-checking-tcp-sockets/</link>
					<comments>https://blog.andreev.it/2016/12/102-visual-c-listen-utility-checking-tcp-sockets/#respond</comments>
		
		<dc:creator><![CDATA[Kliment Andreev]]></dc:creator>
		<pubDate>Sat, 10 Dec 2016 22:33:11 +0000</pubDate>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[port listener]]></category>
		<category><![CDATA[tcp]]></category>
		<category><![CDATA[Visual C#]]></category>
		<guid isPermaLink="false">http://blog.iandreev.com/?p=2964</guid>

					<description><![CDATA[I needed an utility that acts as a TCP server on a random port&#8230;]]></description>
										<content:encoded><![CDATA[<div id="bsf_rt_marker"></div><p>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&#8217;ve decided to make one in Visual C# 2015 using .NET 3.5.<br />
My utility is based on this <a href="https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(v=vs.110).aspx" target="_blank" rel="noopener noreferrer">MSDN </a>article with slight modifications.<br />
So, launch the utility with the port in mind, e.g. <strong>listen -p 25000</strong> and the program will wait for someone to connect to that port. Once you do telnet <server_ip> 25000, you&#8217;ll be greeted with a banned from the utility. Type any key and you&#8217;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&#8217;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.<br />
So, here is the source and <a href="https://github.com/klimenta/listen" target="_blank" rel="noopener noreferrer">link </a>to github repo.<br />
An image of listen in action.<br />
<a href="https://blog.andreev.it/wp-content/uploads/2016/12/P081-01.png"><img fetchpriority="high" decoding="async" src="https://blog.andreev.it/wp-content/uploads/2016/12/P081-01.png" alt="" width="621" height="383" class="aligncenter size-full wp-image-7626" srcset="https://blog.andreev.it/wp-content/uploads/2016/12/P081-01.png 621w, https://blog.andreev.it/wp-content/uploads/2016/12/P081-01-300x185.png 300w, https://blog.andreev.it/wp-content/uploads/2016/12/P081-01-585x361.png 585w" sizes="(max-width: 621px) 100vw, 621px" /></a></p>
<pre class="brush: cpp; title: ; notranslate">
using System;
using System.Net;
using System.Net.Sockets;

namespace listen
{
    class Program
    {
        static void usage()
        {
            Console.WriteLine(new string('=', 42));
            Console.WriteLine(&quot;listen v1.0, Kliment Andreev 2016&quot;);
            Console.WriteLine(&quot;Usage: listen -p &lt;port&gt;&quot;);
            Console.WriteLine(&quot;Opens a TCP socket on a given port&quot;);
            Console.WriteLine(&quot;CTRL + C to exit or type q from the client&quot;);
            Console.WriteLine(new string('=', 42));
        }

        static void Main(string&#x5B;] args)
        {
            if ((args.Length == 0) || (args.Length != 2))
            {
                usage();
                return;
            }

            if (args&#x5B;0]!=&quot;-p&quot;)
            {
                Console.WriteLine(&quot;Unknown switch {0}&quot;, args&#x5B;0]);
                usage();
                return;
            }

            int intTemp;
            bool bisNumeric = int.TryParse(args&#x5B;1], out intTemp);
            if (!bisNumeric)
            {
                usage();
                return;
            }
           
            TcpListener server = null;
            try
            {
                // Set the TcpListener on port intTemp.
                Int32 intPort = intTemp;
                IPAddress localAddr = IPAddress.Parse(&quot;0.0.0.0&quot;);
                // TcpListener server = new TcpListener(port);
                server = new TcpListener(localAddr, intPort);
                // Start listening for client requests.
                server.Start();
                // Buffer for reading data
                Byte&#x5B;] bytes = new Byte&#x5B;4096];
                
                String data = null;
                // Enter the listening loop.
                while (true)
                {
                    usage();
                    Console.WriteLine(&quot;I am listening on {0}... &quot;, intPort);
                    // Perform a blocking call to accept requests.
                    // You could also user server.AcceptSocket() here.
                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine(&quot;=====&gt; Connected to {0}&quot;, client.Client.RemoteEndPoint);
                    data = null;
                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream();
                    byte&#x5B;] banner = System.Text.Encoding.ASCII.GetBytes(
                        &quot;listen v1.0, Kliment Andreev 2016\r\nType q to exit\r\n&quot; 
                        + &quot;Connected to &quot; + client.Client.LocalEndPoint + &quot;\r\n&quot;);
                    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(&quot;Received: {0} from {1}&quot;, data, client.Client.RemoteEndPoint);
                        // Process the data sent by the client.
                        data = data.ToUpper();
                        if (data == &quot;Q&quot;) return;
                        byte&#x5B;] msg = System.Text.Encoding.ASCII.GetBytes(data);
                        // Send back a response.                        
                        stream.Write(msg, 0, msg.Length);                        
                        Console.WriteLine(&quot;Sent: {0} to {1}&quot;, data, client.Client.RemoteEndPoint);
                    }

                    // Shutdown and end connection
                    client.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(&quot;SocketException: {0}&quot;, e);
            }
            finally
            {
                // Stop listening for new clients.
                //server.Stop();
            }
        }
    }
}
</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.andreev.it/2016/12/102-visual-c-listen-utility-checking-tcp-sockets/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
