about linux socket value-result argument

if you are familiar with linux socket, you may encounter the accept and connect function, the prototype of these two functions are shown below:

int accept(int sockfd, struct sockaddr* addr, socklen_t *addrlen)

int connect(int sockfd, struct sockaddr* addr, socklen_t addrlen)

you may think of that the arguments of these two functions are identical. in fact, the third argument is different. the first one's type is pointer of integer and the second one's type is integer. so what is the difference?

that's the topic we talk here--value-result argument. as we know, each system call interacts with the kernel, so does accept function. when calling the accept function, we put the socket's file discriptor sock_fd, the beginning address of  the socket addr and the length of socket structure addrlen to the kernel so that the kernel would fill the socket structure with the received data from the network. due to the addrlen argument, the kernel would not write past end of the socket structure. at the same time, the kernel can tell the process the actual data size copied with the addrlen. so the argument addrlen is both the value(the process tell the kernel the size of socket structure) and the result(the kernel tell the process the size of copied data), so we call it value-result argument.

相关推荐