Skip to content

Instantly share code, notes, and snippets.

@audinue
Created March 24, 2024 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audinue/45d3343eb8160c7f81faf689e0d5629e to your computer and use it in GitHub Desktop.
Save audinue/45d3343eb8160c7f81faf689e0d5629e to your computer and use it in GitHub Desktop.
Echo server & client in C.
@echo off
tcc echo_server.c
tcc echo_client.c
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32")
void main() {
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("WSAStartup() failed\n");
return;
}
SOCKET client;
if ((client = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
printf("socket() failed\n");
return;
}
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = 8080;
if (connect(client, &addr, sizeof(addr)) == SOCKET_ERROR) {
printf("connect() failed\n");
return;
}
char* msg = "Hello world!";
int len = strlen(msg);
int total = 0;
do {
int sent;
if ((sent = send(client, msg, len, 0)) == SOCKET_ERROR) {
printf("send() failed\n");
return;
}
total += sent;
} while (total < len);
printf("sent: %s\n", msg);
char msg2[1024] = {};
int len2;
if ((len2 = recv(client, msg2, sizeof(msg2) - 1, 0)) == SOCKET_ERROR) {
printf("recv() failed\n");
return;
}
printf("received: %s\n", msg2);
if (closesocket(client) == SOCKET_ERROR) {
printf("closesocket(client) failed\n");
return;
}
if (WSACleanup() != 0) {
printf("WSACleanup() failed\n");
return;
}
}
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32")
void main() {
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("WSAStartup() failed\n");
return;
}
SOCKET server;
if ((server = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
printf("socket() failed\n");
return;
}
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = 8080;
if (bind(server, &addr, sizeof(addr)) == SOCKET_ERROR) {
printf("bind() failed\n");
return;
}
if (listen(server, 1) == SOCKET_ERROR) {
printf("listen() failed\n");
return;
}
SOCKET client;
if ((client = accept(server, 0, 0)) == INVALID_SOCKET) {
printf("accept() failed\n");
return;
}
char msg[1024] = {};
int len;
if ((len = recv(client, msg, sizeof(msg), 0)) == SOCKET_ERROR) {
printf("recv() failed\n");
return;
}
printf("received: %s\n", msg);
msg[0] = 'X';
int total = 0;
do {
int sent;
if ((sent = send(client, msg, len, 0)) == SOCKET_ERROR) {
printf("send() failed\n");
return;
}
total += sent;
} while (total < len);
printf("sent: %s\n", msg);
if (closesocket(client) == SOCKET_ERROR) {
printf("closesocket(client) failed\n");
return;
}
if (closesocket(server) == SOCKET_ERROR) {
printf("closesocket(server) failed\n");
return;
}
if (WSACleanup() != 0) {
printf("WSACleanup() failed\n");
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment