我有用 C 编写的服务器和客户端程序,它工作正常,但我无法找出下面解释的行为的原因:
服务器.c
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9374);
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
getsockname (server_sockfd, (struct sockaddr *)&server_address, &server_len);
printf("server port = %d\n", server_address.sin_port);
printf("Server Waiting......\n");
listen(server_sockfd, 5);
客户端.c
address.sin_family = AF_INET;
address.sin_port = htons(9374);
int length, result;
length = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, length);
getsockname(sockfd, (struct sockaddr *)&address, &length);
printf("Connecting to Port = %d \n", address.sin_port);
在服务器端输出:
[root@dhcppc1 Socket]# ./server
server port = 40484
Server Waiting......
客户端输出:
[root@dhcppc1 Socket]# ./client
Connecting to Port = 18576
我的问题是:
虽然9374
在服务器和客户端代码中实现了相同的端口号(