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

前端 未结 4 1530
挽巷
挽巷 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:13

    Your other code sample works because you are using sendto before recvfrom. If a UDP socket is unbound and either sendto or connect are called on it, the system will automatically bind it for you and thus the recvfrom call later on will succeed. recvfrom will not bind a socket, as this call expects the socket to have been bound already, or an error will be thrown.

提交回复
热议问题