0% found this document useful (0 votes)
2 views5 pages

Expt 8

The document describes the User Datagram Protocol (UDP), highlighting its connectionless nature and suitability for time-sensitive applications despite potential packet loss. It includes source code for a simple UDP client-server model in C, detailing the server's ability to receive messages and respond, as well as the client's functionality to send messages and receive responses. Example output from both the server and client is provided to illustrate the communication process.

Uploaded by

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

Expt 8

The document describes the User Datagram Protocol (UDP), highlighting its connectionless nature and suitability for time-sensitive applications despite potential packet loss. It includes source code for a simple UDP client-server model in C, detailing the server's ability to receive messages and respond, as well as the client's functionality to send messages and receive responses. Example output from both the server and client is provided to illustrate the communication process.

Uploaded by

sharmila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Description

UDP:

• Does not establish a connection before data transmission, making it faster.

• Sends packets directly to the destination without ensuring delivery, order, or


integrity.

• Is suitable for time-sensitive applications, although it may result in packet loss.

Source Code

Server Code

The server initializes a UDP socket, receives messages from a client, and sends a
response back.

// Server side implementation of UDP client-server model

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#define PORT 8080

#define MAXLINE 1024

int main() {

int sockfd;

char buffer[MAXLINE];

char *hello = "Hello from server";

struct sockaddr_in servaddr, cliaddr;


// Creating socket file descriptor

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {

perror("socket creation failed");

exit(EXIT_FAILURE);

memset(&servaddr, 0, sizeof(servaddr));

memset(&cliaddr, 0, sizeof(cliaddr));

// Filling server information

servaddr.sin_family = AF_INET; // IPv4

servaddr.sin_addr.s_addr = INADDR_ANY;

servaddr.sin_port = htons(PORT);

// Bind the socket with the server address

if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {

perror("bind failed");

exit(EXIT_FAILURE);

int len, n;

len = sizeof(cliaddr);

// Receive message from client

n = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, (struct sockaddr *)


&cliaddr, &len);

buffer[n] = '\0';

printf("Client : %s\n", buffer);


// Send response to client

sendto(sockfd, (const char *)hello, strlen(hello), MSG_CONFIRM, (const struct


sockaddr *) &cliaddr, len);

printf("Hello message sent.\n");

return 0;

Client Code

The client initializes a UDP socket, sends a message to the server, and waits for a
response.

// Client side implementation of UDP client-server model

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#define PORT 8080

#define MAXLINE 1024

int main() {

int sockfd;

char buffer[MAXLINE];

char *hello = "Hello from client";

struct sockaddr_in servaddr;


// Creating socket file descriptor

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {

perror("socket creation failed");

exit(EXIT_FAILURE);

memset(&servaddr, 0, sizeof(servaddr));

// Filling server information

servaddr.sin_family = AF_INET;

servaddr.sin_port = htons(PORT);

servaddr.sin_addr.s_addr = INADDR_ANY;

int n, len;

// Send message to server

sendto(sockfd, (const char *)hello, strlen(hello), MSG_CONFIRM, (const struct


sockaddr *) &servaddr, sizeof(servaddr));

printf("Hello message sent.\n");

// Receive response from server

n = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, (struct sockaddr *)


&servaddr, &len);

buffer[n] = '\0';

printf("Server : %s\n", buffer);

close(sockfd);

return 0;
}

Explanation

1. Server:

o Creates a UDP socket and binds it to a specified port.

o Waits for a message from the client and responds with "Hello from server."

2. Client:

o Creates a UDP socket and sends "Hello from client" to the server.

o Receives a response from the server and displays it.

Output Example

Server

$ ./server

Client: Hello from client

Hello message sent.

Client

$ ./client

Hello message sent.

Server : Hello from server

You might also like