Setsockopt linux so rcvtimeo

Linux: is there a read or recv from socket with timeout?

How can I try to read data from socket with timeout? I know, select, pselect, poll, has a timeout field, but using of them disables «tcp fast-path» in tcp reno stack. The only idea I have is to use recv(fd, . MSG_DONTWAIT) in a loop

5 Answers 5

You can use the setsockopt function to set a timeout on receive operations:

SO_RCVTIMEO

Sets the timeout value that specifies the maximum amount of time an input function waits until it completes. It accepts a timeval structure with the number of seconds and microseconds specifying the limit on how long to wait for an input operation to complete. If a receive operation has blocked for this much time without receiving additional data, it shall return with a partial count or errno set to [EAGAIN] or [EWOULDBLOCK] if no data is received. The default for this option is zero, which indicates that a receive operation shall not time out. This option takes a timeval structure. Note that not all implementations allow this option to be set.

// LINUX struct timeval tv; tv.tv_sec = timeout_in_seconds; tv.tv_usec = 0; setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv); // WINDOWS DWORD timeout = timeout_in_seconds * 1000; setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof timeout); // MAC OS X (identical to Linux) struct timeval tv; tv.tv_sec = timeout_in_seconds; tv.tv_usec = 0; setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv); 

Reportedly on Windows this should be done before calling bind . I have verified by experiment that it can be done either before or after bind on Linux and OS X.

Источник

PROLOG

This manual page is part of the POSIX Programmer’s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.

NAME

setsockopt — set the socket options

SYNOPSIS

int setsockopt(int socket, int level, int option_name, 
const void *option_value, socklen_t option_len);

DESCRIPTION

The setsockopt() function shall set the option specified by the option_name argument, at the protocol level specified by the level argument, to the value pointed to by the option_value argument for the socket associated with the file descriptor specified by the socket argument.

Читайте также:  Linux all format usb

The level argument specifies the protocol level at which the option resides. To set options at the socket level, specify the level argument as SOL_SOCKET. To set options at other levels, supply the appropriate level identifier for the protocol controlling the option. For example, to indicate that an option is interpreted by the TCP (Transport Control Protocol), set level to IPPROTO_TCP as defined in the header.

The option_name argument specifies a single option to set. It can be one of the socket-level options defined in and described in Section 2.10.16, Use of Options. If option_name is equal to SO_RCVTIMEO or SO_SNDTIMEO and the implementation supports setting the option, it is unspecified whether the struct timeval pointed to by option_value is stored as provided by this function or is rounded up to align with the resolution of the clock being used. If setsockopt() is called with option_name equal to SO_ACCEPTCONN, SO_ERROR, or SO_TYPE, the behavior is unspecified.

RETURN VALUE

Upon successful completion, setsockopt() shall return 0. Otherwise, -1 shall be returned and errno set to indicate the error.

ERRORS

The setsockopt() function shall fail if:

EBADF The socket argument is not a valid file descriptor. EDOM The send and receive timeout values are too big to fit into the timeout fields in the socket structure. EINVAL The specified option is invalid at the specified socket level or the socket has been shut down. EISCONN The socket is already connected, and a specified option cannot be set while the socket is connected. ENOPROTOOPT
The option is not supported by the protocol. ENOTSOCK The socket argument does not refer to a socket.

The setsockopt() function may fail if:

ENOMEM There was insufficient memory available for the operation to complete. ENOBUFS Insufficient resources are available in the system to complete the call.

The following sections are informative.

EXAMPLES

APPLICATION USAGE

The setsockopt() function provides an application program with the means to control socket behavior. An application program can use setsockopt() to allocate buffer space, control timeouts, or permit socket data broadcasts. The header defines the socket-level options available to setsockopt().

Читайте также:  Configuration manager for linux

Options may exist at multiple protocol levels. The SO_ options are always present at the uppermost socket level.

RATIONALE

FUTURE DIRECTIONS

SEE ALSO

Section 2.10, Sockets, bind(), endprotoent(), getsockopt(), socket()

The Base Definitions volume of POSIX.1‐2017, ,

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology — Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .

Package name: core/man-pages Version: 6.04-1 Upstream: https://www.kernel.org/doc/man-pages/ Licenses: GPL, custom Manuals: /listing/core/man-pages/ Table of contents

Powered by archmanweb, using mandoc for the conversion of manual pages.

The website is available under the terms of the GPL-3.0 license, except for the contents of the manual pages, which have their own license specified in the corresponding Arch Linux package.

Источник

Proper use of getsockopt and setsockopt for SO_RCVTIMEO and SO_SNDTIMEO

By various reasons I would like to implement timeout on reading and writing to socket in a server but fail to get it running and therefore kindly ask for some insight into wherein the problem may reside. In order to set the timeout on the read and write to the socket I’m trying to use of the functions setsocketopt() and getsocketopt() . However I must be doing something wrong as the return value indicates that a problem have occurred and perror outputs «Invalid argument». Strangely enough the error does not always occur at the first usage of setsocketopt() and getsocketopt() , which puzzles me bit. The following server and client codes reproduces my problems (compiled using gcc) server code:

#include #include #include #include #include #include #include int main() < struct sockaddr_in saddr,caddr; socklen_t clen; int sock, csock, reuse = 1, ret=0; socklen_t ntrcv, ntsnd; struct timeval tout, tsnd, trcv; // create a new stream socket if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) perror("failed to create socket"); else < // enable the socket to reuse the address if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) perror("failed allowing server socket to reuse address"); else < // set up the server address memset((char *) &saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = inet_addr("127.0.0.1"); saddr.sin_port = htons(45454); // bind socket to address if (bind(sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) perror("failed to bind"); else < // listen to the socket for connections if (listen(sock,5) < 0) perror("failed to listen"); else < clen = sizeof(caddr); if ((csock = accept(sock, (struct sockaddr *) &caddr, &clen)) < 0) perror("failed to accept"); else < tout.tv_sec=0; tout.tv_usec=10000; // check value of errno prior to setting timeout perror("errno prior to timeout"); if (getsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &trcv, &ntrcv) < 0) perror("2"); else if (getsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tsnd, &ntsnd) < 0) perror("3"); else if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tout, sizeof(tout)) < 0) perror("4"); else if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tout, sizeof(tout)) < 0) perror("5"); else < printf ("all ok so far in server\n"); sleep(1); if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &trcv, ntrcv) < 0) perror("6"); else if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tsnd, ntsnd) < 0) perror("7"); >if (close(csock) < 0) perror("failed to close csock"); >> > > > if (close(sock)
#include #include #include #include #include #include #include int main() < struct sockaddr_in addr; struct hostent *server; int sock = 0; // resolve server name if (!(server = gethostbyname("127.0.0.1"))) perror("failed to resolve host"); else < // prepare the server address memset((char *) &addr, 0, sizeof(addr)); addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&addr.sin_addr.s_addr, server->h_length); addr.sin_port = htons(45454); // create a socket and connect to the server if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) perror("failed to create client socket"); else < if (connect(sock,(struct sockaddr *) &addr,sizeof(addr)) < 0) perror("failed to connect to server"); else < printf("Connection is established will sleep now\n"); sleep(3); printf("done sleeping\n"); close(sock); printf("socket is closed\n"); >> > return 0; > 

In the present example the second call to getsockopts() in the server fails resulting in perror(«3») to be called. If I however comment this line out as well as the last call to setsockopts() the first call to getsockopts() fails (which previously seemed to work). Any insights to where I go wrong are appreciated

Читайте также:  How to delete partition in linux

Источник

Оцените статью
Adblock
detector