2025-8-29 03:02:08 [显示全部楼层]
8浏览
查看: 8|回复: 0

[求助问答] 这个例子没有考虑多客户端并发,一次只能处理一个客...

[复制链接]
本帖最后由 傲世堂 于 2025-8-29 03:11 编辑


在Linux下创建一个简单的TCP服务器,我们可以使用C语言和基本的socket编程。
以下是一个简单的TCP服务器程序,它监听指定端口,接受客户端连接,然后接收客户端发来的数据并原样发回(echo server)。

步骤:
1. 创建socket
2. 绑定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[BUFFER_SIZE] = {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. 监听连接,设置最大等待连接数为3
if (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_ADDRSTRLEN];
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`,然后输入一些文本,服务器会将文本回显。
*/


[size=16.002px]
[size=16.002px]在Linux下创建一个简单的TCP服务器,可以使用C语言和标准Socket API实现。以下是一个基础示例,该服务器监听指定端口,接收客户端连接,并将收到的消息原样返回(回显服务器):
[size=16.002px]代码示例:tcp_echo_server.c[size=12.573px][size=12.573px]c
[td]http://my.tv.sohu.com/us/439633867/668987950.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4Nzk1MC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987854.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4Nzg1NC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987932.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzkzMi5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987927.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzkyNy5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987754.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4Nzc1NC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987750.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4Nzc1MC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987915.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzkxNS5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987826.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzgyNi5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987649.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzY0OS5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987642.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzY0Mi5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987807.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzgwNy5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987576.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzU3Ni5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987631.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzYzMS5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987722.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzcyMi5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987490.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzQ5MC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987702.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzcwMi5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987488.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzQ4OC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987609.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzYwOS5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987605.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzYwNS5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987603.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzYwMy5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987382.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzM4Mi5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987298.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzI5OC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987289.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzI4OS5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987461.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzQ2MS5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987360.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzM2MC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987275.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzI3NS5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987270.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzI3MC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987261.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzI2MS5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987438.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzQzOC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987434.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzQzNC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987503.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzUwMy5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987325.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzMyNS5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987320.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzMyMC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987415.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzQxNS5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987071.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzA3MS5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668986993.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4Njk5My5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668986988.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4Njk4OC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987059.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzA1OS5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668986978.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4Njk3OC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668986977.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4Njk3Ny5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668986973.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4Njk3My5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668987157.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4NzE1Ny5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668986970.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4Njk3MC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668986884.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4Njg4NC5zaHRtbA==.htmlhttp://my.tv.sohu.com/us/439633867/668986956.shtmlhttps://tv.sohu.com/v/dXMvNDM5NjMzODY3LzY2ODk4Njk1Ni5zaHRtbA==.html

[/td]
http://my.tv.sohu.com/us/439633584/668989200.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTIwMC5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989199.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTE5OS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989345.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTM0NS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989282.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTI4Mi5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989279.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTI3OS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989275.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTI3NS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989331.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTMzMS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989185.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTE4NS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989270.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTI3MC5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989180.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTE4MC5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989266.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTI2Ni5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989263.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTI2My5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989261.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTI2MS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989323.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTMyMy5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989259.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTI1OS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989172.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTE3Mi5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989321.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTMyMS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989253.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTI1My5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989094.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTA5NC5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989093.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTA5My5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989245.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTI0NS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989312.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTMxMi5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989311.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTMxMS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989302.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTMwMi5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989151.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTE1MS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988997.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODk5Ny5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988995.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODk5NS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989069.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTA2OS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989225.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTIyNS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988978.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODk3OC5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989212.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTIxMi5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988966.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODk2Ni5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988897.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODg5Ny5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988890.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODg5MC5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988888.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODg4OC5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988880.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODg4MC5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668989025.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4OTAyNS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988869.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODg2OS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988689.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODY4OS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988798.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODc5OC5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988853.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODg1My5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988851.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODg1MS5zaHRtbA==.html
http://my.tv.sohu.com/us/439633584/668988849.shtml
https://tv.sohu.com/v/dXMvNDM5NjMzNTg0LzY2ODk4ODg0OS5zaHRtbA==.html

高级模式
B Color Image Link Quote Code Smilies |上传

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail