我正在recvfrom()的'from'参数中读取存储的IP地址0.0.0.0,我不确定为什么。消息通过得很好,并且在“ 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)
。