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

Sample UDP Implementation

Uploaded by

Shilpa Das
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)
15 views4 pages

Sample UDP Implementation

Uploaded by

Shilpa Das
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

Sample UDP Implementation

// Server side implementation of UDP client-server model

#include <bits/stdc++.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

// Driver code

int main() {

int sockfd;

char buffer[MAXLINE];

const 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);

socklen_t len;

int n;

len = sizeof(cliaddr); //len is value/result

n = recvfrom(sockfd, (char *)buffer, MAXLINE,

MSG_WAITALL, ( struct sockaddr *) &cliaddr,

&len);

buffer[n] = '\0';

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

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

MSG_CONFIRM, (const struct sockaddr *) &cliaddr,

len);

std::cout<<"Hello message sent."<<std::endl;

return 0;
}

// Client side implementation of UDP client-server model

#include <bits/stdc++.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

// Driver code

int main() {

int sockfd;

char buffer[MAXLINE];

const 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;

socklen_t len;

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

MSG_CONFIRM, (const struct sockaddr *) &servaddr,

sizeof(servaddr));

std::cout<<"Hello message sent."<<std::endl;

n = recvfrom(sockfd, (char *)buffer, MAXLINE,

MSG_WAITALL, (struct sockaddr *) &servaddr,

&len);

buffer[n] = '\0';

std::cout<<"Server :"<<buffer<<std::endl;

close(sockfd);

return 0;

You might also like