Last active
August 11, 2021 06:09
-
-
Save pavi2410/3d4b1bb767b05410be98da811eee7951 to your computer and use it in GitHub Desktop.
UDP Echo Server-Client in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <netdb.h> | |
#include <arpa/inet.h> | |
int main() { | |
struct sockaddr_in server_addr; | |
server_addr.sin_family = AF_INET; | |
server_addr.sin_port = htons(3000); | |
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
int sockfd; | |
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { | |
printf("couldn't create socket\n"); | |
return 1; | |
} | |
printf("socket created\n"); | |
while (1) { | |
char msg[100]; | |
printf("write your message : "); | |
scanf("%[^\n]%*c", msg); | |
int len = sendto(sockfd, msg, strlen(msg), 0, (struct sockaddr *) &server_addr, sizeof(server_addr)); | |
char buffer[100]; | |
recvfrom(sockfd, buffer, len, 0, NULL, NULL); | |
buffer[len] = 0; | |
printf("received: '%s'\n", buffer); | |
if (strcmp(buffer, "bye") == 0) { | |
printf("exiting...\n"); | |
break; | |
} | |
} | |
close(sockfd); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
int main() { | |
struct sockaddr_in server_addr; | |
server_addr.sin_family = AF_INET; | |
server_addr.sin_port = htons(3000); | |
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
int sockfd; | |
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { | |
printf("couldn't create socket\n"); | |
return 1; | |
} | |
printf("socket created\n"); | |
if (bind(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) { | |
printf("couldn't bind socket\n"); | |
return 1; | |
} | |
printf("bind at port 3000\n"); | |
struct sockaddr_in client_addr; | |
int client_addr_size = sizeof(client_addr); | |
while (1) { | |
char buffer[100]; | |
int len = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *) &client_addr, &client_addr_size); | |
buffer[len] = 0; | |
printf("received: '%s' from %s\n", buffer, inet_ntoa(client_addr.sin_addr)); | |
sendto(sockfd, buffer, len, 0, (struct sockaddr *) &client_addr, sizeof(client_addr)); | |
} | |
close(sockfd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment