Skip to content

Instantly share code, notes, and snippets.

@EteimZ
Created November 26, 2023 02:37
Show Gist options
  • Select an option

  • Save EteimZ/72c17c8a994239931422d07e3b3a1224 to your computer and use it in GitHub Desktop.

Select an option

Save EteimZ/72c17c8a994239931422d07e3b3a1224 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
#define MAX_BUFFER_SIZE 1024
int main() {
int client_socket;
struct sockaddr_in server_addr;
char buffer[MAX_BUFFER_SIZE];
// Create a socket
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Set up the server address
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(PORT);
if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Error connecting to server");
close(client_socket);
exit(EXIT_FAILURE);
}
// Send a message to the server
const char *message = "Hello, Echo Server!";
write(client_socket, message, strlen(message));
// Receive and print the echoed message from the serve
ssize_t bytes_received = read(client_socket, buffer, sizeof(buffer));
if (bytes_received == -1){
perror("Error reading from server");
} else {
buffer[bytes_received] = '\0';
printf("Received from server: %s\n", buffer);
}
close(client_socket);
return 0;
}
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(EchoServerClient)
# Server executable
add_executable(server server.c)
# Client executable
add_executable(client client.c)
CC = gcc
CFLAGS = -Wall -Wextra
all: server client
server: server.c
$(CC) $(CFLAGS) -o server server.c
client: client.c
$(CC) $(CFLAGS) -o client client.c
.PHONY: clean run_server run_client
clean:
rm -f server client
run_server: server
./server
run_client: client
./client
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
#define MAX_BUFFER_SIZE 1024
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len = sizeof(client_addr);
char buffer[MAX_BUFFER_SIZE];
// Create a socket
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Set up the server address
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
// Bind the socket to the specified address and port
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Error binding socket");
close(server_socket);
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(server_socket, 5) == -1) {
perror("Error listening for connections");
close(server_socket);
exit(EXIT_FAILURE);
}
printf("Server listening on port %d...\n", PORT);
// Accept and handle incoming connections
while (1){
// Accept a connection
if ((client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len)) == -1){
perror("Error accepting connection");
continue;
}
printf("Accepted connection from %s\n", inet_ntoa(client_addr.sin_addr));
// Receive and echo data
ssize_t bytes_received;
while ((bytes_received = read(client_socket, buffer, sizeof(buffer))) > 0) {
// Echo back the received data
write(client_socket, buffer, bytes_received);
}
if (bytes_received == -1) {
perror("Error reading from client");
}
// Close the client socket
close(client_socket);
printf("Connection closed\n");
}
// Close the server socket (this part will not be reached in this example)
close(server_socket);
return 0;
}

Usage

To run the build the program you can use CMAKE or Make.

Using Make

Running the following commands

make all
make run_server  # Run this in one terminal
make run_client  # Run this in another terminal

Using CMake

Create a build directory:

mkdir build
cd build

Run the command below from within the build directory:

cmake ..

This will make your Makefile then build your project by running the command below:

make

This will create the executables.

Run the server executable:

./server

Open another terminal and run the client executable:

./client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment