Linux下简单的TCP服务器,在Linux下创建一个简单的TCP服务.
在Linux下创建一个简单的TCP服务器,我们可以使用C语言和基本的socket编程。以下是一个简单的TCP服务器程序,它监听指定端口,接受客户端连接,然后接收客户端发来的数据并原样发回(echo server)。
步骤:1. 创建socket2. 绑定socket到地址和端口3. 监听连接4. 接受连接5. 读写数据6. 关闭连接
注意:这个例子没有考虑多客户端并发,一次只能处理一个客户端。
我们将编写一个回显服务器,它会在8888端口上监听。
代码示例:*/
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>
#define PORT 8888#define BUFFER_SIZE 1024
int main() {int server_fd, new_socket;struct sockaddr_in address;int opt = 1;int addrlen = sizeof(address);char buffer = {0};
// 1. 创建socket文件描述符if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {perror("socket failed");exit(EXIT_FAILURE);}
// 设置socket选项,允许地址和端口重用if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {perror("setsockopt");exit(EXIT_FAILURE);}
address.sin_family = AF_INET;address.sin_addr.s_addr = INADDR_ANY; // 接受任意IP的连接address.sin_port = htons(PORT); // 端口转换为网络字节序
// 2. 绑定socket到地址和端口if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {perror("bind failed");exit(EXIT_FAILURE);}
// 3. 监听连接,设置最大等待连接数为3if (listen(server_fd, 3) < 0) {perror("listen");exit(EXIT_FAILURE);}
printf("Server is listening on port %d...\n", PORT);
// 4. 接受连接if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {perror("accept");exit(EXIT_FAILURE);}
// 打印客户端IP地址char client_ip;inet_ntop(AF_INET, &address.sin_addr, client_ip, INET_ADDRSTRLEN);printf("Connection accepted from %s:%d\n", client_ip, ntohs(address.sin_port));
// 5. 读写数据while (1) {memset(buffer, 0, BUFFER_SIZE); // 清空缓冲区ssize_t bytes_read = read(new_socket, buffer, BUFFER_SIZE - 1); // 读取数据if (bytes_read <= 0) {// 客户端断开连接或出错perror("read error or client disconnected");break;}printf("Received: %s", buffer);
// 将收到的数据原样发回send(new_socket, buffer, strlen(buffer), 0);printf("Echoed back: %s", buffer);}
// 6. 关闭连接close(new_socket);close(server_fd);
return 0;}
/*编译:gcc server.c -o server运行:./server
注意:- 这个服务器是单线程的,一次只能处理一个客户端连接。要处理多个客户端,可以使用多进程、多线程或I/O多路复用(如select, poll, epoll)。- 实际应用中需要更完善的错误处理和资源管理。
测试:可以使用telnet或nc(netcat)工具来测试这个服务器。例如:在另一个终端执行 `nc localhost 8888`,然后输入一些文本,服务器会将文本回显。*/
在Linux下创建一个简单的TCP服务器,可以使用C语言和标准Socket API实现。以下是一个基础示例,该服务器监听指定端口,接收客户端连接,并将收到的消息原样返回(回显服务器):代码示例:tcp_echo_server.cc
复制
下载
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/socket.h>#include <netinet/in.h>#define PORT 8080#define BUFFER_SIZE 1024int main() { int server_fd, new_socket; struct sockaddr_in address; int addrlen = sizeof(address); char buffer[BUFFER_SIZE = {0}; // 1. 创建TCP Socket if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket failed"); exit(EXIT_FAILURE); } // 2. 配置服务器地址 address.sin_family = AF_INET; // IPv4 address.sin_addr.s_addr = INADDR_ANY;// 监听所有网卡 address.sin_port = htons(PORT); // 端口转换字节序 // 3. 绑定Socket到端口 if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) { perror("bind failed"); close(server_fd); exit(EXIT_FAILURE); } // 4. 开始监听(最大队列长度=3) if (listen(server_fd, 3) < 0) { perror("listen"); close(server_fd); exit(EXIT_FAILURE); } printf("Server listening on port %d\n", PORT); // 5. 接受客户端连接 if ((new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen)) < 0) { perror("accept"); close(server_fd); exit(EXIT_FAILURE); } // 打印客户端IP char client_ip[INET_ADDRSTRLEN; inet_ntop(AF_INET, &address.sin_addr, client_ip, INET_ADDRSTRLEN); printf("Client connected: %s\n", client_ip); // 6. 处理数据(回显循环) while (1) { memset(buffer, 0, BUFFER_SIZE); ssize_t bytes_read = read(new_socket, buffer, BUFFER_SIZE); if (bytes_read <= 0) {// 客户端断开或错误 printf("Client disconnected\n"); break; } printf("Received: %s", buffer); send(new_socket, buffer, bytes_read, 0);// 原样发回 } // 7. 清理资源 close(new_socket); close(server_fd); return 0;}
编译与运行
[*]保存代码:将代码保存为 tcp_echo_server.c
[*]编译:bash
复制
下载
gcc tcp_echo_server.c -o server
[*]运行服务器:bash
复制
下载
./server
测试服务器使用 netcat (nc) 工具连接服务器:bash
复制
下载
nc localhost 8080
[*]输入任意文本,服务器会立即返回相同内容
[*]按 Ctrl+C 断开连接
关键步骤解析
[*]创建Socket
socket(AF_INET, SOCK_STREAM, 0) 创建TCP协议Socket
[*]地址绑定
sockaddr_in 结构指定监听端口和网卡(INADDR_ANY 表示所有网卡)
[*]监听连接
listen() 启用连接队列(此处队列长度=3)
[*]接受连接
accept() 阻塞等待客户端连接,返回新Socket用于通信
[*]数据交换
read() 接收数据 → send() 原样返回
扩展建议
[*]多客户端支持:使用 fork() 或线程处理并发
[*]健壮性增强:添加错误检查、信号处理(如SIGINT)
[*]端口复用:添加 setsockopt(SO_REUSEADDR) 避免重启时"Address already in use"
[*]协议设计:添加消息边界处理(如长度前缀)
完整可运行代码已通过测试(Ubuntu 22.04, gcc 11.4.0)。实际部署时需考虑安全性和性能优化。
https://vk.com/topic-230906111_54610885
https://vk.com/topic-230906111_54610884
https://vk.com/topic-230906111_54610883
https://vk.com/topic-230906111_54610881
https://vk.com/topic-230906111_54610880
https://vk.com/topic-230906111_54610878
https://vk.com/topic-230906111_54610874
https://vk.com/topic-230906111_54610873
https://vk.com/topic-230906111_54610872
https://vk.com/topic-230906111_54610871
https://vk.com/topic-230906111_54610870
https://vk.com/topic-230906111_54610869
https://vk.com/topic-230906111_54610867
https://vk.com/topic-230906111_54610866
https://vk.com/topic-230906111_54610865
https://vk.com/topic-230906111_54610864
https://vk.com/topic-230906111_54610862
https://vk.com/topic-230906111_54610861
https://vk.com/topic-230906111_54610860
https://vk.com/topic-230906111_54610859
https://vk.com/topic-230906111_54610858
https://vk.com/topic-230906111_54610856
https://vk.com/topic-230906111_54610855
https://vk.com/topic-230906111_54610854
https://vk.com/topic-230906111_54610853
https://vk.com/topic-230906111_54610852
https://vk.com/topic-230906111_54610851
https://vk.com/topic-230906111_54610849
https://vk.com/topic-230906111_54610845
https://vk.com/topic-230906111_54610843
https://vk.com/topic-230906111_54610841
https://vk.com/topic-230906111_54610839
https://vk.com/topic-230906111_54610833
https://vk.com/topic-230906223_53515237
https://vk.com/topic-230906223_53515236
https://vk.com/topic-230906223_53515235
https://vk.com/topic-230906223_53515234
https://vk.com/topic-230906223_53515233
https://vk.com/topic-230906223_53515232
https://vk.com/topic-230906223_53515231
https://vk.com/topic-230906223_53515230
https://vk.com/topic-230906223_53515229
https://vk.com/topic-230906223_53515228
https://vk.com/topic-230906223_53515227
https://vk.com/topic-230906223_53515226
https://vk.com/topic-230906223_53515225
https://vk.com/topic-230906223_53515223
https://vk.com/topic-230906223_53515221
https://vk.com/topic-230906223_53515219
https://vk.com/topic-230906223_53515217
https://vk.com/topic-230906223_53515216
https://vk.com/topic-230906223_53515215
https://vk.com/topic-230906223_53515213
https://vk.com/topic-230906223_53515211
https://vk.com/topic-230906223_53515209
https://vk.com/topic-230906223_53515208
https://vk.com/topic-230906223_53515207
https://vk.com/topic-230906223_53515206
https://vk.com/topic-230906223_53515205
https://vk.com/topic-230906223_53515204
https://vk.com/topic-230906223_53515203
https://vk.com/topic-230906223_53515202
https://vk.com/topic-230906223_53515201
https://vk.com/topic-230906223_53515200
https://vk.com/topic-230906223_53515199
https://vk.com/topic-230906223_53515198
https://vk.com/topic-230906223_53515196
https://vk.com/topic-230906223_53515195
https://vk.com/topic-230906223_53515194
https://vk.com/topic-230906223_53515192
https://vk.com/topic-230906223_53515191
https://vk.com/topic-230906223_53515190
https://vk.com/topic-230906223_53515189
https://vk.com/topic-230906223_53515187
https://vk.com/topic-230906223_53515186
https://vk.com/topic-230906223_53515184
https://vk.com/topic-230906223_53515183
https://vk.com/topic-230906223_53515181
https://vk.com/topic-230906223_53515178
https://vk.com/topic-230906223_53515177
https://vk.com/topic-230906223_53515176
https://vk.com/topic-230906223_53515173
https://vk.com/topic-230906223_53515172
https://vk.com/topic-230906223_53515155
https://vk.com/topic-230906223_53515154
https://vk.com/topic-230906223_53515153
https://vk.com/topic-230906223_53515152
https://vk.com/topic-230906223_53515150
https://vk.com/topic-230906223_53515149
https://vk.com/topic-230906223_53515148
https://vk.com/topic-230906223_53515147
https://vk.com/topic-230906223_53515146
https://vk.com/topic-230906223_53515143
https://vk.com/topic-230906223_53515142
https://vk.com/topic-230906223_53515141
https://vk.com/topic-230906223_53515140
https://vk.com/topic-230906223_53515139
https://vk.com/topic-230906223_53515138
https://vk.com/topic-230906223_53515136
https://vk.com/topic-230906223_53515135
https://vk.com/topic-230906223_53515134
https://vk.com/topic-230906223_53515132
https://vk.com/topic-230906223_53515131
https://vk.com/topic-230906223_53515130
https://vk.com/topic-230906223_53515129
https://vk.com/topic-230906223_53515128
https://vk.com/topic-230906223_53515126
https://vk.com/topic-230906223_53515125
https://vk.com/topic-230906223_53515124
https://vk.com/topic-230906223_53515123
https://vk.com/topic-230906223_53515122
https://vk.com/topic-230906223_53515120
https://vk.com/topic-230906223_53515119
https://vk.com/topic-230906223_53515117
https://vk.com/topic-230906223_53515115
https://vk.com/topic-230906223_53515114
https://vk.com/topic-230906223_53515112
https://vk.com/topic-230906223_53515111
https://vk.com/topic-230906223_53515110
https://vk.com/topic-230906223_53515105
https://vk.com/topic-230906223_53515103
https://vk.com/topic-230906223_53515102
https://vk.com/topic-230906223_53515100
https://vk.com/topic-230906223_53515096
https://vk.com/topic-230906223_53515094
https://vk.com/topic-230906223_53515093
https://vk.com/topic-230906223_53515091
https://vk.com/topic-230906223_53515089
https://vk.com/topic-230906223_53515088
https://vk.com/topic-230906223_53515087
https://vk.com/topic-230906223_53515086
https://vk.com/topic-230906223_53515085
https://vk.com/topic-230906223_53515083
https://vk.com/topic-230906223_53515022
https://vk.com/topic-230906223_53515020
https://vk.com/topic-230906223_53515019
https://vk.com/topic-230906223_53515018
https://vk.com/topic-230906223_53515016
https://vk.com/topic-230906223_53515015
https://vk.com/topic-230906223_53515012
https://vk.com/topic-230906223_53515011
https://vk.com/topic-230906223_53515009
https://vk.com/topic-230906223_53515008
https://vk.com/topic-230906223_53515006
https://vk.com/topic-230906223_53515002
https://vk.com/topic-230906223_53515000
https://vk.com/topic-230906223_53514999
https://vk.com/topic-230906223_53514997
https://vk.com/topic-230906223_53514995
https://vk.com/topic-230906223_53514991
https://vk.com/topic-230906223_53514990
https://vk.com/topic-230906223_53514988
https://vk.com/topic-230906223_53514987
https://vk.com/topic-230906223_53514986
https://vk.com/topic-230906223_53514983
https://vk.com/topic-230906223_53514982
https://vk.com/topic-230906223_53514981
https://vk.com/topic-230906223_53514980
https://vk.com/topic-230906223_53514979
https://vk.com/topic-230906223_53514978
https://vk.com/topic-230906223_53514976
https://vk.com/topic-230906223_53514974
https://vk.com/topic-230906223_53514971
https://vk.com/topic-230906223_53514968
https://vk.com/topic-230906223_53514966
https://vk.com/topic-230906223_53514964
https://vk.com/topic-230906223_53514961
https://vk.com/topic-230906223_53514960
https://vk.com/topic-230906223_53514959
https://vk.com/topic-230906223_53514958
https://vk.com/topic-230906223_53514956
https://vk.com/topic-230906223_53514954
https://vk.com/topic-230906223_53514953
https://vk.com/topic-230906223_53514952
https://vk.com/topic-230906223_53514949
https://vk.com/topic-230906223_53514948
https://vk.com/topic-230906223_53514945
https://vk.com/topic-230906223_53514943
https://vk.com/topic-230906223_53514942
https://vk.com/topic-230906551_54611389
https://vk.com/topic-230906551_54611386
https://vk.com/topic-230906551_54611384
https://vk.com/topic-230906551_54611381
https://vk.com/topic-230906551_54611379
https://vk.com/topic-230906551_54611376
https://vk.com/topic-230906551_54611371
https://vk.com/topic-230906551_54611369
https://vk.com/topic-230906551_54611365
https://vk.com/topic-230906551_54611363
https://vk.com/topic-230906551_54611361
https://vk.com/topic-230906551_54611359
https://vk.com/topic-230906551_54611357
https://vk.com/topic-230906551_54611354
https://vk.com/topic-230906551_54611351
https://vk.com/topic-230906551_54611347
https://vk.com/topic-230906551_54611343
https://vk.com/topic-230906551_54611340
https://vk.com/topic-230906551_54611337
https://vk.com/topic-230906551_54611334
https://vk.com/topic-230906551_54611331
https://vk.com/topic-230906551_54611328
https://vk.com/topic-230906551_54611324
https://vk.com/topic-230906551_54611321
https://vk.com/topic-230906551_54611318
https://vk.com/topic-230906551_54611315
https://vk.com/topic-230906551_54611312
https://vk.com/topic-230906551_54611309
https://vk.com/topic-230906551_54611304
https://vk.com/topic-230906551_54611301
https://vk.com/topic-230906551_54611298
https://vk.com/topic-230906551_54611294
https://vk.com/topic-230906551_54611289
https://vk.com/topic-230906551_54611286
https://vk.com/topic-230906551_54611283
https://vk.com/topic-230906551_54611280
https://vk.com/topic-230906551_54611276
https://vk.com/topic-230906551_54611272
https://vk.com/topic-230906551_54611269
https://vk.com/topic-230906551_54611266
https://vk.com/topic-230906551_54611262
https://vk.com/topic-230906551_54611257
https://vk.com/topic-230906551_54611254
https://vk.com/topic-230906551_54611251
https://vk.com/topic-230906551_54611248
https://vk.com/topic-230906551_54611246
https://vk.com/topic-230906551_54611243
https://vk.com/topic-230906551_54611240
https://vk.com/topic-230906551_54611237
https://vk.com/topic-230906551_54611235
https://vk.com/topic-230906551_54611191
https://vk.com/topic-230906551_54611189
https://vk.com/topic-230906551_54611187
https://vk.com/topic-230906551_54611185
https://vk.com/topic-230906551_54611182
https://vk.com/topic-230906551_54611179
https://vk.com/topic-230906551_54611176
https://vk.com/topic-230906551_54611173
https://vk.com/topic-230906551_54611169
https://vk.com/topic-230906551_54611166
https://vk.com/topic-230906551_54611160
https://vk.com/topic-230906551_54611156
https://vk.com/topic-230906551_54611152
https://vk.com/topic-230906551_54611149
https://vk.com/topic-230906551_54611146
https://vk.com/topic-230906551_54611143
https://vk.com/topic-230906551_54611140
https://vk.com/topic-230906551_54611136
https://vk.com/topic-230906551_54611133
https://vk.com/topic-230906551_54611130
https://vk.com/topic-230906551_54611126
https://vk.com/topic-230906551_54611121
https://vk.com/topic-230906551_54611118
https://vk.com/topic-230906551_54611115
https://vk.com/topic-230906551_54611111
https://vk.com/topic-230906551_54611107
https://vk.com/topic-230906551_54611104
https://vk.com/topic-230906551_54611101
https://vk.com/topic-230906551_54611096
https://vk.com/topic-230906551_54611092
https://vk.com/topic-230906551_54611089
https://vk.com/topic-230906551_54611085
https://vk.com/topic-230906551_54611080
https://vk.com/topic-230906551_54611077
https://vk.com/topic-230906551_54611074
https://vk.com/topic-230906551_54611071
https://vk.com/topic-230906551_54611067
https://vk.com/topic-230906551_54611064
https://vk.com/topic-230906551_54611060
https://vk.com/topic-230906551_54611057
https://vk.com/topic-230906551_54611054
https://vk.com/topic-230906551_54611049
https://vk.com/topic-230906551_54611047
https://vk.com/topic-230906551_54611043
https://vk.com/topic-230906551_54611041
https://vk.com/topic-230906551_54611039
https://vk.com/topic-230906551_54611037
https://vk.com/topic-230906551_54611035
https://vk.com/topic-230906551_54611033
https://vk.com/topic-230906551_54611031
https://vk.com/topic-230906551_54610985
https://vk.com/topic-230906551_54610984
https://vk.com/topic-230906551_54610983
https://vk.com/topic-230906551_54610982
https://vk.com/topic-230906551_54610981
https://vk.com/topic-230906551_54610980
https://vk.com/topic-230906551_54610979
https://vk.com/topic-230906551_54610977
https://vk.com/topic-230906551_54610976
https://vk.com/topic-230906551_54610975
https://vk.com/topic-230906551_54610973
https://vk.com/topic-230906551_54610970
https://vk.com/topic-230906551_54610968
https://vk.com/topic-230906551_54610965
https://vk.com/topic-230906551_54610963
https://vk.com/topic-230906551_54610960
https://vk.com/topic-230906551_54610958
https://vk.com/topic-230906551_54610957
https://vk.com/topic-230906551_54610955
https://vk.com/topic-230906551_54610954
https://vk.com/topic-230906551_54610952
https://vk.com/topic-230906551_54610950
页:
[1]