Blocking vs Non Blocking Socket

RECEIVE SOCKET TIMEOUT
The recv call is used to receive messages from a socket, and may be used to receive data on a socket whether or not it is connection-oriented.The recv() function shall return the length of the message written to the buffer pointed to by the buffer argument.
If no messages are available at the socket and O_NONBLOCK is not set on the socket’s file descriptor, recv() shall block until a message arrives.
However sometimes application needs its thread to timeout on a socket that blocks when reading. We can set timeout for a blocking rcv ( ) call using setscokopt function call.
setsockopt (connfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv);
Here SO_RCVTIMEO allow us to place a timeout on socket receives. Timeout value can be specified in timeval structure, this let us specify the timeout in seconds and microseconds. We can disable timeout by setting its value to 0 seconds and 0 microsecond. Both timeout are disabled by default. The receive timeout affects the five input functions.read, readv,rcv,recvfrom,recvmsg.
RETURN VALUE of rcv ()
Upon successful completion, recv() shall return the length of the message in bytes. If no messages are available to be received and the peer has performed an orderly shutdown, recv() shall return 0. Otherwise, -1 shall be returned and errno set to indicate the error.
Example:
Following is an example program in which timeout for recv call is shown.This is a simple program in which client sends current system time to the server after 5 minutes and if server can’t get data from client it will timeout after 8 seconds .

//SERVER CODE

#define SERV_PORT 7070
#define BACKLOG 10
#define MAXDATASIZE 200

char st1[13];
char st2[12][6];
char ip[15];

void command_result(int sockfd);
void child_commandprocessor(int sockfd);

int main(int argc,char *argv[])
{
int listenfd,connfd;
pid_t childpid;
socklen_t chilen,len;
struct sockaddr_in cliaddr,servaddr;
void sig_chld(int);

listenfd= socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));

servaddr.sin_family= AF_INET;
servaddr.sin_addr.s_addr= htonl(INADDR_ANY);
servaddr.sin_port= htons(SERV_PORT);

bind(listenfd,(struct sockaddr *) &servaddr,sizeof(struct sockaddr));

listen(listenfd,BACKLOG);

for( ; ; ){
chilen=sizeof( struct sockaddr_in);

if ( (connfd =accept(listenfd,(struct sockaddr *) &cliaddr,&chilen)) perror("accept error");
exit(1);

}

struct timeval tv;

tv.tv_sec =8;
tv.tv_usec = 0 ;

if (setsockopt (connfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv) perror(“setsockopt error”);

printf("Server: got connection from %s \n",inet_ntoa(cliaddr.sin_addr));
strcpy(ip,(char *)inet_ntoa(cliaddr.sin_addr));
printf("IP=%s",ip);

if ( (childpid =fork()) == 0){
close(listenfd);
child_commandprocessor(connfd);
exit(0);
}
close(connfd);
}

return 0;
}
void child_commandprocessor(int sockfd){

int numbytes,j;

while(1)
{

int ret;

ret=recv(sockfd,st1,sizeof(st1),0);

printf("TIME=%s\n",st1);
if(ret {
printf("Error:Client is Down \n");
close(sockfd);
exit(0);
}

}
}

//CLIENT CODE

#include
#define PORT 7070
#define MAXDATASIZE 200
#define BUFFER_LEN 128

int main(int argc,char *argv[]) {

printf("\n\t\t\tMonitoring System.\n\n");

time_t t;
char *dt;
int n=0,j;
char buf[100]={""};
int sockfd,reslen;
struct sockaddr_in servaddr;

if (argc
exit(1);
}

sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof( struct sockaddr_in));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(PORT);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);

connect(sockfd,(struct sockaddr*)&servaddr,sizeof (struct sockaddr_in));

while(1)
{
char st1[13];
char *t1,*t2,*t3,*t4;
time_t now;

now = time((time_t *)NULL);

//printf("%s", ctime(&now));

char *buftime;
buftime=ctime(&now);

t1=(char *)strtok(buftime," ");
t2=(char *)strtok(NULL, " ");
t3=(char *)strtok(NULL," ");
t4=(char *)strtok(NULL," ");

// st1[0]=(char *)malloc(OFFSET+1);
strcpy(st1,t4);
printf("Time=%s\n",st1);

send(sockfd,st1,sizeof(st1),0);

sleep(5);
printf("\n\n");
}
close(sockfd);

return 0;
}

Advertisements
This entry was posted in Programming and tagged , , , . Bookmark the permalink.

4 Responses to Blocking vs Non Blocking Socket

  1. Qamar Toheed says:

    The name of that bug was sh…

  2. gulaftab says:

    yeah, what days!!! ..every day was a new day.. & hard times in resolving bugs specially in your groups 🙂

  3. Qamar Toheed says:

    we both worked on it that day and i still remember those final dayz of that group which thought that its over yet it was just the strarting of a ……………

  4. Nestor says:

    Excellent!
    I was looking for a way to timeout a blocking socket for my ATL service program, couldn’t find any in microsoft docs.

    I wish they would have task-oriented articles, instead of pretending developers to read their whole library in order to find out something as simple as this timeout!

    Thanks a million!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s