How to create a TCP listener?

Introduction: I have created a bash function that is able to check whether a port is available and increments it by 1 if false until a certain maximum port number. E.g., if port 500 is unavailable then the availability of 501 will be checked until 550.

Aim: In order to test this bash function I need to create a range of ports that are in LISTEN state.

Attempts: On Windows it is possible to create a LISTEN port using these PowerShell commands:

PS C:Usersu> netstat -nat | grep 1234
PS C:Usersu> $listener = [System.Net.Sockets.TcpListener]1234
PS C:Usersu> $listener.Start();
PS C:Usersu> netstat -nat | grep 1234
TCP               LISTENING       InHost
PS C:Usersu> $listener.Stop();
PS C:Usersu> netstat -nat | grep 1234
PS C:Usersu>

Based on this I was trying to think about a command that could do the same on CentOS, but I do not know why and I started to Google without finding a solution that solves this issue.

Expected answer: I will accept and upvote the answer that contains a command that is able to create a LISTEN port and once the command has been run the port should stay in LISTEN state, i.e.:

[user@host ~]$ ss -nat | grep 500
LISTEN     0      128                       *:500                       *:*
Asked By: 030


You could use nc -l as a method to do what you are looking for. Some implementations of nc have a -L option which allows the connections to persist.

If you only need them for a little while you could open this command in a for loop and have a bunch of ports opened that way.

If you need these opened longer you can use one of the super servers to create a daemon.

Answered By: Dana Marble

Listen using netcat.

# nc -l 5555

Check using ss

# ss -nat|grep 5555
LISTEN     0      1                         *:5555                     *:*
Answered By: steve

you can create a port listener using Netcat .

root@ubuntu:~# nc -l 5000

you can also check if port is open or not using netstat command .

root@vm-ubuntu:~# netstat -tulpen | grep nc
tcp        0      0   *               LISTEN      0          710327      17533/nc

you can also check with nc :

Netcat Server listener :

nc -l localhost 5000

Netcat Client :

root@vm-ubuntu:~# nc -v localhost 5000
Connection to localhost 5000 port [tcp/*] succeeded!

if port is not open

root@vm-ubuntu:~# nc -v localhost 5000
nc: connect to localhost port 5000 (tcp) failed: Connection refused
Answered By: sameh Ammar

Note that Debian’s netcat package has a different implementation where (at least) you need to provide the port via the -p option and the -k option doesn’t work. You might run into this issue using Docker.

sudo apt install -y netcat

nc -l -p 1337

You may consider using openbsd-netcat instead where the -k option works.

Answered By: thisismydesign

Below python code is helpful to open a dummy port

'''    Simple socket server using threads
import socket
import sys
HOST = ''   # Symbolic name, meaning all available interfaces
PORT = 5500 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
    s.bind((HOST, PORT))
except socket.error as msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
print 'Socket bind complete'
#Start listening on socket
print 'Socket now listening'
#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])

Save the file and run it with python command as shown below

~]# python
Socket created
Socket bind complete
Socket now listening

Then you can verify from required machine.

Answered By: Rohit

Below command was immediately exiting without listening on port on RHEL 6
nc -l localhost 5000

For me below command worked where netcat started listening on port.
nc -lk localhost 5000

Answered By: Shadaksharayya H A
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.