为什么 recvfrom 在 UDP 消息上存储 0.0.0.0 的 ip 地址?
Why does recvfrom store an ip address of 0.0.0.0 on UDP message?
我正在读取 recvfrom() 的 'from' 参数中存储的 0.0.0.0 的 ip 地址,我不确定为什么。消息通过得很好,'fromSockAddrIn' 中存储了一些信息。
代码:
int UDPSock = 0; //response from creating sock
int bindRes = 0; //response from binding
uint8_t *errCount = calloc(1, sizeof(uint8_t*)); //allocate memory for counting errors
size_t recvBytes = 0; //n of bytes received from 'recvfrom'
void * buff[MAX_MSG_SIZE] = {0}; //message storage from 'recvfrom'
size_t len = sizeof(buff); //size of buffer
int afFamily = AF_INET;
char fromIp[INET_ADDRSTRLEN] = {0};
struct sockaddr_in mySockAddrIn; //Socket address, internet style
memset(&mySockAddrIn, 0, sizeof(mySockAddrIn)); //allocate memory for socket address
mySockAddrIn.sin_family = afFamily; //save address family
mySockAddrIn.sin_port = htons(8004); //convert numeric port number to network byte-order
struct sockaddr_in *fromSockAddrIn = calloc(1, sizeof(fromSockAddrIn)); //Socket address, Internet style
socklen_t fromLen = sizeof(fromSockAddrIn); //get size of socket address
*errCount = 0;
do{ //get a descripter referencing the socket while checking for errors
UDPSock = socket(afFamily, SOCK_DGRAM, IPPROTO_UDP);
Check_Print_Error(UDPSock, __func__, "socket", errCount);
}while(UDPSock == -1);
*errCount = 0;
do{ //Bind while checking for errors
bindRes = bind(UDPSock, (struct sockaddr *) &mySockAddrIn, sizeof(mySockAddrIn));
Check_Print_Error(bindRes, __func__, "bind", errCount);
}while(bindRes == -1);
do{ //block until a message is received while checking for errors
fromLen = sizeof(fromSockAddrIn);
recvBytes = recvfrom(UDPSock, buff, len, 0,(struct sockaddr *) fromSockAddrIn, &fromLen);
Check_Print_Error(recvBytes, __func__, "recvfrom", errCount);
printf("received %d bytes on UDP: %s ", recvBytes, buff);
inet_ntop(afFamily, &fromSockAddrIn->sin_addr, fromIp, sizeof(fromIp));
printf("from ip: %s \n", fromIp);
}while(recvBytes > 0);
Check_Print_Error:
void Check_Print_Error(int errNum, const char *func1, char *func2, uint8_t *errCount){
if(errNum == -1){
errCount += 1;
fprintf(stderr, "%s, %s error: (%d) %s \n", func1, func2, errno, strerror(errno));
}
if(*errCount >= 10){
fprintf(stderr, "%s tried %s too many times (%u)\nExiting...", func1, func2, *errCount);
fflush(stdout);
fflush(stderr);
exit(EXIT_FAILURE);
}
return;
}
python 发送消息的程序:
import socket
import time
toIPAddr = "192.168.202.128"
port = 8004
message = "ping from python"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
toAddr = (toIPAddr, port)
print "toAddr:", toAddr
for x in range(0,6):
print "sending...'",message,"' size of", len(message)
sock.sendto(message , toAddr)
time.sleep(1)
我的输出:
received 16 bytes on UDP: ping from python from ip: 0.0.0.0
感谢您的帮助。
您在这段代码中对 sizeof(fromSockAddrIn)
的使用到处都是错误的
struct sockaddr_in *fromSockAddrIn = calloc(1, sizeof(fromSockAddrIn));
这只是为指针分配空间。你需要得到一个struct sockardd_in
的大小,所以
struct sockaddr_in *fromSockAddrIn = calloc(1, sizeof(*fromSockAddrIn));
类似你计算fromLen = sizeof(fromSockAddrIn);
,那应该是fromLen = sizeof(*fromSockAddrIn);
看来您不需要动态分配 fromSockAddrIn
变量。如果您只是将其声明为 struct sockaddr_in fromSockAddrIn;
,则可以使用 sizeof(mySockAddrIn)
。
我正在读取 recvfrom() 的 'from' 参数中存储的 0.0.0.0 的 ip 地址,我不确定为什么。消息通过得很好,'fromSockAddrIn' 中存储了一些信息。
代码:
int UDPSock = 0; //response from creating sock
int bindRes = 0; //response from binding
uint8_t *errCount = calloc(1, sizeof(uint8_t*)); //allocate memory for counting errors
size_t recvBytes = 0; //n of bytes received from 'recvfrom'
void * buff[MAX_MSG_SIZE] = {0}; //message storage from 'recvfrom'
size_t len = sizeof(buff); //size of buffer
int afFamily = AF_INET;
char fromIp[INET_ADDRSTRLEN] = {0};
struct sockaddr_in mySockAddrIn; //Socket address, internet style
memset(&mySockAddrIn, 0, sizeof(mySockAddrIn)); //allocate memory for socket address
mySockAddrIn.sin_family = afFamily; //save address family
mySockAddrIn.sin_port = htons(8004); //convert numeric port number to network byte-order
struct sockaddr_in *fromSockAddrIn = calloc(1, sizeof(fromSockAddrIn)); //Socket address, Internet style
socklen_t fromLen = sizeof(fromSockAddrIn); //get size of socket address
*errCount = 0;
do{ //get a descripter referencing the socket while checking for errors
UDPSock = socket(afFamily, SOCK_DGRAM, IPPROTO_UDP);
Check_Print_Error(UDPSock, __func__, "socket", errCount);
}while(UDPSock == -1);
*errCount = 0;
do{ //Bind while checking for errors
bindRes = bind(UDPSock, (struct sockaddr *) &mySockAddrIn, sizeof(mySockAddrIn));
Check_Print_Error(bindRes, __func__, "bind", errCount);
}while(bindRes == -1);
do{ //block until a message is received while checking for errors
fromLen = sizeof(fromSockAddrIn);
recvBytes = recvfrom(UDPSock, buff, len, 0,(struct sockaddr *) fromSockAddrIn, &fromLen);
Check_Print_Error(recvBytes, __func__, "recvfrom", errCount);
printf("received %d bytes on UDP: %s ", recvBytes, buff);
inet_ntop(afFamily, &fromSockAddrIn->sin_addr, fromIp, sizeof(fromIp));
printf("from ip: %s \n", fromIp);
}while(recvBytes > 0);
Check_Print_Error:
void Check_Print_Error(int errNum, const char *func1, char *func2, uint8_t *errCount){
if(errNum == -1){
errCount += 1;
fprintf(stderr, "%s, %s error: (%d) %s \n", func1, func2, errno, strerror(errno));
}
if(*errCount >= 10){
fprintf(stderr, "%s tried %s too many times (%u)\nExiting...", func1, func2, *errCount);
fflush(stdout);
fflush(stderr);
exit(EXIT_FAILURE);
}
return;
}
python 发送消息的程序:
import socket
import time
toIPAddr = "192.168.202.128"
port = 8004
message = "ping from python"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
toAddr = (toIPAddr, port)
print "toAddr:", toAddr
for x in range(0,6):
print "sending...'",message,"' size of", len(message)
sock.sendto(message , toAddr)
time.sleep(1)
我的输出:
received 16 bytes on UDP: ping from python from ip: 0.0.0.0
感谢您的帮助。
您在这段代码中对 sizeof(fromSockAddrIn)
的使用到处都是错误的
struct sockaddr_in *fromSockAddrIn = calloc(1, sizeof(fromSockAddrIn));
这只是为指针分配空间。你需要得到一个struct sockardd_in
的大小,所以
struct sockaddr_in *fromSockAddrIn = calloc(1, sizeof(*fromSockAddrIn));
类似你计算fromLen = sizeof(fromSockAddrIn);
,那应该是fromLen = sizeof(*fromSockAddrIn);
看来您不需要动态分配 fromSockAddrIn
变量。如果您只是将其声明为 struct sockaddr_in fromSockAddrIn;
,则可以使用 sizeof(mySockAddrIn)
。