winsock 用accept函数不停收到204.204.204.204的地址
winsock 用accept函数不断收到204.204.204.204的地址网上找了一圈也没有答案关键代码如下:int addr_sizes
winsock 用accept函数不断收到204.204.204.204的地址
网上找了一圈也没有答案
关键代码如下:
int addr_size=sizeof(sockaddr_in);
SOCKET newSocket;
while(true)
{
newSocket=accept(sSocket,(LPSOCKADDR)&clientAddr,&addr_size);
if(newSocket<0)
{
printf("Accept Error!\n");
return 1;
}
printf("there is a connection from %s\n",inet_ntoa(clientAddr.sin_addr));
下略……
运行时不断收到204.204.204.204的地址
求解惑
[解决办法]inet_ntoa
The Windows Sockets inet_ntoa function converts an (Ipv4) Internet network address into a string in Internet standard dotted format.
char FAR * inet_ntoa (
struct in_addr in
);
Parameters
in
[in] A structure that represents an Internet host address.
Remarks
The inet_ntoa function takes an Internet address structure specified by the in parameter and returns an ASCII string representing the address in ".'' (dot) notation as in "a.b.c.d''. The string returned by inet_ntoa resides in memory that is allocated by Windows Sockets. The application should not make any assumptions about the way in which the memory is allocated. The data is guaranteed to be valid until the next Windows Sockets function call within the same thread, but no longer. Therefore, the data should be copied before another Windows Sockets call is made.
Return Values
If no error occurs, inet_ntoa returns a char pointer to a static buffer containing the text address in standard ".'' notation. Otherwise, it returns NULL.
QuickInfo
Windows NT: Yes
Windows: Yes
Windows CE: Use version 1.0 and later.
Header: Declared in winsock2.h.
Import Library: Link with ws2_32.lib.
See Also
inet_addr