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

Reply

Join Date: May 2008
Posts: 25
Reputation: anitha joe has a little shameless behaviour in the past 
Solved Threads: 0
anitha joe anitha joe is offline Offline
Light Poster

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

 
-1
  #1
May 12th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,607
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

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

 
0
  #2
May 13th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

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

 
0
  #3
May 14th, 2009
For connection oriented UDP (like in your case), it is important to bind
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 25
Reputation: anitha joe has a little shameless behaviour in the past 
Solved Threads: 0
anitha joe anitha joe is offline Offline
Light Poster

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

 
0
  #4
May 14th, 2009
If I bind to the same port number wont it give an error??
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC