UDP Client Server Communication Using Sockets
To implement a UDP client and server program to exchange data using socket programming in C
Algorithms
Client
Step 1: Create Socket
- Create a UDP socket using socket(AF_INET, SOCK_DGRAM, 0).
- If socket creation fails, terminate the program.
Step 2: Define Server Address
- Set the address family to AF_INET.
- Set the port number to 5555.
- Set the IP address to 127.0.0.1.
Step 3: Get Input from User
- Read data from the user and store it in a buffer.
Step 4: Send Data to Server
- Send the data to the server using sendto().
Step 5: Receive Response
- Receive the response from the server using recvfrom().
- Display the received message.
Step 6: Close Socket
Server
Step 1: Create Socket
- Create a UDP socket using socket(AF_INET, SOCK_DGRAM, 0).
- If socket creation fails, terminate the program.
Step 2: Define Server Address
- Set the address family to AF_INET.
- Set the port number to 5555.
- Set the IP address to 127.0.0.1.
Step 3: Bind Socket
- Bind the socket to the specified IP address and port using bind().
Step 4: Receive Data from Client
- Receive data from the client using recvfrom().
- Display the received message.
Step 5: Send Response to Client
- Send a confirmation message to the client using sendto().
Step 6: Close Socket
Programs
Client
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
char *ip = "127.0.0.1";
int port = 5555;
int sockfd;
struct sockaddr_in addr;
char buffer[1024];
socklen_t addr_size;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("[-] Socket creation failed");
exit(1);
}
printf("[CLIENT] UDP socket created\n");
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
memset(buffer, 0, sizeof(buffer));
printf("[CLIENT] Enter the data: ");
scanf(" %[^\n]", buffer);
sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&addr,
sizeof(addr));
printf("[CLIENT] Sent to server: %s\n", buffer);
memset(buffer, 0, sizeof(buffer));
addr_size = sizeof(addr);
recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&addr,
&addr_size);
printf("[CLIENT] Received from server: %s\n", buffer);
close(sockfd);
return 0;
}
Server
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
char *ip = "127.0.0.1";
int port = 5555;
int sockfd;
struct sockaddr_in server_addr, client_addr;
char buffer[1024];
socklen_t addr_size;
int n;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("[-] Socket creation failed");
exit(1);
}
printf("[SERVER] UDP socket created\n");
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(ip);
n = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (n < 0) {
perror("[-] Bind failed");
exit(1);
}
printf("[SERVER] Bound to %s:%d\n", ip, port);
memset(buffer, 0, sizeof(buffer));
addr_size = sizeof(client_addr);
recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_addr,
&addr_size);
printf("[SERVER] Received from client: %s\n", buffer);
strcpy(buffer, "Data successfully received");
sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&client_addr,
sizeof(client_addr));
printf("[SERVER] Sent to client: %s\n", buffer);
close(sockfd);
return 0;
}