Description: This is a simple Client and Server UDP program in which the server waits for a datagram from client and on receiving datagram it displays the information, sent by the client, in proper format. From the client side, name and roll number are sent to the server.
Display Format
Roll No: XX-XXXX
Name: XXXXXX
C Code (Server)
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define PORT_NO 8888
#define IP "127.0.0.1"
int main() {
int msg_len = 100, i;
char *msg = (char*)malloc(msg_len);
struct sockaddr_in serv_addr;
int sock = socket(AF_INET,
SOCK_DGRAM, 0);
serv_addr.sin_family
= AF_INET;
serv_addr.sin_port
= htons(PORT_NO);
serv_addr.sin_addr.s_addr
= inet_addr(IP);
bind(sock,
(struct sockaddr*)&serv_addr, sizeof(serv_addr));
int sockaddr_len = sizeof(serv_addr);
recvfrom(sock,
msg, msg_len, 0, (struct sockaddr*)&serv_addr,
&sockaddr_len);
printf("\n\t*** Received from
Client ***\n");
printf("\nIP: %s", IP);
printf("\nPort# %i", PORT_NO);
printf("\nROLL NO: ");
for (i = 0;msg[i] != ' ';i++)
printf("%c", msg[i]);
printf("\nNAME: ");
for (i++;msg[i] != '\0';i++)
printf("%c", msg[i]);
printf("\n\n\n");
}
C Code (Client)
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define PORT_NO 8888
int main() {
int msg_len = 100;
char *msg = (char*)malloc(msg_len);
struct sockaddr_in serv_addr;
int sock = socket(AF_INET,
SOCK_DGRAM, 0);
serv_addr.sin_family
= AF_INET;
serv_addr.sin_port
= htons(PORT_NO);
serv_addr.sin_addr.s_addr
= inet_addr("127.0.0.1");
strcpy(msg,
"12-3456
HaZa");
sendto(sock,
msg, msg_len, 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
}
No comments
Post a Comment