Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
29 views4 pages

UDP Client/Server Communication Aim: Procedure: Server (Server.c)

Uploaded by

Tejesshree.S
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)
29 views4 pages

UDP Client/Server Communication Aim: Procedure: Server (Server.c)

Uploaded by

Tejesshree.S
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
You are on page 1/ 4

LAB: 03

Date: 22.08.2024
UDP Client/Server Communication

Aim:
To implement the UDP Client/Server Communication.
Procedure:
Server (server.c):
1. Initialize: Check for the port argument; define IP as "127.0.0.1" and convert the port
argument to an integer.
2. Create UDP Socket: Use `socket(AF_INET, SOCK_DGRAM, 0)` to create a UDP socket.
3. Configure Server Address: Set address family to `AF_INET`, port using `htons(port)`, and
IP using `inet_addr(ip)`.
4. Bind: Bind the socket to the IP and port; exit if bind fails.
5. Receive Data: Use `recvfrom` to receive a message from the client and print it.
6. Respond: Prepare a response message and use `sendto` to send it back to the client.
7. End: The server completes the transaction.
Client (client.c):
1. Initialize: Check for the port argument, define server IP as "127.0.0.1," and convert the
port argument to an integer.
2. Create UDP Socket: Use `socket(AF_INET, SOCK_DGRAM, 0)` to create a UDP socket.
3. Set Server Address: Configure `addr` with address family `AF_INET`, port using
`htons(port)`, and IP using `inet_addr(ip)`.
4. Send Data: Set a message in `buffer` and send it to the server using `sendto`.
5. Receive Response: Use `recvfrom` to receive the server's response, then print it.
6. End: The client completes the transaction.
Server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char **argv){
if (argc != 2){
printf("Usage: %s <port>\n", argv[0]);

1
2022503524
exit(0);
}
char *ip = "127.0.0.1";
int port = atoi(argv[1]);
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 error");
exit(1);
}
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 error");
exit(1);
}
bzero(buffer, 1024);
addr_size = sizeof(client_addr);
recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr*)&client_addr, &addr_size);
printf("[+]Data recv: %s\n", buffer);
bzero(buffer, 1024);
strcpy(buffer, "Welcome to the UDP Server.");
sendto(sockfd, buffer, 1024, 0, (struct sockaddr*)&client_addr, sizeof(client_addr));
printf("[+]Data send: %s\n", buffer);
return 0;
}
Output:

2
2022503524
Client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char **argv){
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
exit(0);
}
char *ip = "127.0.0.1";
int port = atoi(argv[1]);
int sockfd;
struct sockaddr_in addr;
char buffer[1024];
socklen_t addr_size;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
bzero(buffer, 1024);
strcpy(buffer, "Hello, World!");
sendto(sockfd, buffer, 1024, 0, (struct sockaddr*)&addr, sizeof(addr));
printf("[+]Data send: %s\n", buffer);
bzero(buffer, 1024);
addr_size = sizeof(addr);
recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr*)&addr, &addr_size);
printf("[+]Data recv: %s\n", buffer);
return 0;
}
Output

3
2022503524
Result:
The UDP Server/Client communication has been implemented successfully and the
message is sent successfully.

4
2022503524

You might also like