Marco Freudenberger Marco Freudenberger - 1 year ago 80
C++ Question

Calling connect() multiple times on UDP socket to send to different target address - valid?

I'm working on a UDP communication protocol implementation.

I've got a question in regards to connect() call for UDP.

I want to use connect() and send() rather than sendto(). Usually the receiver end does not change. However, under certain circumstances, it might.
Is it valid to call connect() on a UDP socket that already is connected to set a different target without closing and re-opening the socket (and by that loosing buffered received datagrams)?

I didn't find any documentation whether that is valid or not (so, testing might only tell me if it happens to work, which might not be portable). I am currently working with winsock2 on Windows 7, but want to be portable to other (newer) Windows versions and to linux.

Answer Source

At least for Linux, the manpage (man 2 connect) says OK: ' Generally, connection-based protocol sockets may successfully connect() only once; connectionless protocol sockets may use connect() multiple times to change their association. Connectionless sockets may dissolve the asso‐ ciation by connecting to an address with the sa_family member of sockaddr set to AF_UNSPEC (supported on Linux since kernel 2.2). '

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download