If I know the network interface to which server is connected and server IP but don't know the server port. Can I send the UDP packets on interface so that server can receive it?
What if i set server port 0 in sendto()? server_addr.sin_port = 0;
If I know the network interface to which server is connected and server IP but don't know the server port. Can I send the UDP packets on interface so that server can receive it?
What if i set server port 0 in sendto()? server_addr.sin_port = 0;
[I] don't know the server port. Can I send the UDP packets on interface so that server can receive it?
No -- not unless you want to send a UDP datagram to every possible UDP port (i.e. 65,535 datagram -- possible but terribly inefficient, and potentially disruptive to other software that might be receiving UDP datagrams on those ports).
What if i set server port 0 in sendto()? server_addr.sin_port = 0;
0 has no special meaning for sendto()
, but port 0 is reserved and should not be used; so specifying server_addr.sin_port = 0
before a sendto()
call will not do anything useful.
Most software deals with this problem by choosing in advance a "well-known" port number that both the server and the client agree to communicate on, at least for the initial traffic.
UDP is both IP-based and port-based, ie a UDP packet carries both a destination IP and a destination port number in its headers. Even if you could dump a UDP packet onto a network interface, the target server would ignore the packet if it doesn't match the IP/port that the server is listening on.
So, you simply cannot send a UDP packet to an unknown IP and/or unknown port. If you don't know the server's IP/port, you need to find out what it is beforehand. Either ask the server admin, or check if the server maybe advertises its current IP/Port somewhere that you can read it from.
Otherwise, the only way I could see this ever working is if the sender's interface has a dedicated connection to the server, and the server is using a promiscuous reading of packets, such as with a pcap library, handling any and all packets it sees regardless of their destination.