943,648 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1359
  • C RSS
May 12th, 2009
-1

Can a UDP socket be bound to same port number and different ip addresses

Expand Post »
Hello All,
I have a C program where I have created a UDP socket and bound to a particular ip address and a port number. As the program proceeds, msgs will be sent to different clients using the same socket, using the same port, but each has a different ip address. Is it possible to just overload the remoteServAddr1.sin_addr.s_addr with the desired ipaddress each time? Or is it important to bind the socket every time the ipaddress changes? If I call bind more than once, it gives me an error because the port is already in use.
  1. struct hostent *h;
  2. sd = socket(AF_INET,SOCK_DGRAM,0);
  3. if(sd<0)
  4. {
  5. printf(" cannot open socket \n");
  6.  
  7. }
  8.  
  9.  
  10. h = gethostbyname(address[0]); /*the first ip address*/
  11. if(h==NULL)
  12. {
  13. printf("unknown host '%s' \n", address[0]);
  14.  
  15. }
  16.  
  17. h->h_name,inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
  18.  
  19. remoteServAddr.sin_family = h->h_addrtype;
  20. memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
  21. remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);
  22.  
  23.  
  24. /* bind any port */
  25. cliAddr.sin_family = AF_INET;
  26. cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  27. cliAddr.sin_port = htons(REMOTE_SERVER_PORT);
  28.  
  29. rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
  30.  
  31. if(rc<0)
  32. {
  33. printf("cannot bind port\n");
  34.  
  35. }
I want to change the ipaddress which will be stored as address[1]..So rather than address[0] in the above code, I want address[1]( socket,port being the same)..

Something like this
  1. memcpy((char *) &remoteServAddr.sin_addr.s_addr, address[1], h1->h_length);
  2. accept(sd, (struct sockaddr *)(&remoteServAddr), &cliLen);
  3. printf("sending %s to %s on UDP port %u \n",connection,inet_ntoa(remoteServAddr.sin_addr), ntohs(remoteServAddr1.sin_port));
  4. rc = sendto(sd,connection,MAX_MSG, 0,(struct sockaddr *) &remoteServAddr, sizeof(remoteServAddr));
Is it possible at all??Any help would be appreciated..Right now its giving segmentation fault...
Thanks
Ani
Last edited by Narue; May 14th, 2009 at 12:24 pm. Reason: added code tags
Similar Threads
Reputation Points: 4
Solved Threads: 0
Light Poster
anitha joe is offline Offline
25 posts
since May 2008
May 13th, 2009
0

Re: Can a UDP socket be bound to same port number and different ip addresses

maybe i'm missing something, but UDP doesn't care what IP address is in the pseudo-header. it's a multicast protocol. anyone on the network can see and process the datagrams if they desire to do so.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 14th, 2009
0

Re: Can a UDP socket be bound to same port number and different ip addresses

For connection oriented UDP (like in your case), it is important to bind
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
May 14th, 2009
0

Re: Can a UDP socket be bound to same port number and different ip addresses

If I bind to the same port number wont it give an error??
Reputation Points: 4
Solved Threads: 0
Light Poster
anitha joe is offline Offline
25 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to Flip the order of strings in C/C++
Next Thread in C Forum Timeline: How to find index of input array from sorted output?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC