Thread: UDP programming - set local listening port

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    UDP programming - set local listening port

    Hi, I am interested in a variant of question posted in this locked topic:
    UDP programming - set local port
    I am programming a network software that sends file using UDP protocol. I have some dubts about how to let the server listen to a specific port while cycling recevfrom()
    I think this can be done with bind function, am I right? THX

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well yes you would create a UDP socket first and bind that socket with the socket information after initialisng the structure. Something like as follow

    Code:
    struct sockaddr_in my_sock;
    
    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
        exit(1);
    
    memset((char *) &si_me, 0, sizeof(my_sock));
    my_sock.sin_family = AF_INET;
    my_sock.sin_port = htons(PORT);
    my_sock.sin_addr.s_addr = htonl(INADDR_ANY);
    
    if (bind(s, &my_sock, sizeof(my_sock))==-1)
      exit(1);
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    I have another question: in server-side code, should I specify the machine ip address where server is running? This line of code
    Code:
    my_sock.sin_addr.s_addr = htonl(INADDR_ANY);
    specifies that the packets can come from any address? From GAPIL book I can read:
    Code:
    struct sockaddr_in {
    sa_family_t  sin_family ;  /* address family : AF_INET */ 
    in_port_t  sin_port ;  /* port in network byte order */
    struct in_addr sin_addr ;  /* internet address */ 
    };
     /* Internet address . */
    struct in_addr {
    in_addr_t  s_addr ;  /* address in network byte order */
    };
    Thank you for your time
    Last edited by Rendering; 03-22-2012 at 09:26 AM.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    No not necessarly. The INADDR_ANY will take the default ip address of your machine and tryied to assign it. If you need to assign a specific ip address yes you can.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Com Port Listening
    By W35 in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2006, 12:39 PM
  2. How to do port listening in C++?
    By voodoosg in forum Networking/Device Communication
    Replies: 1
    Last Post: 06-07-2006, 12:40 AM
  3. UDP programming - set local port
    By Specterx in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2005, 11:40 AM
  4. port listening
    By winterflood_j in forum Windows Programming
    Replies: 6
    Last Post: 12-26-2003, 01:38 PM
  5. Replies: 1
    Last Post: 10-03-2002, 09:52 AM