| | |
Can a UDP socket be bound to same port number and different ip addresses
![]() |
•
•
Join Date: May 2008
Posts: 25
Reputation:
Solved Threads: 0
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.
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
Is it possible at all??Any help would be appreciated..Right now its giving segmentation fault...
Thanks
Ani
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.
c Syntax (Toggle Plain Text)
struct hostent *h; sd = socket(AF_INET,SOCK_DGRAM,0); if(sd<0) { printf(" cannot open socket \n"); } h = gethostbyname(address[0]); /*the first ip address*/ if(h==NULL) { printf("unknown host '%s' \n", address[0]); } h->h_name,inet_ntoa(*(struct in_addr *)h->h_addr_list[0])); remoteServAddr.sin_family = h->h_addrtype; memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT); /* bind any port */ cliAddr.sin_family = AF_INET; cliAddr.sin_addr.s_addr = htonl(INADDR_ANY); cliAddr.sin_port = htons(REMOTE_SERVER_PORT); rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr)); if(rc<0) { printf("cannot bind port\n"); }
Something like this
c Syntax (Toggle Plain Text)
memcpy((char *) &remoteServAddr.sin_addr.s_addr, address[1], h1->h_length); accept(sd, (struct sockaddr *)(&remoteServAddr), &cliLen); printf("sending %s to %s on UDP port %u \n",connection,inet_ntoa(remoteServAddr.sin_addr), ntohs(remoteServAddr1.sin_port)); rc = sendto(sd,connection,MAX_MSG, 0,(struct sockaddr *) &remoteServAddr, sizeof(remoteServAddr));
Thanks
Ani
Last edited by Narue; May 14th, 2009 at 12:24 pm. Reason: added code tags
![]() |
Similar Threads
- Winsock Multi-Client Servers (C++)
- about finding server port number (Java)
- Keeping a map of IP, Mac and Port number (Networking Hardware Configuration)
- What will a flash xml socket connect to - please help - insanity is near (PHP)
- How do i change/get my port number? (Java)
- How to specify a port number (ASP)
- Port number, ip address, computer name (Windows 95 / 98 / Me)
Other Threads in the C Forum
- Previous Thread: how to generate random matrix in c
- Next Thread: How to find index of input array from sorted output?
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql odf open opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard string suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






