0% found this document useful (0 votes)
9 views3 pages

UDP Client-Server Program in C

The document contains a C implementation of a UDP server and client program. The server listens for messages on port 8810, echoes them back to the client, and can be terminated by sending the message 'exit'. The client sends a message to the server and waits for a response, also terminating if 'exit' is sent.

Uploaded by

emojianna123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

UDP Client-Server Program in C

The document contains a C implementation of a UDP server and client program. The server listens for messages on port 8810, echoes them back to the client, and can be terminated by sending the message 'exit'. The client sends a message to the server and waits for a response, also terminating if 'exit' is sent.

Uploaded by

emojianna123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//Server Program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
int sockfd, n;
socklen_t len;
char buff[512];
struct sockaddr_in ser_addr, cli_addr;

// Creating UDP socket (SOCK_DGRAM instead of SOCK_STREAM)


if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket error");
exit(1);
}

// Initializing server address


memset(&ser_addr, 0, sizeof(ser_addr));
ser_addr.sin_family = AF_INET;
ser_addr.sin_port = htons(8810);
ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);

// Binding
if (bind(sockfd, (struct sockaddr*)&ser_addr, sizeof(ser_addr)) < 0) {
perror("bind error");
exit(1);
}

while (1) {
len = sizeof(cli_addr);

// Receiving message from client


n = recvfrom(sockfd, buff, sizeof(buff) - 1, 0,
(struct sockaddr*)&cli_addr, &len);
if (n < 0) {
perror("recvfrom error");
continue;
}
buff[n] = '\0'; // null terminate

printf("Message received from client: %s\n", buff);


if (strncmp(buff, "exit", 4) == 0) {
printf("Server is closing...\n");
break;
}

// Sending back the same message (echo)


sendto(sockfd, buff, n, 0,
(struct sockaddr*)&cli_addr, len);
printf("Message echoed back to client.\n");
}

close(sockfd);
return 0;
}

//Client Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main() {
int sockfd, n;
char buff1[512], buff2[512];
struct sockaddr_in serv_addr;
socklen_t len;

// Creating UDP socket


if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket error");
exit(1);
}

// Initializing server address


memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8810);
serv_addr.sin_addr.s_addr = inet_addr("[Link]"); // loopback

len = sizeof(serv_addr);

printf("Enter the message to be sent:\n");


fflush(stdout);

// Reading message from keyboard


n = read(0, buff1, sizeof(buff1) - 1);
if (n < 0) {
perror("read error");
close(sockfd);
exit(1);
}
buff1[n] = '\0'; // null terminate

printf("Message sent to the server: %s\n", buff1);

// Sending message to server


sendto(sockfd, buff1, n, 0,
(struct sockaddr*)&serv_addr, len);

if (strncmp(buff1, "exit", 4) == 0) {
printf("Client is exiting...\n");
close(sockfd);
exit(0);
}

// Receiving response from server


n = recvfrom(sockfd, buff2, sizeof(buff2) - 1, 0,
(struct sockaddr*)&serv_addr, &len);
if (n < 0) {
perror("recvfrom error");
close(sockfd);
exit(1);
}
buff2[n] = '\0'; // null terminate

printf("Message received from the server: %s\n", buff2);

// Closing socket
close(sockfd);
return 0;
}

You might also like