C语言,TCP服务器和客户端
language C, TCP Server and Client
我正在尝试创建一个 tcpServer 和 tcpClient 消息终端。我 运行 遇到一些代码问题,我无法开始工作
我在这里遵循本指南https://www.youtube.com/watch?v=BIJGSQEipEE
int newSocket = accept(sockfd, (struct sockaddr*)&newAddr, sizeof(newAddr));
printf("Connection accepted from %s:%d\n", inet_ntoa(newAddr.sin_addr), ntoa(newAddr.sin_port));
我的错误代码是:
tcpServer.c:50:64: warning: incompatible integer to pointer conversion passing 'unsigned long' to parameter of type 'socklen_t *' (aka 'unsigned int *') [-Wint-conversion]
newSocket = accept(sockfd, (struct sockaddr*)&newAddr, sizeof(newAddr));
^~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/sys/socket.h:686:73: note: passing argument to parameter here
int accept(int, struct sockaddr * __restrict, socklen_t * __restrict)
^
tcpServer.c:54:81: warning: implicit declaration of function 'ntoa' is invalid in C99 [-Wimplicit-function-declaration]
printf("Connection accepted from %s:%d\n", inet_ntoa(newAddr.sin_addr), ntoa(newAddr.sin_port));
为什么会出现错误2?
谢谢你帮助我:)
accept
的原型是这样的:
int accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);
(来自Open Group Base Specification Issue 7)
address_len
参数是指向 socklen_t
对象的指针,而不是数字长度。
相反,您应该创建一个类型为 socklen_t
的变量,并为其指定要传递的大小:
socklen_t newAddrLength = sizeof(newAddr);
然后您可以在对 accept
:
的调用中传递指向此变量的指针
newSocket = accept(sockfd, (struct sockaddr*)&newAddr, &newAddrLength);
我不确定你想用 ntoa
做什么。我相信您想在其位置使用的函数是 ntohs
,它将 sin_port
成员的网络顺序转换为主机顺序 uint16_t
:
ntohs(newAddr.sin_port))
然后您需要将其转换为标准整数类型,或者在 <inttypes.h>
:
中使用 PRIu16
格式宏
#include <inttypes.h>
printf("Connection accepted from %s:%" PRIu16 "\n", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
我正在尝试创建一个 tcpServer 和 tcpClient 消息终端。我 运行 遇到一些代码问题,我无法开始工作
我在这里遵循本指南https://www.youtube.com/watch?v=BIJGSQEipEE
int newSocket = accept(sockfd, (struct sockaddr*)&newAddr, sizeof(newAddr));
printf("Connection accepted from %s:%d\n", inet_ntoa(newAddr.sin_addr), ntoa(newAddr.sin_port));
我的错误代码是:
tcpServer.c:50:64: warning: incompatible integer to pointer conversion passing 'unsigned long' to parameter of type 'socklen_t *' (aka 'unsigned int *') [-Wint-conversion]
newSocket = accept(sockfd, (struct sockaddr*)&newAddr, sizeof(newAddr));
^~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/sys/socket.h:686:73: note: passing argument to parameter here
int accept(int, struct sockaddr * __restrict, socklen_t * __restrict)
^
tcpServer.c:54:81: warning: implicit declaration of function 'ntoa' is invalid in C99 [-Wimplicit-function-declaration]
printf("Connection accepted from %s:%d\n", inet_ntoa(newAddr.sin_addr), ntoa(newAddr.sin_port));
为什么会出现错误2?
谢谢你帮助我:)
accept
的原型是这样的:
int accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);
(来自Open Group Base Specification Issue 7)
address_len
参数是指向 socklen_t
对象的指针,而不是数字长度。
相反,您应该创建一个类型为 socklen_t
的变量,并为其指定要传递的大小:
socklen_t newAddrLength = sizeof(newAddr);
然后您可以在对 accept
:
newSocket = accept(sockfd, (struct sockaddr*)&newAddr, &newAddrLength);
我不确定你想用 ntoa
做什么。我相信您想在其位置使用的函数是 ntohs
,它将 sin_port
成员的网络顺序转换为主机顺序 uint16_t
:
ntohs(newAddr.sin_port))
然后您需要将其转换为标准整数类型,或者在 <inttypes.h>
:
PRIu16
格式宏
#include <inttypes.h>
printf("Connection accepted from %s:%" PRIu16 "\n", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));