昨天用recvfrom去接收数据,好久没有过这个函数,按照man的手册,一个一个传参,但是遇到一个问题,数据可以接收正常,但是总是errno一直为22,并且sendto一直失败返回-1。这里做一下记录
函数原型:
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
前面几个参数没什么好说,一搜一大把。遇到的问题就在于最后一个参数:socklen_t *addrlen。看到传入指针,我的第一反应就是,这是一个结果参数,所以初始化完之后直接传入。导致在之后调用sendto时,一直失败。后来才发现recvfrom的函数说明中有下面一段话:
recvfrom()
recvfrom() places the received message into the buffer buf. The caller must specify the size of the buffer inlen.
If src_addr is not NULL, and the underlying protocol provides the source address of the message, that source address is placed in the buffer pointed to by src_addr. In this case, addrlen is a value-result argument. Before the call, it should be initialized to the size of the buffer associated with src_addr. Upon return, addrlen is updated to contain the actual size of the source address. The returned address is truncated if the buffer provided is too small; in this case, addrlen will return a value greater than was supplied to the call.
If the caller is not interested in the source address, src_addr and addrlen should be specified as NULL.
真神奇,粗心大意浪费了时间,打自己一下。。