Below you’ll find the code of a simple server-client program in C using UDP sockets for the transmission. Basically the client sends a message to the server, the server converts the message to uppercase and returns it to the client.
If you want to see a simpler program first check this client-server program that only sends a “Hello World” message. The functions and structs are better explained there.
Second, you can also see this same (server converting message to uppercase) example using TCP here.
Server Code
/************* UDP SERVER CODE *******************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
int main(){
int udpSocket, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr, clientAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size, client_addr_size;
int i;
/*Create UDP socket*/
udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
/*Configure settings in address struct*/
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*Bind socket with address struct*/
bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
/*Initialize size variable to be used later on*/
addr_size = sizeof serverStorage;
while(1){
/* Try to receive any incoming UDP datagram. Address and port of
requesting client will be stored on serverStorage variable */
nBytes = recvfrom(udpSocket,buffer,1024,0,(struct sockaddr *)&serverStorage, &addr_size);
/*Convert message received to uppercase*/
for(i=0;i<nBytes-1;i++)
buffer[i] = toupper(buffer[i]);
/*Send uppercase message back to client, using serverStorage as the address*/
sendto(udpSocket,buffer,nBytes,0,(struct sockaddr *)&serverStorage,addr_size);
}
return 0;
}
Client Code
/************* UDP CLIENT CODE *******************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int clientSocket, portNum, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;
/*Create UDP socket*/
clientSocket = socket(PF_INET, SOCK_DGRAM, 0);
/*Configure settings in address struct*/
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*Initialize size variable to be used later on*/
addr_size = sizeof serverAddr;
while(1){
printf("Type a sentence to send to server:\n");
fgets(buffer,1024,stdin);
printf("You typed: %s",buffer);
nBytes = strlen(buffer) + 1;
/*Send message to server*/
sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr,addr_size);
/*Receive message from server*/
nBytes = recvfrom(clientSocket,buffer,1024,0,NULL, NULL);
printf("Received from server: %s\n",buffer);
}
return 0;
}
Nice job on the code man, It’s been very helpful!
In client program,what value should i give for “serverAddr.sin_addr.s_add” if i run these two programs in different machines.
Similar to the TCP code, there’s an unused size variable, here called ‘client_addr_size’. What were you originally intending to do with it?
how to send data greater than MTU size
What modifications are required to add file(e.g. .bin) sharing feature in this code? please help.
what is the different between linux 3.10 and linux 4.0
Some libraries are missing in the above code to use some functionality
In server Code
#include
#include
In Client Code
#include
#include < arpa/inet.h >
Correction in my above comment
Your comment is awaiting moderation.
Some libraries are missing in the above code to use some functionality
In server Code
#include <arpa/inet.h>
#include <ctype.h>
In Client Code
#include <arpa/inet.h>