我的代码接受传入的连接并将它们存储在数组中。
if(i == sockServer){
//handle incoming connection
addrlen = sizeof(remoteaddr);
sockClient = accept(sockServer, (struct sockaddr*)&remoteaddr, &addrlen);
if(sockClient < 0){
perror("Accept failed: ");
}else{
FD_SET(sockClient, &master);
if(sockClient > fdmax){
fdmax = sockClient;
}
IpList[connectionCount] = remoteaddr;
connectionCount++;
当我尝试对数组进行迭代并打印出连接的IP地址和端口号时,后面的代码得到以下内容。
for (int i = 0; i < connectionCount; ++i)
{
struct sockaddr_in tempSock = IpList[connectionCount];
printf("%d %s %d\n", connectionCount, inet_ntoa(tempSock.sin_addr), ntohs(tempSock.sin_port));
}
0 0.0.0.0 0
1 0.0.0.0 0
数组是静态分配的数组
struct sockaddr_in IpList[256];
我究竟做错了什么。
更改此设置(在打印连接的周期中):
struct sockaddr_in tempSock = IpList[connectionCount];
对此:
struct sockaddr_in tempSock = IpList[i];
应该希望解决它:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句