Created
September 30, 2023 01:27
-
-
Save tiagopog/34abaa3e9a53ebe71390c6fc1b845c4f to your computer and use it in GitHub Desktop.
TCP Client/Server with Sockets API (Berkeley Sockets)
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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <unistd.h> | |
int main(void) { | |
struct sockaddr_in server_address; /* address of the server */ | |
int client_socket; /* FD of socket to connect with servers */ | |
char client_message[2000], server_message[2000]; /* holds messages from client and server */ | |
// Clean buffers: | |
memset(server_message, '\0', sizeof(server_message)); | |
memset(client_message, '\0', sizeof(client_message)); | |
client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
if (client_socket < 0) { | |
printf("Client socket failed to create\n"); | |
exit(1); | |
} | |
printf("Client socket successfully created\n"); | |
// Set port and IP the same as server-side: | |
server_address.sin_family = AF_INET; | |
server_address.sin_port = htons(2000); | |
server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
// Send connection request to server: | |
if (connect(client_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) { | |
printf("Unable to connect to 127.0.0.1:2000\n"); | |
exit(2); | |
} | |
// Get input from the user: | |
printf("Enter message: "); | |
fgets(client_message, 2000, stdin); | |
// Send the message to server: | |
if (send(client_socket, client_message, strlen(client_message), 0) < 0) { | |
printf("Unable to send message\n"); | |
exit(3); | |
} | |
// Receive the server's response: | |
if (recv(client_socket, server_message, sizeof(server_message), 0) < 0) { | |
printf("Error while receiving server's msg\n"); | |
exit(4); | |
} | |
printf("Server response:\n => %s \n", server_message); | |
// Close the socket: | |
close(client_socket); | |
return 0; | |
} |
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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <unistd.h> | |
int main(void) { | |
struct sockaddr_in client_address; /* client address information */ | |
struct sockaddr_in server_address; /* server address information */ | |
int client_socket; /* FD of socket connected to client */ | |
int server_socket; /* FD of socket for accepting connections */ | |
unsigned int client_name_len; /* used for pointer to the length of client address */ | |
char server_message[2000]; /* server message sent to client */ | |
char client_message[2000]; /* client message received in the server */ | |
/* | |
* Clean buffers with a null terminator: | |
*/ | |
memset(server_message, '\0', sizeof(server_message)); | |
memset(client_message, '\0', sizeof(client_message)); | |
/* | |
* Create a TCP/IP stream socket that returns the socket descriptor: | |
*/ | |
server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
if (server_socket < 0) { | |
printf("Error while creating the server socket\n"); | |
exit(1); | |
} | |
printf("Socket created successfully\n"); | |
/* | |
* Set the server address: IP (ipv4) and port | |
*/ | |
server_address.sin_family = AF_INET; | |
server_address.sin_port = htons(2000); | |
server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
/* | |
* Bind the socket to the server address: | |
*/ | |
if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) { | |
printf("Error while binding to the port\n"); | |
exit(2); | |
} | |
printf("Socket bound to 127.0.0.1:2000\n"); | |
/* | |
* Set the socket to listening state so it can receive incoming connections. | |
* The second argument is the max number of pending connections: | |
*/ | |
if (listen(server_socket, 3) < 0) { | |
printf("Error while listening to incoming connections\n"); | |
exit(3); | |
} | |
printf("Listening to incoming connections...\n"); | |
/* | |
* Accept an incoming connection. | |
* The server-side code stops and waits at accept() until a client calls connect(): | |
*/ | |
for (;;) { | |
client_socket = accept(server_socket, (struct sockaddr*)&client_address, &client_name_len); | |
if (client_socket < 0) { | |
printf("Can't accept connection\n"); | |
printf("Error: %d\n", client_socket); | |
exit(4); | |
} | |
char *client_ip = inet_ntoa(client_address.sin_addr); | |
int client_port = ntohs(client_address.sin_port); | |
printf("Client connected at IP %s and port %i\n", client_ip, client_port); | |
/* | |
* Receive client's message: | |
* When recv() is called, the code stops and waits for a message from the client. | |
*/ | |
if (recv(client_socket, client_message, sizeof(client_message), 0) < 0) { | |
printf("Error while receiving the message\n"); | |
exit(5); | |
} | |
printf("Message from client: %s\n", client_message); | |
/* | |
* Respond to client: | |
*/ | |
/* strcpy(server_message, "pong %s\n", client_message); */ | |
snprintf(server_message, sizeof(server_message), "pong: %s", client_message); | |
if (send(client_socket, server_message, strlen(server_message), 0) < 0) { | |
printf("Can't send message\n"); | |
exit(6); | |
} | |
close(client_socket); | |
} | |
/* | |
* Close sockets: | |
*/ | |
close(server_socket); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment