Utility to open TCP port to listen state

25

7

Is there some basic utility to open a specific network TCP port on my machine?

I need to test how my program deals with ports in the listening state.

Vic

Posted 2012-03-07T12:39:18.733

Reputation: 395

occupy you mean about let an specific TCP port oppened on listen state? – Diogo – 2012-03-07T12:42:19.617

1This question makes no sense. A port is "closed" by default when nothing is listening on it. – Ingmar Hupp – 2012-03-07T12:43:19.577

2This is what I want - some utility that will be listening on the port of my choosing and do nothing. – Vic – 2012-03-07T12:49:40.903

Answers

11

netcat should do what you want. Have it listen on you machine and echo stuff to STDOUT:

nc -4 -k -l -v localhost 1026

when you want it to close when the connection ends, don't use -k

Florenz Kley

Posted 2012-03-07T12:39:18.733

Reputation: 1 453

2It works on Windows??? I tried this command here and it was not recognized. – Diogo – 2012-03-07T13:05:47.073

4

I was going to post this as my answer! =p You can download netcat here: http://joncraton.org/files/nc111nt.zip It's a pretty old tool, but I think people failed to realize just how useful it can be.

– cutrightjm – 2012-03-07T13:05:52.737

@Diogo Rocha You know it is a downloaded program, correct? – cutrightjm – 2012-03-07T13:06:22.830

3I didn't know... It would be nice if the answear post the link to download then... – Diogo – 2012-03-07T13:07:40.643

@DiogoRocha - I removed the bogus link to netcat. The official site is at sourceforge. It's part of pretty much every packaging repository out there, so I have not installed in from source for like 10 years in a row. – Florenz Kley – 2012-03-07T13:16:20.653

Sure, no problem. – Diogo – 2012-03-07T13:17:01.070

Looks good, but the antivirus won't allow me to install neither the "regular" nor the "safe" version of nc for windows. – Vic – 2012-03-07T13:17:38.390

@Vic - time to make friends with your admin, then... or maybe time for that Linux or BSD virtual machine where you find more tools for network programming you ever dreamed of :-) – Florenz Kley – 2012-03-07T13:22:08.553

@FlorenzKley If you are saying that my link is bogus, you are mistaken. – cutrightjm – 2012-03-07T13:26:27.423

6

You have

TCP Listen: http://www.allscoop.com/tcp-listen.php

Port Peeker: http://www.linklogger.com/portpeeker.htm

Microsoft's Command-line utility Portqry.exe

nelson.t.cunha

Posted 2012-03-07T12:39:18.733

Reputation: 321

1TCP listen - nice little GUI program without installation! ;-) – hoggar – 2015-07-03T02:53:24.377

1Port Peeker, site no longer exists or is not reachable (Jan 2019) – shawty – 2019-02-01T14:54:21.963

Here you go, InternetArchive's latest snapshot: http://web.archive.org/web/20180616055542/http://www.linklogger.com/portpeeker.htm Some download links are still working.

– nelson.t.cunha – 2019-02-04T16:11:31.687

4

Try iperf. There is a version for Windows. You can just run it like iperf -s -p 1234, and it will listen on port 1234. You can then connect to that port from a remote machine by doing something like:

telnet 192.168.1.1 1234

iperf -c 192.168.1.1 1234

portqry -n 192.168.1.1 -e 1234

You would need to obtain iperf.exe or portqry.exe for the last two. iPerf isn't strictly designed for this task, but it's great for troubleshooting connectivity, bandwidth availability, stress testing links, etc.

reggaethecat

Posted 2012-03-07T12:39:18.733

Reputation: 41

2

It looks like this utility will do exactly what you want, even displaying the received data if you like: http://www.drk.com.ar/builder.php

It has a GUI rather than just a command line, an advantage for some.

Jon

Posted 2012-03-07T12:39:18.733

Reputation: 21

1

The netpipes tools faucet and hose have always served me well, simplifying stdin and stdout for my programs to use over the network.

Similar to netcat.

Ubuntu description:

The netpipes package makes TCP/IP streams usable in shell scripts. It can also simplify client/server code by allowing the programmer to skip all the tedious programming bits related to sockets and concentrate on writing a filter, or other service.

EXAMPLES
       This creates a TCP-IP socket on the local machine bound to port 3000.

       example$ faucet 3000 --out --verbose tar -cf - .

       Every  time  some process (from any machine) attempts to connect to port 3000 on this machine the faucet program will fork(2) a process and the child
       will exec(2) a

       tar -cf - .

Marcos

Posted 2012-03-07T12:39:18.733

Reputation: 1 051

Thanks, but I need it for Windows... – Vic – 2012-03-07T13:49:06.740

Not even Cygwin? – Marcos – 2012-03-07T21:19:18.737

0

TCP Listen is the BEST answer IMHO. I looked at and liked TCP Builder, but you NEED admin rights to run that app, you do NOT need them with TCP listener, and you also need to install TCP Builder, or unzip and copy a bunch of files, whereas TCP Listener is 1 EXE, nothing more.

TCP Listener also did not need admin rights, and when I AV scanned it nothing said it was malicious. Builder had 1 AV on Virustotal say it was bad, but it turned out to be a false positive (I hope) :)

While I got Builder to run sans admin rights, it could not hook the socket while Listener could. Once I did all my testing, I could just delete the 1 file of Listener and all was like before.

Netcat would have been nice, but I did not find a version that would work with 2012 or later server. So, to fully test if the network firewall and local firewalls allow specific TCP ports to connect, TCP listener seems like the best tool for this job.

Enjoy!

David Howard

Posted 2012-03-07T12:39:18.733

Reputation: 53

0

I like netcat on Windows, but downloading and installing stuff from the Internet is not always possible. Maybe you are setting up a production server, and you want to test your firewall rules before (and without) installing anything.

Most (all?) Windows servers have a JScript.net compiler. You can write plain old Windows batch file that is also a valid JScript.net program, a polyglot program.

tl;dr;

The idea is to find the jsc.exe executable on your system:

for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
   set "jsc=%%v"
)

And use it to compile the batch file.

"!jsc!" /nologo /out:"%APPDATA%\listener.exe" "%~dpsfnx0"

The batch file contains basic JScript.Net code that creates a synchronous socket, listen on it, accept the connection and drop everything that comes to it :

listener.Bind(localEndPoint);  
listener.Listen(10);  

// Start listening for connections.  
while (true) {  
    var data:byte[] = new byte[1024];

    Console.WriteLine("Waiting for a TCP connection on {0}:{1}...", ipAddress, port);  
    var handler = listener.Accept();  

    Console.WriteLine("Connected to {0}", handler.RemoteEndPoint);  

    try {
        // An incoming connection needs to be processed.  
        while (handler.Receive(data) > 0);  
    } finally {
        Console.WriteLine("Disconected\n");
    }

    handler.Shutdown(SocketShutdown.Both);  
    handler.Close();  
}

The compiled program will be saved as %APPDATA%\listener.exe. It can run on its own, copied on an server, but compiling from the polyglot batch file will work regardless of any security hurdles in your way.

ixe013

Posted 2012-03-07T12:39:18.733

Reputation: 738

0

You can use netcat's Windows version:

nc -l -v localhost -p 7

netawater

Posted 2012-03-07T12:39:18.733

Reputation: 183

Note: some malware or firewall security software might flag this file as a virus. Due to what this program can do it may have been included in malware programs but it is not malware itself and as of 2017-10-04 this file is scanned as clean by VirusTotal (marked as "riskware" by only one scanner). See the Community note on the url and the File scan itself

– Mokubai – 2017-10-04T12:44:02.810

-1

This is the perfect use for Wireshark, a packet and protocol analyzer which sits in-between the Windows/Linux networking stack.

It will allow you to view all TCP/UDP packets that are received by your entire machine, regardless of the port. You can also tell the program to filter out only packets sent across a certain port for further analysis. The advantage to Wireshark is that it provides very detailed messages for each packet - source, destination, port, MAC addresses, data, dates, checksums, etc. Very useful (and free!) tool.

Breakthrough

Posted 2012-03-07T12:39:18.733

Reputation: 32 927

7humor me - since when when does Wireshark actually behave like a service listening on a port? It passively records the traffic. – Florenz Kley – 2012-03-07T13:20:45.047

@FlorenzKley you're right, it doesn't... I interpreted his question as how to view the network traffic on a certain port though, since wouldn't it be trivial for the O.P. to just run two copies of his program at once on the same port (to see what happens if another program is already listening on the port)? – Breakthrough – 2012-03-07T13:24:19.940

-3

TCPView from the Sysinternals toolkit provides a very nice overview.

Patrik Mihalčin

Posted 2012-03-07T12:39:18.733

Reputation: 95

Does this tool allow the opening of TCP port on the machine it is running? – Pimp Juice IT – 2017-08-21T11:19:22.003

I'm not aware of that.. it's just a view as the name suggests – Patrik Mihalčin – 2017-08-21T11:21:08.727

But the question is/was: "Is there some basic utility to *open* a specific network TCP port on my machine?". In TCPView you can close a connection, but that is the only thing that can be changed in the system. – Peter Mortensen – 2019-04-06T20:58:44.520