套接字间通信的两个函数

本文详细介绍了sendto函数用于向特定目的地发送数据的方法及其参数含义,同时解析了recvfrom函数如何接收数据并存储源地址的过程。文章还列举了recvfrom函数中多种标志的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sendto Function

The sendto function sends data to a specific destination.

int sendto(
  __in          SOCKET s,
  __in          const char* buf,
  __in          int len,
  __in          int flags,
  __in          const struct sockaddr* to,
  __in          int tolen
);
Parameters
s

Descriptor identifying a (possibly connected) socket.

buf

Buffer containing the data to be transmitted.

len

Length of the data in buf, in bytes.

flags

Indicator specifying the way in which the call is made.

to

Optional pointer to a sockaddr structure that contains the address of the target socket.

tolen

Size of the address in to, in bytes.

Return Value

If no error occurs, sendto returns the total number of bytes sent, which can be less than the number indicated by len. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

ecvfrom函数(经socket接收数据):

函数原型:ssize_t recvfrom(int sockfd,void *buf,int len,unsigned int flags, struct sockaddr *from,socket_t *fromlen); ssize_t 相当于 int,socket_t 相当于int ,这里用这个名字为的是提高代码的自说明性。

参数:

sockfd:标识一个已连接 套接口的描述字。
buf:接收 数据缓冲区
len:缓冲区长度。
flags:调用操作方式。是以下一个或者多个标志的组合体,可通过or操作连在一起:
MSG_DONTWAIT:操作不会被阻塞。
MSG_ERRQUEUE: 指示应该从套接字的错误队列上接收错误值,依据不同的协议,错误值以某种辅佐性消息的方式传递进来,使用者应该提供足够大的缓冲区。导致错误的原封包通过msg_iovec作为一般的数据来传递。导致错误的数据报原目标地址作为msg_name被提供。错误以sock_extended_err结构形态被使用,定义如下
#define SO_EE_ORIGIN_NONE 0
#define SO_EE_ORIGIN_LOCAL 1
#define SO_EE_ORIGIN_ICMP 2
#define SO_EE_ORIGIN_ICMP6 3
struct sock_extended_err
{
u_int32_t ee_errno;
u_int8_t ee_origin;
u_int8_t ee_type;
u_int8_t ee_code;
u_int8_t ee_pad;
u_int32_t ee_info;
u_int32_t ee_data;
};
MSG_PEEK:指示数据接收后,在接收队列中保留原数据,不将其删除,随后的读操作还可以接收相同的数据。
MSG_TRUNC:返回封包的实际长度,即使它比所提供的缓冲区更长, 只对packet套接字有效。
MSG_WAITALL:要求阻塞操作,直到请求得到完整的满足。然而,如果捕捉到信号,错误或者连接断开发生,或者下次被接收的数据类型不同,仍会返回少于请求量的数据。
MSG_EOR:指示记录的结束,返回的数据完成一个记录。
MSG_TRUNC:指明数据报尾部数据已被丢弃,因为它比所提供的缓冲区需要更多的空间。
MSG_CTRUNC:指明由于缓冲区空间不足,一些控制数据已被丢弃。
MSG_OOB:指示接收到out-of-band数据(即需要优先处理的数据)。
MSG_ERRQUEUE:指示除了来自套接字错误队列的错误外,没有接收到其它数据。
from:(可选) 指针,指向装有源地址的缓冲区。
fromlen:(可选) 指针,指向from缓冲区长度值。

The recvfrom function receives a datagram and stores the source address.

int recvfrom(
  __in          SOCKET s,
  __out         char* buf,
  __in          int len,
  __in          int flags,
  __out         struct sockaddr* from,
  __in_out      int* fromlen
);
Parameters
s

Descriptor identifying a bound socket.

buf

Buffer for the incoming data.

len

Length of buf, in bytes.

flags

Indicator specifying the way in which the call is made.

from

Optional pointer to a buffer in a sockaddr structure that will hold the source address upon return.

fromlen

Optional pointer to the size, in bytes, of the from buffer.

Return Value

If no error occurs, recvfrom returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值