0

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);
      }
    }
  }
}
Neel Mehta
  • 278
  • 6
  • 17
  • you want to discard packets you've sent to yourself? why are you sending them in the first place? (sorry if I completely misunderstood you) – Mat Jun 15 '11 at 19:14
  • Actually, there are multiple transceivers and hence I am not interested in receiving the packages sent by me but sent by others. Hence, I wish to discard those sent by me. – Neel Mehta Jun 15 '11 at 19:16
  • I still don't understand you setup, but: how do you distinguish a packet coming from "yourself" from one coming from "someone else"? – Mat Jun 15 '11 at 19:17
  • I should be able to distinguish the packets from the sender IP, but I dont have access to it as of moment. The answer which has just turned up looks to be correct. Will have to check it out. – Neel Mehta Jun 15 '11 at 19:20
  • @Mat - if you have several computers broadcasting stuff and you only want the broadcasts from others - that would be a reasonable scenario. Depends on the protocol I would put some ID inside the UDP message itself, but if that's not possible - then filtering by the sender is indeed the only option. – littleadv Jun 15 '11 at 19:20
  • well it does start to make sens if there are multiple IPs involved. The only thing mentioned in the question is multiple threads - was having a hard time wrapping my head around an app sending itself packets and having to discard them :-) – Mat Jun 15 '11 at 19:22
  • Sorry, I forgot to mention that I am broadcasting the packets!! – Neel Mehta Jun 15 '11 at 19:27

2 Answers2

3

You need to fill the last two parameters of the recvfrom call - 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.

Community
  • 1
  • 1
littleadv
  • 20,100
  • 2
  • 36
  • 50
  • If you have access to the local socket that you are using for sending, you can simply use `getsockname()` on that socket to find the local address to compare with. – caf Jun 22 '11 at 07:39
2

If you are talking about Multicast, unset the IP(V6)?_MULTICAST_LOOP option (which may be enabled by default) to not receive what you send. Note that this will prevent other applications on the same host to receive what you sent.

int zero = 0;
err = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &zero, sizeof zero);

I haven't heard of an equivalent for broadcast. But broadcast is so old that it was replaced by Multicast in the IPv6 standard.

BatchyX
  • 4,986
  • 2
  • 18
  • 17