网络编程UDP

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);

socket function

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))

bind function

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值