2025-8-30 01:08:16 [显示全部楼层]
9浏览
查看: 9|回复: 0

[求助问答] ckaddr_in 结构指定监听端口和网卡(INADDR_ANY 表示所有网卡

[复制链接]

在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实现。以下是一个基础示例,该服务器监听指定端口,接收客户端连接,并将收到的消息原样返回(回显服务器):
代码示例:tcp_echo_server.c[size=12.573px][size=12.573px]c


复制



下载






#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;}


编译与运行
  • [size=16.002px]保存代码:将代码保存为 tcp_echo_server.c
  • [size=16.002px]编译:
    [size=12.573px][size=12.573px]bash


    复制



    下载






    gcc tcp_echo_server.c -o server
  • [size=16.002px]运行服务器:
    [size=12.573px][size=12.573px]bash


    复制



    下载






    ./server

测试服务器
[size=16.002px]使用 netcat (nc) 工具连接服务器:
[size=12.573px][size=12.573px]bash


复制



下载






nc localhost 8080
  • [size=16.002px]输入任意文本,服务器会立即返回相同内容
  • [size=16.002px]按 Ctrl+C 断开连接


关键步骤解析
  • [size=16.002px]创建Socket
    socket(AF_INET, SOCK_STREAM, 0) 创建TCP协议Socket
  • [size=16.002px]地址绑定
    sockaddr_in 结构指定监听端口和网卡(INADDR_ANY 表示所有网卡)
  • [size=16.002px]监听连接
    listen() 启用连接队列(此处队列长度=3)
  • [size=16.002px]接受连接
    accept() 阻塞等待客户端连接,返回新Socket用于通信
  • [size=16.002px]数据交换
    read() 接收数据 → send() 原样返回


https://m.ximalaya.com/sound/905201041?735=442
https://m.ximalaya.com/sound/905201041?537=69
https://m.ximalaya.com/sound/905201041?268=812
https://m.ximalaya.com/sound/905201041?000=74
https://m.ximalaya.com/sound/905201032?542=181
https://m.ximalaya.com/sound/905201032?336=84
https://m.ximalaya.com/sound/905201032?231=968
https://m.ximalaya.com/sound/905201032?694=54
https://m.ximalaya.com/sound/905201027?936=071
https://m.ximalaya.com/sound/905201027?653=48
https://m.ximalaya.com/sound/905201027?443=066
https://m.ximalaya.com/sound/905201027?735=97
https://m.ximalaya.com/sound/905201022?979=663
https://m.ximalaya.com/sound/905201022?423=44
https://m.ximalaya.com/sound/905201022?375=864
https://m.ximalaya.com/sound/905201022?735=15
https://m.ximalaya.com/sound/905201017?373=748
https://m.ximalaya.com/sound/905201017?881=72
https://m.ximalaya.com/sound/905201017?483=772
https://m.ximalaya.com/sound/905201017?778=90
https://m.ximalaya.com/sound/905201011?616=645
https://m.ximalaya.com/sound/905201011?187=95
https://m.ximalaya.com/sound/905201011?931=586
https://m.ximalaya.com/sound/905201011?969=29
https://m.ximalaya.com/sound/905201006?758=743
https://m.ximalaya.com/sound/905201006?188=13
https://m.ximalaya.com/sound/905201006?993=705
https://m.ximalaya.com/sound/905201006?445=96
https://m.ximalaya.com/sound/905201003?137=659
https://m.ximalaya.com/sound/905201003?744=07
https://m.ximalaya.com/sound/905201003?613=438
https://m.ximalaya.com/sound/905201003?360=06
https://m.ximalaya.com/sound/905201000?814=906
https://m.ximalaya.com/sound/905201000?087=72
https://m.ximalaya.com/sound/905201000?587=246
https://m.ximalaya.com/sound/905201000?402=59
https://m.ximalaya.com/sound/905200995?752=666
https://m.ximalaya.com/sound/905200995?411=14
https://m.ximalaya.com/sound/905200995?475=293
https://m.ximalaya.com/sound/905200995?753=68
https://m.ximalaya.com/sound/905200990?453=311
https://m.ximalaya.com/sound/905200990?020=11
https://m.ximalaya.com/sound/905200990?975=296
https://m.ximalaya.com/sound/905200990?606=18
https://m.ximalaya.com/sound/905200988?578=243
https://m.ximalaya.com/sound/905200988?889=62
https://m.ximalaya.com/sound/905200988?783=996
https://m.ximalaya.com/sound/905200988?938=90
https://m.ximalaya.com/sound/905200982?328=497
https://m.ximalaya.com/sound/905200982?776=45
https://m.ximalaya.com/sound/905200982?313=754
https://m.ximalaya.com/sound/905200982?538=79
https://m.ximalaya.com/sound/905200976?859=415
https://m.ximalaya.com/sound/905200976?847=99
https://m.ximalaya.com/sound/905200976?104=784
https://m.ximalaya.com/sound/905200976?247=29
https://m.ximalaya.com/sound/905200972?123=935
https://m.ximalaya.com/sound/905200972?965=60
https://m.ximalaya.com/sound/905200972?486=079
https://m.ximalaya.com/sound/905200972?705=62
https://m.ximalaya.com/sound/905200969?260=008
https://m.ximalaya.com/sound/905200969?301=28
https://m.ximalaya.com/sound/905200969?297=376
https://m.ximalaya.com/sound/905200969?182=84
https://m.ximalaya.com/sound/905200965?406=632
https://m.ximalaya.com/sound/905200965?254=79
https://m.ximalaya.com/sound/905200965?779=693
https://m.ximalaya.com/sound/905200965?338=67
https://m.ximalaya.com/sound/905200963?619=719
https://m.ximalaya.com/sound/905200963?371=56
https://m.ximalaya.com/sound/905200963?742=453
https://m.ximalaya.com/sound/905200963?293=36
https://m.ximalaya.com/sound/905200958?523=712
https://m.ximalaya.com/sound/905200958?138=73
https://m.ximalaya.com/sound/905200958?789=146
https://m.ximalaya.com/sound/905200958?375=25
https://m.ximalaya.com/sound/905200955?776=471
https://m.ximalaya.com/sound/905200955?368=03
https://m.ximalaya.com/sound/905200955?049=886
https://m.ximalaya.com/sound/905200955?959=32
https://m.ximalaya.com/sound/905200953?159=331
https://m.ximalaya.com/sound/905200953?923=28
https://m.ximalaya.com/sound/905200953?345=166
https://m.ximalaya.com/sound/905200953?064=14
https://m.ximalaya.com/sound/905200949?447=668
https://m.ximalaya.com/sound/905200949?153=86
https://m.ximalaya.com/sound/905200949?951=174
https://m.ximalaya.com/sound/905200949?556=61
https://m.ximalaya.com/sound/905200947?470=182
https://m.ximalaya.com/sound/905200947?174=31
https://m.ximalaya.com/sound/905200947?598=077
https://m.ximalaya.com/sound/905200947?412=54
https://m.ximalaya.com/sound/905200943?352=849
https://m.ximalaya.com/sound/905200943?083=55
https://m.ximalaya.com/sound/905200943?864=115
https://m.ximalaya.com/sound/905200943?077=11
https://m.ximalaya.com/sound/905200941?796=980
https://m.ximalaya.com/sound/905200941?412=02
https://m.ximalaya.com/sound/905200941?390=005
https://m.ximalaya.com/sound/905200941?486=92
https://m.ximalaya.com/sound/905200933?596=364
https://m.ximalaya.com/sound/905200933?876=70
https://m.ximalaya.com/sound/905200933?815=535
https://m.ximalaya.com/sound/905200933?990=92
https://m.ximalaya.com/sound/905200929?937=597
https://m.ximalaya.com/sound/905200929?521=32
https://m.ximalaya.com/sound/905200929?960=210
https://m.ximalaya.com/sound/905200929?254=00
https://m.ximalaya.com/sound/905200922?649=119
https://m.ximalaya.com/sound/905200922?223=59
https://m.ximalaya.com/sound/905200922?631=405
https://m.ximalaya.com/sound/905200922?824=92
https://m.ximalaya.com/sound/905200918?807=664
https://m.ximalaya.com/sound/905200918?361=21
https://m.ximalaya.com/sound/905200918?648=824
https://m.ximalaya.com/sound/905200918?220=25
https://m.ximalaya.com/sound/905200911?076=883
https://m.ximalaya.com/sound/905200911?664=87
https://m.ximalaya.com/sound/905200911?108=863
https://m.ximalaya.com/sound/905200911?245=64
https://m.ximalaya.com/sound/905200907?255=552
https://m.ximalaya.com/sound/905200907?237=87
https://m.ximalaya.com/sound/905200907?227=260
https://m.ximalaya.com/sound/905200907?555=33
https://m.ximalaya.com/sound/905200901?420=264
https://m.ximalaya.com/sound/905200901?227=32
https://m.ximalaya.com/sound/905200901?045=697
https://m.ximalaya.com/sound/905200901?600=00
https://m.ximalaya.com/sound/905200894?751=336
https://m.ximalaya.com/sound/905200894?580=74
https://m.ximalaya.com/sound/905200894?590=704
https://m.ximalaya.com/sound/905200894?635=32
https://m.ximalaya.com/sound/905200887?703=446
https://m.ximalaya.com/sound/905200887?282=16
https://m.ximalaya.com/sound/905200887?638=486
https://m.ximalaya.com/sound/905200887?346=63
https://m.ximalaya.com/sound/905200882?342=374
https://m.ximalaya.com/sound/905200882?250=92
https://m.ximalaya.com/sound/905200882?771=882
https://m.ximalaya.com/sound/905200882?931=58
https://m.ximalaya.com/sound/905200874?280=280
https://m.ximalaya.com/sound/905200874?287=57
https://m.ximalaya.com/sound/905200874?810=044
https://m.ximalaya.com/sound/905200874?419=33
https://m.ximalaya.com/sound/905200870?055=445
https://m.ximalaya.com/sound/905200870?703=10
https://m.ximalaya.com/sound/905200870?375=298
https://m.ximalaya.com/sound/905200870?348=28
https://m.ximalaya.com/sound/905200864?404=032
https://m.ximalaya.com/sound/905200864?801=74
https://m.ximalaya.com/sound/905200864?634=964
https://m.ximalaya.com/sound/905200864?661=64
https://m.ximalaya.com/sound/905200860?726=145
https://m.ximalaya.com/sound/905200860?879=15
https://m.ximalaya.com/sound/905200860?007=337
https://m.ximalaya.com/sound/905200860?575=84
https://m.ximalaya.com/sound/905200856?806=849
https://m.ximalaya.com/sound/905200856?960=58
https://m.ximalaya.com/sound/905200856?488=220
https://m.ximalaya.com/sound/905200856?040=76
https://m.ximalaya.com/sound/905200851?873=292
https://m.ximalaya.com/sound/905200851?608=58
https://m.ximalaya.com/sound/905200851?032=975
https://m.ximalaya.com/sound/905200851?660=69
https://m.ximalaya.com/sound/905200845?720=119
https://m.ximalaya.com/sound/905200845?379=98
https://m.ximalaya.com/sound/905200845?778=075
https://m.ximalaya.com/sound/905200845?695=27
https://m.ximalaya.com/sound/905200841?106=331
https://m.ximalaya.com/sound/905200841?889=92
https://m.ximalaya.com/sound/905200841?390=346
https://m.ximalaya.com/sound/905200841?872=45
https://m.ximalaya.com/sound/905200835?149=232
https://m.ximalaya.com/sound/905200835?853=41
https://m.ximalaya.com/sound/905200835?661=290
https://m.ximalaya.com/sound/905200835?523=33
https://m.ximalaya.com/sound/905200828?981=556
https://m.ximalaya.com/sound/905200828?155=19
https://m.ximalaya.com/sound/905200828?907=082
https://m.ximalaya.com/sound/905200828?064=33
https://m.ximalaya.com/sound/905200824?548=739
https://m.ximalaya.com/sound/905200824?550=92
https://m.ximalaya.com/sound/905200824?237=442
https://m.ximalaya.com/sound/905200824?416=23
https://m.ximalaya.com/sound/905200816?105=391
https://m.ximalaya.com/sound/905200816?579=05
https://m.ximalaya.com/sound/905200816?950=386
https://m.ximalaya.com/sound/905200816?089=47
https://m.ximalaya.com/sound/905200810?823=416
https://m.ximalaya.com/sound/905200810?601=11
https://m.ximalaya.com/sound/905200810?288=816
https://m.ximalaya.com/sound/905200810?057=51
https://m.ximalaya.com/sound/905200804?160=446
https://m.ximalaya.com/sound/905200804?005=32
https://m.ximalaya.com/sound/905200804?333=605
https://m.ximalaya.com/sound/905200804?962=21
https://m.ximalaya.com/sound/905200796?229=645
https://m.ximalaya.com/sound/905200796?745=57
https://m.ximalaya.com/sound/905200796?120=118
https://m.ximalaya.com/sound/905200796?668=88
https://m.ximalaya.com/sound/905200792?577=782
https://m.ximalaya.com/sound/905200792?179=54
https://m.ximalaya.com/sound/905200792?927=114
https://m.ximalaya.com/sound/905200792?355=57
https://m.ximalaya.com/sound/905200783?941=345
https://m.ximalaya.com/sound/905200783?586=93
https://m.ximalaya.com/sound/905200783?667=167
https://m.ximalaya.com/sound/905200783?031=96
https://m.ximalaya.com/sound/905200777?043=713
https://m.ximalaya.com/sound/905200777?296=28
https://m.ximalaya.com/sound/905200777?220=030
https://m.ximalaya.com/sound/905200777?907=15
https://m.ximalaya.com/sound/905200772?316=675
https://m.ximalaya.com/sound/905200772?338=17
https://m.ximalaya.com/sound/905200772?290=823
https://m.ximalaya.com/sound/905200772?053=52
https://m.ximalaya.com/sound/905200764?201=823
https://m.ximalaya.com/sound/905200764?261=12
https://m.ximalaya.com/sound/905200764?633=300
https://m.ximalaya.com/sound/905200764?070=75
https://m.ximalaya.com/sound/905200757?727=730
https://m.ximalaya.com/sound/905200757?403=75
https://m.ximalaya.com/sound/905200757?674=412
https://m.ximalaya.com/sound/905200757?926=66




高级模式
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