Build the minimal programs that exchange text over a TCP socket β server side and client side.
socket() β create socketbind() β attach to a portlisten() β start listeningaccept() β accept a connectionrecv() / send()close()socket() β create socketconnect() β reach the serversend() / recv()close()bind/listen/accept are server-only, connect is client-only. Everything else is shared.#include <stdio.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> #define PORT 12345 #define BUF_SIZE 1024 int main(void) { // 1. create socket int listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (listen_fd < 0) { perror("socket"); return 1; } // (recommended) allow quick reuse of the port int yes = 1; setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); // 2. prepare address to listen on struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); // any local IP addr.sin_port = htons(PORT); // 3. bind β attach the socket to the port if (bind(listen_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { perror("bind"); return 1; } // 4. listen β start accepting connections if (listen(listen_fd, 1) < 0) { perror("listen"); return 1; } printf("Listening on port %d...\n", PORT); // 5. accept one client struct sockaddr_in client_addr; socklen_t clen = sizeof(client_addr); int conn_fd = accept(listen_fd, (struct sockaddr*)&client_addr, &clen); if (conn_fd < 0) { perror("accept"); return 1; } printf("Client connected: %s\n", inet_ntoa(client_addr.sin_addr)); // 6. echo loop β read what arrives, send it back char buf[BUF_SIZE]; ssize_t n; while ((n = recv(conn_fd, buf, BUF_SIZE - 1, 0)) > 0) { buf[n] = '\0'; printf("received: %s", buf); send(conn_fd, buf, n, 0); // send the same bytes back } // 7. clean up close(conn_fd); close(listen_fd); printf("Connection closed\n"); return 0; }
inet_addr("127.0.0.1").#include <stdio.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> #define PORT 12345 #define BUF_SIZE 1024 int main(void) { // 1. create socket int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket"); return 1; } // 2. target address: localhost:12345 struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(PORT); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // 3. connect β reach the server if (connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { perror("connect"); return 1; } printf("Connected. Type a message then Enter:\n"); // 4. read one line from stdin and send it char msg[BUF_SIZE]; if (fgets(msg, BUF_SIZE, stdin) == NULL) return 1; send(sockfd, msg, strlen(msg), 0); // 5. receive the reply char buf[BUF_SIZE]; ssize_t n = recv(sockfd, buf, BUF_SIZE - 1, 0); if (n > 0) { buf[n] = '\0'; printf("Server replied: %s", buf); } // 6. close close(sockfd); return 0; }
127.0.0.1 (localhost), so run the server on the same machine. To reach a different machine, use its IP address.bind: Address already in use β another process holds the port. Wait a moment, or change the port.connect: Connection refused β the server is not running, or the port/IP is wrong.accept.toupper from <ctype.h>.accept, call fork() so each client is handled by a child process while the parent loops back to accept../echo_server 8080. Use argv[1]. See the argc/argv lesson.