-1

我正在读取 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 

谢谢你的帮助。

4

1 回答 1

2

sizeof(fromSockAddrIn)在此代码中的任何地方都使用错误

struct sockaddr_in *fromSockAddrIn = calloc(1, sizeof(fromSockAddrIn));

这只是为指针分配空间。你需要得到 a 的大小struct sockardd_in,所以这样做

struct sockaddr_in *fromSockAddrIn = calloc(1, sizeof(*fromSockAddrIn));

计算时类似fromLen = sizeof(fromSockAddrIn);,应该是fromLen = sizeof(*fromSockAddrIn);

看来您不需要动态分配fromSockAddrIn变量。如果您只是将其声明为struct sockaddr_in fromSockAddrIn;,则可以使用sizeof(mySockAddrIn)

于 2017-02-08T23:08:52.667 回答