Simple Socket Server in Bash?
$ nc -k -l 4444 > filename.out
see nc(1)
Looking for simple socket server application that could be used to test socket applications?
For a simple TCP server you could use netcat
:
Ncat is a feature-packed networking utility which reads and writes data across networks from the command line. Ncat was written for the Nmap Project and is the culmination of the currently splintered family of Netcat incarnations. It is designed to be a reliable back-end tool to instantly provide network connectivity to other applications and users. Ncat will not only work with IPv4 and IPv6 but provides the user with a virtually limitless number of potential uses.
It can provide bidirectional communication through TCP/UDP. There's even some HTTP server implementations using only netcat.
It'd be as easy as:
$ nc -l 0.0.0.0 9999
And voilá, you're now listening to the port 9999 in all interfaces and can connect from your device.
To test the communications you can also use netcat
. In another bash session, try this:
$ nc 127.0.0.1 9999
Easy and available on all distros :)
Opening a socket to see output with bash
If you version of bash
supports networking
#!/bin/bash
set -u
host="$1"
port="$2"
path="$3"
exec 3<>/dev/tcp/$host/$port
printf '%s\r\n' "$path" >&3
cat <&3
if you are hitting a HTTP server the probably you have to pass the GET
requests as path
, something like
script example.com 80 $'GET / HTTP/1.1\r\nConnection: close\r\n'
How to make an Echo server with Bash?
If you use ncat instead of nc your command line works fine with multiple connections but (as you pointed out) without -p.
ncat -l 2000 -k -c 'xargs -n1 echo'
ncat is available at http://nmap.org/ncat/.
P.S. with the original the Hobbit's netcat (nc) the -c flag is not supported.
Update: -k (--keep-open) is now required to handle multiple connections.
Want to create a TCP server
If your nc
supports it, you can use the '-e' option: put your loop in a new file handle_client.sh
, then start the server with
nc -l localhost 3000 -e ./handle_client.sh
C++ UNIX Help - simple TCP server socket connection
You problem is here:
else if(s.st_mode & S_IFREG)
{
int fd = open(path, O_RDONLY);
if(fd < 0) { perror("open"); exit(EXIT_FAILURE); }
read(fd, buffer, strlen(buffer)); << Change strlen(buffer)
strcat(buffer, "\n");
if(write(connSock, buffer, strlen(buffer)) < 0)
{ perror("write"); exit(EXIT_FAILURE); }
close(fd);
}
strlen(buffer)
can be any value, because you are initializing buffer to 1024 bytes. The memory area is possibly being filled full of zeroes. strlen(buffer)
would then be returning 0, because the first character is a null byte. Nothing is being written into the buffer because read
will end up writing zero bytes.
Related Topics
How to Create a Link to a Directory
Why Is the Page Size of Linux (X86) 4 Kb, How Is That Calculated
How to Trim White Space from a Variable in Awk
Git Status Ignore Line Endings/Identical Files/Windows & Linux Environment/Dropbox/Meld
Converting Jiffies to Milli Seconds
Does Gcc Have Any Options to Add Version Info in Elf Binary File
Using Interrupt 0X80 on 64-Bit Linux
Command to Change the Default Home Directory of a User
Difference Between Posix Aio and Libaio on Linux
Splitting a File in Linux Based on Content
Restarting Cron After Changing Crontab File
How to Run a Windows Executable from Wsl (Ubuntu) Bash
How to Edit /Etc/Sudoers from a Script
How to Undo Strip - I.E. Add Symbols Back to Stripped Binary