Using server-side sockets in datagram mode

In C language:

After creating the socket, you will have to indicate the port on which you will listen for the arrival of client datagrams.

To do this, we will use the bind function by passing it 3 parameters:

We will therefore have to declare a variant of the sockaddr structure corresponding to AF_INET. This is the struct sockaddr_in type. We will start by zeroing the content of this structure via the bzero function of string.h, then we will assign, to the first field named here sin_family, the AF_INET value.

struct sockaddr_in	adr_srv;

bzero(&adr_srv, sizeof(adr_srv));
adr_srv.sin_family = AF_INET;
	

You must then fill in the sin_port field in the structure with the number of its TCP port on which the server is listening and possibly the sin_addr field with the address IPv4. (You can also leave the IPv4 address zero to listen on all interfaces.)

inet_aton("127.0.0.1", &adr_srv.sin_addr);
adr_srv.sin_port = htons(7);
	

Having thus specified the address 127.0.0.1 and the port 7, all that remains is to call the function bind.

bind(fd_srv, (struct sockaddr *) &adr_srv, sizeof(adr_srv));
	

You must then wait, in a while loop, for the arrival of a datagram via the recvfrom function which requires 6 parameters:

The function returns the number of bytes received in the buffer.

struct sockaddr_in adr_cli;
socklen_t adr_cli_len;

adr_cli_len = sizeof(adr_cli);
n = recvfrom(fd_srv, &buffer, sizeof(buffer), MSG_WAITALL, (struct sockaddr *) &adr_cli, &adr_cli_len);
	

Example:

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>

#include <string.h>		// for the function bzero
#include <arpa/inet.h>	// for the function inet_addr

int main()
{
	int fd_srv;
	struct sockaddr_in adr_srv, adr_cli;
	socklen_t adr_cli_len;
	int err;

	int n;
	char buffer[1024];

	fd_srv = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (fd_srv<0) {
		printf("Socket creation error!\n");
		exit(1);
	}

	bzero(&addr, sizeof(adr_srv));
	addr.sin_family = AF_INET;
	err = inet_aton("127.0.0.1", &adr_srv.sin_addr);
	if (err == 0) {
		printf("Invalid IPv4 address!\n");
		exit(1);
	}
	addr.sin_port = htons(8080);
	err = bind(fd_srv, (struct sockaddr *) &adr_srv, sizeof(adr_srv));
	if (err != 0) {
		printf("Server port access error!\n");
		exit(1);
	}

	while(1) {
		adr_cli_len = sizeof(adr_cli);
		n = recvfrom(fd_srv, &buffer, sizeof(buffer), MSG_WAITALL, (struct sockaddr *) &adr_cli, &adr_cli_len);
		printf("Message of size %d bytes received from %s:%d", n, inet_ntoa(adr_cli.sin_addr), ntohs(adr_cli.sin_port));
	}
	return 0;
}
	

PHP and Python:

In languages like PHP or Python, there are libraries interfacing with system functions.

PHP

The example code written in C becomes in PHP:

$sock_srv = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($sock_srv== false) die("Socket creation error!\n");
$err = socket_bind($sock_srv, '127.0.0.1', 8080);
if ($err == false) die("Server port access error!\n");
echo "Server launched!\n";
while (true) {
	$from = '';
	$port = 0;
	socket_recvfrom($sock_srv, $buf, 1024, MSG_WAITALL, $from, $port);
	echo "message of size ",strlen($buf)," bytes received from ",$from,":",$port,"\n";
}
	

Python

The example code written in C becomes in Python:

import socket

try:
	sock_srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock_srv.bind(('127.0.0.1', 8080))
except socket.error:
	print("Server launch error!")
	exit()
print("Server launched!")
while True :
	try :
		buffer, adresse = sock_srv.recvfrom(1024)
		ip, port = adresse
		print("message of size",len(buffer),"bytes received from",ip+":"+str(port))
	except socket.error :
		print("Error reading a datagram!")
		exit()