Thread: C Socket Sendto() from a specific interface

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    C Socket Sendto() from a specific interface

    Hi there,

    First of all thanks to everybody that may help you or even just read my post.

    I am pretty new to C programming, even more to socket programming and I have a problem...Sure otherwise I wouldn't ask for help

    I have a coded a multicast program which listen querries on a multicast address and then send back information through unicast address.

    My problem comes from the sendto, when I send back via unicast on my server I have many interface and I would like to choose the interface that is in my LAN not the one in the internet....

    How should I specify which interface to use on my own server with sendto()?

    I beleive this is quite a common question but I cannot find any answer or topic....Unfortunately.

    PS: I dont think that code is needed for now but let me know if you need piece of my code.

    Thanks,
    Best Regards,
    Sismon

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Please note that the follow assignement:

    Code:
       
       server_addr.sin_addr.s_addr = INADDR_ANY;
    ...will allow your system to listen in on any interface, but will egress traffic out with the lowest numbered interface.

    But if you need to explicitely assign your IP address, then you will have to create another sin.addr construct and give it the IP in network byte order then:

    Code:
       destination.sin_addr.s_addr = inet_addr("10.10.10.100");
    Then call your sendto to use the struct with the above declaration. Please note that the sendto does have an extra struct argument that allows for this.

    Code:
    /* Added sockaddr *myDestination here as an example */
       sendto(int fd,const void *msg, int len, unsigned int flags, const struct sockaddr *myDestination, int tolen);
    Last edited by slingerland3g; 07-16-2009 at 09:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sendto Socket packet size
    By bj00 in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-27-2007, 11:05 AM
  2. Socket program
    By mhetfield in forum C Programming
    Replies: 5
    Last Post: 04-03-2007, 03:46 PM
  3. socket program help
    By mhetfield in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-02-2007, 06:02 AM
  4. Socket handles
    By nico in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-03-2005, 10:33 PM
  5. OOP in C
    By lyx in forum C Programming
    Replies: 4
    Last Post: 11-23-2003, 01:12 PM