Last active
January 13, 2022 12:15
-
-
Save gustavorv86/df9cf96038608ed58505d2e746213f33 to your computer and use it in GitHub Desktop.
TCP Sockets written in C
This file contains hidden or 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 <arpa/inet.h> | |
#include <netdb.h> | |
#include <pthread.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <unistd.h> | |
#define HOST "localhost" | |
#define SERVER_PORT 3107 | |
#define STATUS_OK "OK" | |
#define FLAGS 0 | |
pthread_barrier_t barrier_server_init; | |
void * tcp_server(void * args) { | |
int sock_server = 0, sock_client = 0; | |
struct sockaddr_in server, client; | |
int c = sizeof(struct sockaddr_in); | |
char client_message[1024] = {0}; | |
int read_size = 0; | |
printf("SERVER: start \n"); | |
sock_server = socket(AF_INET , SOCK_STREAM , 0); | |
if (sock_server == -1) { | |
perror("ERROR socket: "); | |
return NULL; | |
} | |
// Prepare the sockaddr_in structure | |
server.sin_family = AF_INET; | |
server.sin_addr.s_addr = INADDR_ANY; | |
server.sin_port = htons(SERVER_PORT); | |
// Bind | |
printf("SERVER: bind \n"); | |
if(bind(sock_server, (struct sockaddr *)&server , sizeof(server)) < 0) { | |
perror("ERROR bind: "); | |
return NULL; | |
} | |
printf("SERVER: listen \n"); | |
if(listen(sock_server , 5) < 0) { | |
perror("ERROR listen: "); | |
return NULL; | |
} | |
pthread_barrier_wait(&barrier_server_init); | |
while(1) { | |
c = sizeof(struct sockaddr_in); | |
sock_client = accept(sock_server, (struct sockaddr *)&client, (socklen_t*)&c); | |
bzero(client_message, sizeof(client_message)); | |
read_size = recv(sock_client, client_message, sizeof(client_message), FLAGS); | |
printf("SERVER: Receive message (%d bytes) \"%s\"\n", read_size, client_message); | |
sleep(3); | |
send(sock_client, STATUS_OK, strlen(STATUS_OK), FLAGS); | |
close(sock_client); | |
fflush(stdout); | |
} | |
return NULL; | |
} | |
void * tcp_client(void * args) { | |
int sock; | |
struct hostent * host; | |
struct sockaddr_in server; | |
char request[1024] , response[1024]; | |
pthread_barrier_wait(&barrier_server_init); | |
// Create socket | |
sock = socket(AF_INET , SOCK_STREAM , 0); | |
if (sock == -1) { | |
perror("ERROR socket: "); | |
} | |
host = gethostbyname(HOST); | |
memcpy(&server.sin_addr.s_addr, host->h_addr_list[0], sizeof(server.sin_addr.s_addr)); | |
// server.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
server.sin_family = AF_INET; | |
server.sin_port = htons(SERVER_PORT); | |
// Connect to remote server | |
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) { | |
perror("ERROR conncect: "); | |
return NULL; | |
} | |
snprintf(request, sizeof(request), "Hello from client to server..."); | |
send(sock , request , strlen(request) , FLAGS); | |
recv(sock , response , sizeof(response) , FLAGS); | |
printf("CLIENT: Receive message \"%s\"\n", response); | |
close(sock); | |
printf("CLIENT: done \n"); | |
fflush(stdout); | |
return NULL; | |
} | |
int main(int argc, char** argv) { | |
pthread_t client, server; | |
printf("Start...\n"); | |
pthread_barrier_init(&barrier_server_init, NULL, 2 /* Client and Server */); | |
pthread_create(&client, NULL, &tcp_client, NULL); | |
pthread_create(&server, NULL, &tcp_server, NULL); | |
pthread_join(client, NULL); | |
pthread_join(server, NULL); | |
printf("Done...\n"); | |
return (EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment