
September 9th, 2002, 07:37 AM
|
 |
Contributing User
|
|
Join Date: Jan 2002
Posts: 32
Time spent in forums: < 1 sec
Reputation Power: 7
|
|
|
is this the right way to setsockopt()?
i'm trying to use setsockopt() to turn off Nagle's algorithm, to set the send buffer to zero, and to set the receive timeout to 30 seconds... is the code i'm using below correct? and where should i place this code - after a socket() or after the connect()?
Code:
int tcpnodelay_flag = 1;
size_t sendbuffer_sz = 0;
struct timeval timeout;
/* set TCP_NODELAY to turn off Nagle's algorithm */
if(setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *) &tcpnodelay_flag, sizeof(int)) == -1)
{
perror("setsockopt (TCP_NODELAY)");
exit(1);
}
/* set send buffer 0 */
if(setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char *) &sendbuffer_sz, sizeof(size_t)) == -1)
{
perror("setsockopt (SO_SNDBUF)");
exit(1);
}
/* set receive timeout to 30s */
timeout.tv_sec = 30;
timeout.tv_usec = 0;
if(setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == -1)
{
perror("setsockopt (SO_RCVTIMEO)");
exit(1);
}
|