``c,#include,#include,#include,#include,#include,,int main(int argc, char *argv[]) {, int socket_desc, client_sock, c, read_size;, struct sockaddr_in server, client;, char client_message[2000];,, // 创建套接字, socket_desc = socket(AF_INET, SOCK_STREAM, 0);, if (socket_desc == -1) {, printf("无法创建套接字");, return 1;, }, puts("套接字创建成功");,, // 准备 sockaddr_in 结构体, server.sin_family = AF_INET;, server.sin_addr.s_addr = INADDR_ANY;, server.sin_port = htons(8888);,, // 绑定, if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server))< 0) {, perror("绑定失败。错误");, return 1;, }, puts("绑定完成");,, // 监听, listen(socket_desc, 3);,, // 接受并处理连接, puts("等待连接...");, c = sizeof(struct sockaddr_in);, while ((client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c))) {, puts("连接已建立");,, // 接收消息, while ((read_size = recv(client_sock, client_message, 2000, 0)) > 0) {, // 发送消息回客户端, write(client_sock, client_message, strlen(client_message));, },, if (read_size == 0) {, puts("客户端断开连接");, fflush(stdout);, } else if (read_size == -1) {, perror("接收失败");, }, },, if (client_sock< 0) {, perror("接受失败");, return 1;, },, return 0;,},
``