2017-02-08 2 views
-2

Ich lese die gespeicherte IP-Adresse von 0.0.0.0 in der 'von' Argument von recvfrom() und ich bin mir nicht sicher warum. Die Nachricht kommt gut durch und es sind einige Informationen in 'fromSockAddrIn' gespeichert.Warum speichert recvfrom eine IP-Adresse von 0.0.0.0 auf UDP-Nachricht?

Code:

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-Programm-Nachricht senden:

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) 

meine Ausgabe:

received 16 bytes on UDP: ping from python from ip: 0.0.0.0 

Danke für jede Hilfe.

Antwort

2

Ihre Nutzung von sizeof(fromSockAddrIn) falsch ist überall in diesem Code

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

Dies weist nur Raum für einen Zeiger. Sie müssen die Größe eines struct sockardd_in, erhalten, so

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

Ähnliche tun, wenn Sie fromLen = sizeof(fromSockAddrIn); berechnen, dass fromLen = sizeof(*fromSockAddrIn);

sein sollte Es scheint, dass Sie nicht dynamisch brauchen sollte, um Ihre fromSockAddrIn Variable zuordnen. Wenn Sie es nur als struct sockaddr_in fromSockAddrIn; deklarieren, können Sie sizeof(mySockAddrIn) verwenden.

+0

Danke für die Speicherzuteilung Tipps, aber das löst nicht mein Problem. – Matt

+1

@Matt Ja, tut es. Wenn dies nicht der Fall ist, haben Sie Ihren Code nicht richtig repariert und haben immer noch eine Größe von (fromSockAddrIn). – nos

+0

Nevermind, ich habe mich geirrt und Sie sind richtig, Sir. Vielen Dank. Ich vermisse es, eine der Größe von (vonSockAddrIn) zu ändern. – Matt

Verwandte Themen