Last active
December 3, 2023 17:15
-
-
Save aagontuk/b11584225f42fd8a59520e08f1404c9d to your computer and use it in GitHub Desktop.
Simple server-client communication example 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 <sys/socket.h> | |
#include <sys/types.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define MAX_DATA_LEN 8192 | |
int main(int argc, char *argv[]){ | |
int sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
struct sockaddr_in server_address; | |
memset(&server_address, 0, sizeof(server_address)); | |
/* setup server address and port */ | |
server_address.sin_family = AF_INET; | |
server_address.sin_port = htons(1711); | |
inet_aton("127.0.0.1", &server_address.sin_addr); | |
/* connect to the server */ | |
if(connect(sockfd, (struct sockaddr *)&server_address, sizeof(server_address)) == -1){ | |
perror("connection failed"); | |
close(sockfd); | |
exit(EXIT_FAILURE); | |
} | |
/* connected. send some data to server */ | |
char msg[] = "hello server"; | |
int bytes_sent = write(sockfd, msg, strlen(msg)); | |
printf("%d bytes written to server!\n", bytes_sent); | |
/* waiting for response and read when available */ | |
char *read_buffer = malloc(MAX_DATA_LEN + 1); | |
int bytes_read = read(sockfd, read_buffer, MAX_DATA_LEN); | |
read_buffer[bytes_read] = '\0'; | |
printf("Data:\n%s\n", read_buffer); | |
exit(EXIT_SUCCESS); | |
} |
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 <sys/socket.h> | |
#include <sys/types.h> | |
#include <netinet/ip.h> | |
#include <arpa/inet.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
void usage(void){ | |
printf("USAGE: ./server port\n"); | |
} | |
int main(int argc, char *argv[]){ | |
int sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
struct sockaddr_in serv_addr, client_address; | |
size_t client_address_len = sizeof(client_address); | |
int port_num; | |
if(argc < 2){ | |
usage(); | |
exit(EXIT_FAILURE); | |
} | |
/* port to connect to. convert in network order */ | |
port_num = atoi(argv[1]); | |
serv_addr.sin_family = AF_INET; | |
serv_addr.sin_addr.s_addr = INADDR_ANY; /* bind to a local address */ | |
serv_addr.sin_port = htons(atoi(argv[1])); | |
int socket_opt = 1; | |
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &socket_opt, sizeof(socket_opt)); | |
if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1){ | |
perror("bind failed"); | |
} | |
if(listen(sockfd, 1024) == -1){ | |
perror("listen failed"); | |
} | |
printf("Listening on port: %s\n", argv[1]); | |
/* handle request from a client */ | |
int client_fd; | |
if((client_fd = accept(sockfd, (struct sockaddr *)&client_address, (socklen_t *)&client_address_len)) == -1){ | |
perror("accept failed"); | |
} | |
printf("connection from: %s:%u\n", inet_ntoa(client_address.sin_addr), client_address.sin_port); | |
/* read data from client */ | |
int bytes_read; | |
char *client_data = (char *)malloc(8192 * sizeof(char)); | |
if((bytes_read = read(client_fd, client_data, 1024)) > 0){ | |
//*(client_data + bytes_read) = '\0'; | |
printf("data:\n%s\n", client_data); | |
} | |
/* write some data to client */ | |
char msg[] = "hello client"; | |
int bytes_sent = write(client_fd, msg, strlen(msg)); | |
printf("%d bytes sent to %s:%u\n", bytes_sent, inet_ntoa(client_address.sin_addr), client_address.sin_port); | |
free(client_data); | |
close(client_fd); | |
close(sockfd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment