Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
UDP echo client-server implementation
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
const char* server_name = "localhost";
const int server_port = 8877;
struct sockaddr_in server_address;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
// creates binary representation of server name
// and stores it as sin_addr
// http://beej.us/guide/bgnet/output/html/multipage/inet_ntopman.html
inet_pton(AF_INET, server_name, &server_address.sin_addr);
// htons: port in network order format
server_address.sin_port = htons(server_port);
// open socket
int sock;
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
printf("could not create socket\n");
return 1;
}
// data that will be sent to the server
const char* data_to_send = "Gangadhar Hi Shaktimaan hai";
// send data
int len =
sendto(sock, data_to_send, strlen(data_to_send), 0,
(struct sockaddr*)&server_address, sizeof(server_address));
// received echoed data back
char buffer[100];
recvfrom(sock, buffer, len, 0, NULL, NULL);
buffer[len] = '\0';
printf("recieved: '%s'\n", buffer);
// close the socket
close(sock);
return 0;
}
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
// port to start the server on
int SERVER_PORT = 8877;
// socket address used for the server
struct sockaddr_in server_address;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
// htons: host to network short: transforms a value in host byte
// ordering format to a short value in network byte ordering format
server_address.sin_port = htons(SERVER_PORT);
// htons: host to network long: same as htons but to long
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
// create a UDP socket, creation returns -1 on failure
int sock;
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
printf("could not create socket\n");
return 1;
}
// bind it to listen to the incoming connections on the created server
// address, will return -1 on error
if ((bind(sock, (struct sockaddr *)&server_address,
sizeof(server_address))) < 0) {
printf("could not bind socket\n");
return 1;
}
// socket address used to store client address
struct sockaddr_in client_address;
int client_address_len = 0;
// run indefinitely
while (true) {
char buffer[500];
// read content into buffer from an incoming client
int len = recvfrom(sock, buffer, sizeof(buffer), 0,
(struct sockaddr *)&client_address,
&client_address_len);
// inet_ntoa prints user friendly representation of the
// ip address
buffer[len] = '\0';
printf("received: '%s' from client %s\n", buffer,
inet_ntoa(client_address.sin_addr));
// send same content back to the client ("echo")
sendto(sock, buffer, len, 0, (struct sockaddr *)&client_address,
sizeof(client_address));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.