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

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

    With UDP, you have to bind() the socket in the client because UDP is connectionless, so there is no other way for the stack to know which program to deliver datagrams to for a particular port.

    If you could recvfrom() without bind(), you'd essentially be asking the stack to give your program all UDP datagrams sent to that computer. Since the stack delivers datagrams to only one program, this would break DNS, Windows' Network Neighborhood, network time sync....

    You may have read somewhere on the net that binding in a client is lame, but that advice only applies to TCP connections.

提交回复
热议问题