当前位置: 技术问答>linux和unix
socket inet_ntoa返回客户端地址字符串的问题!
来源: 互联网 发布时间:2016-07-12
本文导语: iConnFd = accept(iListenFd, (struct sockaddr *)(&vCliAddr), &vCliLen); memset(sClientIp,0,PARALEN); strcpy( sClientIp, inet_ntoa(vCliAddr.sin_addr) if( strstr(gMgrData->vClientAddr,EVERYTYPE) || strstr(gMgrData->vClientAddr,sClientIp) ) { ...
iConnFd = accept(iListenFd, (struct sockaddr *)(&vCliAddr), &vCliLen);
memset(sClientIp,0,PARALEN);
strcpy( sClientIp, inet_ntoa(vCliAddr.sin_addr)
if( strstr(gMgrData->vClientAddr,EVERYTYPE) || strstr(gMgrData->vClientAddr,sClientIp) )
{
debugLog("client ip is [%s]n",sClientIp);
}
else
{
errorLog("client ip is [%s] can not connect!n",sClientIp);
write(iConnFd,conErrStr,52);
close(iConnFd);
continue;
}
返回的地址字符串是0.0.0.0这是为什么?怎样修改能显示客户端的ip地址字符串
memset(sClientIp,0,PARALEN);
strcpy( sClientIp, inet_ntoa(vCliAddr.sin_addr)
if( strstr(gMgrData->vClientAddr,EVERYTYPE) || strstr(gMgrData->vClientAddr,sClientIp) )
{
debugLog("client ip is [%s]n",sClientIp);
}
else
{
errorLog("client ip is [%s] can not connect!n",sClientIp);
write(iConnFd,conErrStr,52);
close(iConnFd);
continue;
}
返回的地址字符串是0.0.0.0这是为什么?怎样修改能显示客户端的ip地址字符串
|
检查accept的返回值
|
变量不初始化就使用,当然有问题
accept之前加
vCliLen = sizeof(vCliAddr);
The argument addr is a result argument that is filled-in with the address
of the connecting entity, as known to the communications layer. The
exact format of the addr argument is determined by the domain in which
the communication is occurring. A null pointer may be specified for addr
if the address information is not desired; in this case, addrlen is not
used and should also be null. Otherwise, the addrlen argument is a
value-result argument; it should initially contain the amount of space
pointed to by addr; on return it will contain the actual length (in
bytes) of the address returned. This call is used with connection-based
socket types, currently with SOCK_STREAM.
accept之前加
vCliLen = sizeof(vCliAddr);
The argument addr is a result argument that is filled-in with the address
of the connecting entity, as known to the communications layer. The
exact format of the addr argument is determined by the domain in which
the communication is occurring. A null pointer may be specified for addr
if the address information is not desired; in this case, addrlen is not
used and should also be null. Otherwise, the addrlen argument is a
value-result argument; it should initially contain the amount of space
pointed to by addr; on return it will contain the actual length (in
bytes) of the address returned. This call is used with connection-based
socket types, currently with SOCK_STREAM.