从tcp,udp链接角度看send和sendto的区别

send函数适用于TCP连接,sendto函数适用于UDP连接。send在连接状态下使用,sendto在UDP或连接模式套接字中,dest_addr和addrlen参数可能被忽略。虽然sendto可用于TCP,但通常不推荐。recv和recvfrom的使用方式类似。
摘要由CSDN通过智能技术生成
ssize_t send(int sockfd, const void *buf, size_t len, int flags);

ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, 
				const struct sockaddr *dest_addr, socklen_t addrlen);

send函数一般用于TCP链接,sendto函数一般用于UDP连接。

来看看man手册里怎么说send与sendto:

The send() call may be used only when the socket is in a connected state (so that the
intended recipient is known).
The only difference between send() and write(2) is the presence of flags. With a zero
flags argument, send() is equivalent to write(2). Also, the following
callsend(sockfd, buf, len, flags);is equivalent tosendto(sockfd, buf, len, flags, NULL, 0);

If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET) socket, the arguments dest_addr and addrlen are ignored (and the error EISCONN may be returned when they are not NULL and 0), and the error ENOTCONN is returned when the socket was not actually connected. Otherwise, the a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值