0
我的代碼接受傳入連接並將它們存儲在一個數組中。連接列表在C編程中顯示0.0.0.0
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];
我在做什麼錯。
那真是尷尬。但是就像事後想想的那樣,你能幫我從ip地址獲取主機名嗎? – Antithesis
使用gethostbyaddr?另外,請檢查以下問題:http://stackoverflow.com/questions/10082787/how-can-i-get-a-hostname-from-an-ipv4-address-in-c – Ashalynd