Listening on specific port number using netcat to receive http POST messages

The nc (or netcat) utility is used for just about anything under the sun involving TCP, UDP, or UNIX-domain sockets. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6. Unlike telnet(1), nc scripts nicely, and separates error messages onto standard error instead of sending them to standard output, as telnet(1) does with some.

Common uses include:
· simple TCP proxies
· shell-script based HTTP clients and servers
· network daemon testing
· a SOCKS or HTTP ProxyCommand for ssh(1)
· and much, much more

Ref – from man page of nc

Now, here we will see how to debug whether we are receiving HTTP POST messages on specific port, if we are trying to develop some application which indents to send some HTTP POST messages and we want to see what the server is receiving at its end.

For this usecase, we will try to listen on one socket, and then from another terminal we will try to sent HTTP POST message to the port number as we used in nc as,

1. open terminal and type command,

 nc -l 8080 

Here, 8080 is our test port, you can use any port or the port number which you are using in your application.

Now open another terminal, and type following command as, [ NOTE : This is just our test application/bash script/command, you may run your exact application which you are debugging to understand what it is sending to the server ]

curl -k \
        -H "Content-Type:text/xml; charset=UTF-8" \
        -X POST \
        -d '<?xml version="1.0" encoding="UTF-8"?>
        </address>' http://localhost:8080

Here, since we have started nc command on same PC, we are using localhost, if you have nc command started on another PC on which you want to see what you receive on that PC’s port, you need to use IP address as, http://IP_ADDRESS:PORT_NUMBER

SHUFFLED :   send data using http POST from client and receive response from server using netcat over specific port

Now, with this you can see, exactly what you are receiving at the first terminal where we started netcat,

$ nc -l 8080 
Host: localhost:8080
User-Agent: curl/7.47.0
Accept: */*
Content-Type:text/xml; charset=UTF-8
Content-Length: 181

<?xml version="1.0" encoding="UTF-8"?>

So, this nc command listening on port 8080 showed that port received at HTTP POST message with xml as Content Type and also exact xml which client/app sent to server.

Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply