Creating arbitrary sockets
For operations such as file transfer and secure shell, there are prebuilt tools such as ftp and ssh
. We can also write custom scripts as network services. The next recipe demonstrates how to create simple network sockets and use them for communication.
Getting ready
The netcat
or nc
command will create network sockets to transfer data over a TCP/IP network. We need two sockets: one listens for connections and the other connects to the listener.
How to do it...
- Set up the listening socket using the following command:
nc -l 1234
This will create a listening socket on port 1234
on the local machine.
- Connect to the socket using the following command:
nc HOST 1234
If you are running this on the same machine as the listening socket, replace HOST
with localhost, otherwise replace it with the IP address or hostname of the machine.
- Type something and press Enter on the terminal where you performed step 2. The message will appear on the terminal where you performed...