I have a program which transmits(broadcasts) and receives UDP packets simultaneously using pthreads. What I want to do is to remove packets which have been sent by me. How do I do that? My receive code currently looks as follows:
void *receive_thread_body(void *arg)
{
long msg = 0;
while(msg<500)
{
fd_set socket_set;
FD_ZERO(&socket_set);
FD_SET(b_sock,&socket_set);
struct timeval tm;
tm.tv_usec = 10;
tm.tv_sec = 0;
int ret = select(b_sock+1,&socket_set,0,0,&tm);
if(ret == -1)
{
std::cout<<"select failed";
}
if(FD_ISSET(b_sock,&socket_set) != 0)
{
int recvStringLen = recvfrom(b_sock, &msg, sizeof(msg), 0, NULL, 0);
if(recvStringLen < 0)
{
std::cout<<"recvfrom failed";
}
else
{
printf("\t\t\tRX: %d\n",msg);
}
}
}
}
You need to fill the last two parameters of the
recvfromcall – that will be the sender’s address.Then compare it to the list of your own addresses for matches (see here for example for relevant info).
This method is agnostic to the way of sending – you can use it on connection or connection-less sockets, and of course on broadcast or multicast (or unicast) transmissions.