Send and Receive (Transfer) integer over socket using C program

Following the simple example which shows how we can send and receive an integer over a socket. This program is a simple c client program which sends integer over sockets and receives a reply which is also an integer ( Normally useful in example like to send some command number and await for integer response to that command )

$ vim example_client.c
#include <stdio.h>
#include <stdlib.h> 
#include <string.h> 
#include <sys/socket.h> 
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>

// first argument to this program is IP address
// Second argument is Port

void send_receive_integer_over_socket(int sockfd) { 
	int int_to_send = 879612345, int_to_receive; //integer_to_send is something like command

	write(sockfd, &int_to_send, sizeof(int_to_send)); 

	read(sockfd, &int_to_receive, sizeof(int_to_receive)); 

	//int_to_receive = ntohl(int_to_receive);
	printf("Integer Received from Server : %d\n", int_to_receive); 
} 

int main(int argc, char **argv) { 
	int sockfd, port; 
	struct sockaddr_in servaddr, cli; 

	if (argc < 3) {
		printf("Some of the command line arguments missing");
		return -1;
	}

	sockfd = socket(AF_INET, SOCK_STREAM, 0); 
	if (sockfd == -1) { 
		printf("socket creation failed...\n"); 
		exit(errno); 
	}	
	printf("Socket successfully created..\n"); 
	bzero(&amp;amp;servaddr, sizeof(servaddr)); 

	servaddr.sin_family = AF_INET; 
	servaddr.sin_addr.s_addr = inet_addr(argv[1]); 
	port = atoi(argv[2]);
	servaddr.sin_port = htons(port); 

	if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) { 
		printf("connection with the server failed...\n"); 
		exit(errno);
	} 
	printf("connected to the server..\n"); 

	send_receive_integer_over_socket(sockfd); 

	close(sockfd);
	return 0;
} 

Now compile the program as,

$ gcc -o simple_client simple_client.c

Now, lets create a simple server program as,

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define MAXBUF		1024
#define QUEUE_NO 5

int main(int argc, char **argv) {
	int sockfd, clientfd, port, int_to_receive, int_to_send=8796;
	struct sockaddr_in s_addr, c_addr;
	char buffer[MAXBUF];
	socklen_t socklen = (socklen_t)sizeof(struct sockaddr_in);

	if (argc &amp;lt; 2) {
		printf("Some of the command line arguments missing");
		return -1;
	}

	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd == -1) {
		printf("socket creation failed...\n");
		exit(errno);
	}
        printf("Socket successfully created..\n");

	bzero(&s_addr, sizeof(s_addr));
	s_addr.sin_family = AF_INET;
	port = atoi(argv[1]);
	s_addr.sin_port = htons(port);
	s_addr.sin_addr.s_addr = INADDR_ANY;

	if (bind(sockfd, (struct sockaddr*)&s_addr, sizeof(s_addr)) != 0){
		printf("socket creation failed...\n");
		exit(errno);
	}

	if (listen(sockfd, QUEUE_NO) != 0) {
		printf("socket listen failed...\n");
		exit(errno);
	}

	while (1) {

		clientfd = accept(sockfd, (struct sockaddr*)&c_addr, &socklen);
		printf("%s:%d connected\n", inet_ntoa(c_addr.sin_addr), ntohs(c_addr.sin_port));

		read(clientfd, &int_to_receive, sizeof(int_to_receive));
		//int_to_receive = ntohl(int_to_receive);

		write(clientfd, &int_to_send, sizeof(int_to_send));
		printf("Received from client: %d\n", int_to_receive);

		close(clientfd);
	}

	close(sockfd);
	return 0;
}

Compile the server as,

$ gcc -o simple_server simple_server.c

Now, execute the server program with some random port number passed as command line argument one one terminal as, [ Note, we taken port number as 1564 as example, you may use any other number]

$ ./simple_server 1564

This command will start server and await for clients to connect. Now, lets start the client program as we compiled above, [ Here, for client it needs to know server IP which is in our case as, 192.168.42.158 ]

$./simple_client 192.168.42.158 1564
Socket successfully created..
connected to the server..
Integer Received from Server : 8796
$ ./simple_server 1564
Socket successfully created..
192.168.42.158:52120 connected
Received from client: 879612345

Leave a Comment