How do I receive udp packets with winsock in c++?

后端 未结 2 1400
礼貌的吻别
礼貌的吻别 2020-12-28 09:43

As an attempt at wrapping my head around udp sockets I\'ve tried to port the code from this tutorial page http://www.linuxhowtos.org/C_C++/socket.htm to winsock (running on

2条回答
  •  悲哀的现实
    2020-12-28 10:21

    Well, I'm not sure if winsock2.h works the same way in Windows and Linux, but, in Windows, when you create the socket, you must set the protocol you're using, either TCP or UDP:

    SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    

    AF_INET = IPv4; SOCK_STREAM = Byte stream for TCP; IPPROTO_TCP = TCP Protocol.

    For UDP (which i've never used before), according to MSDN it would be:

    SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    

    SOCK_DGRAM = Byte stream for UDP; IPPROTO_UDP = UDP Protocol.

    That code would work in Windows. I guess in Linux it would be similar.

提交回复
热议问题