Skip to content
Advertisement

Setting both read() and send() timeouts for the same C socket on linux

I managed to set a read timeout for a C socket with:

struct timeval tv_read;
tv_read.tv_sec = 2;
tv_read.tv_usec = 0;
setsockopt(my_socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv_read, sizeof tv_read);

Now, I want to make this same socket timeouts for send operations. I have found in setsockopt that the proper way to set a timeout for send is quite similar, just replacing SO_RCVTIMEO by SO_SNDTIMEO:

struct timeval tv_write;
tv_write.tv_sec = 4;
tv_write.tv_usec = 0;
setsockopt(my_socket, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tv_write, sizeof tv_write);

My question is if does a successive call of setsockopt overrides the previous one? Or should I actually call twice being one with SO_RCVTIMEO and other with SO_SNDTIMEO as shown above?

Advertisement

Answer

setsockopt() sets 1 specific option at a time. It is perfectly fine to set multiple options individually.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement