Tuesday, March 18, 2014

[SOCKET] On udp recvfrom peer address return

ISSUE: When using udp socket it is necessary to know the remote peer address from where the data is received. In future this can be used for sending and tracking messages. If you are ICE or STUN ,TURN developer then its a must.

char recv_data[1024];
int sin_size;
struct sockaddr_in remote_addr;
int bytes_recv = recvfrom(mySock, recv_data,1024,0,(struct sockaddr *)&remote_addr,(socklen_t*)&sin_size);

printf("got packet from IP:Port :: %s:%d\n",inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port));  

In receiving data which returns remote port and IP as

Fix: Its happen because the sin_size is not initialize as it takes garbage bellow from system. So just initialize the sin_size will fix that.

 sin_size = sizeof(remote_addr);

So enjoy socket programming :)

No comments:

Post a Comment