Specifying an inter...
 
Notifications
Clear all

Specifying an interface for ethernet UDP use in C

2 Posts
2 Users
0 Reactions
1,971 Views
Posts: 130
Topic starter
(@charleskerr)
Estimable Member
Joined: 16 years ago
[#25481]

Ok, I have a udp socket (C network programming), and want to specify what interface to use (which physical interface on the machine).

I have the following:


    index = if_nametoindex(mydevice);
    if(setsockopt((int)socketfd, IPPROTO_IP, IP_BOUND_IF, &index, sizeof(index)) < 0)

That seems to not return an acceptable response. Does anyone have insight into specifying the appropriate network interface (short of using bind, which since this is connectionless, shouldn't be needed).


1 Reply
Posts: 30
(@cliffg)
Eminent Member
Joined: 14 years ago

If UDP, you want IPPROTO_UDP. You're using the enums for "raw", and will get IP level datagrams instead of UDP level datagrams.

If you're reading UDP (datagrams), you will need to bind a receive port (otherwise, how would you be able to receive specific datagrams?).

Without seeing the rest of your code, not sure what the problem could be.

I'm assuming you're doing unicast UDP, not multicast (UDP) or broadcast (UDP).

Cliff


Reply
Share: