Name: Abhishek Mudit
Reg: 22BCB0065
UDP SOCKETS(SERVER & CLIENT) USING C
Write a Socket- stream based program that responds to client messages as follows: When it receives a
message from a client, it simply converts the message into all uppercase letters and sends back the
same to the client. Write both client and server programs demonstrating this.
Server Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <arpa/inet.h>
#define MAX 1024
#define PORT 8080
#define SA struct sockaddr
void to_uppercase(char* str) {
for (int i = 0; str[i]; i++) {
str[i] = toupper(str[i]);
int main() {
int sockfd;
struct sockaddr_in servaddr, cliaddr;
char buffer[MAX];
socklen_t len;
// Socket creation
Name: Abhishek Mudit
Reg: 22BCB0065
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
printf("Socket creation failed...\n");
exit(0);
} else {
printf("Socket successfully created..\n");
// Server address setup
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
// Bind socket
if (bind(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
printf("Socket binding failed...\n");
exit(0);
} else {
printf("Socket successfully binded..\n");
len = sizeof(cliaddr);
// Chat loop
while (1) {
bzero(buffer, MAX);
// Receive message from client
recvfrom(sockfd, buffer, MAX, 0, (SA*)&cliaddr, &len);
printf("From client %s", buffer);
Name: Abhishek Mudit
Reg: 22BCB0065
// Check for 'exit' command
if (strncmp("exit", buffer, 4) == 0) {
printf("To client : exit\n");
sendto(sockfd, buffer, MAX, 0, (SA*)&cliaddr, len);
printf("Server Exit...\n");
break;
// Convert to uppercase
to_uppercase(buffer);
printf("To client : %s", buffer);
// Send the uppercase message back to client
sendto(sockfd, buffer, MAX, 0, (SA*)&cliaddr, len);
// Close the socket
close(sockfd);
return 0;
OUTPUT:
Client code:
#include <stdio.h>
#include <stdlib.h>
Name: Abhishek Mudit
Reg: 22BCB0065
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define MAX 1024
#define PORT 8080
#define SA struct sockaddr
int main() {
int sockfd;
struct sockaddr_in servaddr;
char buffer[MAX];
socklen_t len;
// Socket creation
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
printf("Socket creation failed...\n");
exit(0);
} else {
printf("Socket successfully created..\n");
// Server address setup
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
len = sizeof(servaddr);
// Chat loop
Name: Abhishek Mudit
Reg: 22BCB0065
while (1) {
bzero(buffer, MAX);
// Input the string from the user
printf("Enter string : ");
int n = 0;
while ((buffer[n++] = getchar()) != '\n');
// Send message to server
sendto(sockfd, buffer, MAX, 0, (SA*)&servaddr, len);
// If message is 'exit', break
if (strncmp("exit", buffer, 4) == 0) {
printf("From Server : exit\n");
printf("Client Exit...\n");
break;
// Receive the server's response
bzero(buffer, MAX);
recvfrom(sockfd, buffer, MAX, 0, (SA*)&servaddr, &len);
printf("From Server : %s", buffer);
// Close the socket
close(sockfd);
return 0;
}
Name: Abhishek Mudit
Reg: 22BCB0065
OUTPUT:
OVERALL OUTPUT (both terminal simultaneously):