Do I have to bind a UDP socket in my client program to receive data? (I always get WSAEINVAL)

前端 未结 4 1519
挽巷
挽巷 2020-12-02 18:27

I am creating a UDP socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP) via Winsock and trying to recvfrom on this socket, but it a

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 19:14

    I had the same problem a few weeks ago, the following remarks helped me to understand whether an explicit bind() call is necessary:

    recvfrom function (MSDN)

    Explicit binding is discouraged for client applications. For client applications using this function, the socket can become bound implicitly to a local address through sendto, WSASendTo, or WSAJoinLeaf.

    sendto function (MSDN)

    Note If a socket is opened, a setsockopt call is made, and then a sendto call is made, Windows Sockets performs an implicit bind function call. If the socket is unbound, unique values are assigned to the local association by the system, and the socket is then marked as bound.

提交回复
热议问题