Creating a socket and binding it to an address

Socket

Sockets are a way to enable inter-process communication between programs running on a machine, or between programs running on separate machines. Network sockets on both clients and servers are represented by socket addresses. A socket address is a unique combination of a transport protocol like the Transmission Control Protocol (TCP) or User Datagram Protocol (UDP), an IP address, and a port number. e.g. 10.0.113.135:443 is an address that possibly is a web server listening on port 443. Port 443 is the HTTPS port.

Creating a socket

We can create sockets programmatically or using a linux utility e.g. socat (sudo apt install socat) . Following is an example

socat TCP4-LISTEN:8081,fork /dev/null &

this socket listens on port 8081. If the port 8081 is already in use, this command may fail

Let’s interpret this command

  • socat is the utility program
  • TCP4-LISTEN:8081: specifies that the socket should use IPv4, TCP protocol, and listen on port 8081
  • fork: instructs socat to fork a new process for each incoming connection
  • dev/null: connects the incoming data to /dev/null, which is a special file that discards all data written to it. Effectively, this means incoming data is ignored
  • &: Runs the command in the background

Connecting to the socket

We will use netcat available as a binary nc (sudo apt install netcat-openbsd) to test the TCP socket we just created. Following is an example to connect to the IPv4 socket over the loopback address

nc -4 -vz 127.0.0.1 8080
  • the 4 specifies IPv4 connection
  • v is for verbose
  • z means don’t send any data to the server

Binding socket to an IP address

The socat command that we ran above binds to 0.0.0.0 by default. 0.0.0.0 means the socket listens on all IPv4 interfaces e.g. loopback, Wi-Fi, Ethernet, etc.

  • Loopback IP: 127.0.0.1
  • If you are connected to Wi-Fi, then Wi-Fi adapter(e.g. wlan0) could have an address e.g. 192.168.1.10.
  • If you are connected to Ethernet using a cable, your Ethernet adapter(e.g. eth0) could have an address 192.168.0.110

What does binding mean?

Binding to an address means that the socket will only accept connections directed to that specific address on the given network interface

e.g. our socket by default binds to all IPv4 interfaces. This means it can accept connections directed to

  • Loopback i.e. a client can connect to 127.0.0.1 from the same machine
  • 192.168.1.10 i.e. a client on the same Wi-Fi network as yours can connect to the socket by specifying this IP address
  • 192.168.0.110 i.e. a client on the same network as your Ethernet can connect to the socket by specifying this IP address

Binding to a specific address

Following is an example to create a socket that binds to a specific address

socat TCP4-LISTEN:8082,bind=192.168.2.20,fork /dev/null

This command binds the socket to the address 192.168.2.20. If the bind address is associated to your Wi-Fi adapter, then this socket can listen only on the Wi-Fi interface i.e. you can connect to this socket by specifying this IP address which is possible only if you on the same Wi-Fi network

Leave a Reply