1、相关函数
1、socket
创建套接字
SOCKET WSAAPI socket(
_In_ int af,
_In_ int type,
_In_ int protocol
);
// AF_INET The Internet Protocol version 4 (IPv4) address family.
/* SOCK_DGRAM A socket type that supports datagrams,
* which are connectionless, unreliable buffers of a fixed(typically small) maximum length.
* This socket type uses the User Datagram Protocol (UDP) for the Internet address family (AF_INET or AF_INET6).
*/
/* IPPROTO_UDP The User Datagram Protocol (UDP).
*This is a possible value when the af parameter is AF_INET or AF_INET6 and the type parameter is SOCK_DGRAM.
*/
//例
Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2、bind
绑定本地地址到所创建的套接字
int bind(
_In_ SOCKET s,
_In_ const struct sockaddr *name,
_In_ int namelen
);
//例
// The socket address to be passed to bind
sockaddr_in service;
// The sockaddr_in structure specifies the address family,
// IP address, and port for the socket that is being bound.
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(27015);
bind(ListenSocket, (SOCKADDR *) &service, sizeof (service))
3、recvfrom
接收数据报并储存源地址
int recvfrom(
_In_ SOCKET s, //A descriptor identifying a bound socket.
_Out_ char *buf, //A buffer for the incoming data.
_In_ int len, // The length, in bytes, of the buffer pointed to by the buf param