SO_SNDTIMEO in setsockopt() doesn't work in send()


 
Thread Tools Search this Thread
Top Forums Programming SO_SNDTIMEO in setsockopt() doesn't work in send()
# 1  
Old 01-16-2011
SO_SNDTIMEO in setsockopt() doesn't work in send()

I create a program that need a timeout on both send() and recv() function, but I found that SO_SNDTIMEO doesn't work but SO_RCVTIMEO works!!

Code:
     int sock;
     int bytesRcvd, totalBytesRcvd = 0;
     struct sockaddr_in servAddr;
     struct timeval tv;
      int rx = 0;
      int tx = 0, sentBytes;
     UINT8* buf;
 
     assert( ( sock = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP ) ) >=  0 );      /* Create a reliable, stream socket using TCP */
 
     memset(&servAddr, 0, sizeof(servAddr));     /* Zero out structure */
     servAddr.sin_family      = AF_INET;             /* Internet address family */
     echoServAddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );   /* Server IP address */
     servAddr.sin_port        = htons( ( unsigned short )OPR_PORT );
 
     tv.tv_sec = 1;
     tv.tv_usec = 0;
     if (setsockopt( sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,  sizeof tv))
         exit( -1 );
     if (setsockopt( sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,  sizeof tv))
         exit( -1 );
 
     /* Establish the connection to the echo server */
     if ( connect( sock, ( struct sockaddr* )&servAddr, sizeof( servAddr ) ) < 0 ) {
         puts( "ERR: unable to connect 127.0.0.1" );
     }
 
     for ( ; ; ) {
         buf = ...;/* Obtain data which to be sent. */
 
 When I close the server when sending data, the following codes don't work and no any message is printed out!
         if ( ( ( sentBytes = send( sock, buf, MSG_SIZE, 0 ) ) == -1 || sentBytes != MSG_SIZE ) && (++tx < 1000) ) {
            printf("After timeout #%d, trying send again:\n", tx);
        }
 
 If I comment the source code about send() and only  enable recv(). 
Since no data is sent to server and the server doesn't  response anything, 
the following codes work and 'trying again ...'  message is printed out!
         while ( totalBytesRcvd < MSG_SIZE ) {   /* Receive feedback. */
             while (((bytesRcvd=recv(sock, buf, MSG_SIZE, 0)) == -1) && (++rx < 1000)) {
                 printf("After timeout #%d, trying recv again:\n", rx);
                 exit( -1 );
             }
             totalBytesRcvd += bytesRcvd;
         }
 
         free( buf );
     }
 
     close( sock );

# 2  
Old 01-16-2011
That's a mighty big and complicated if-statement. I'd break it out not just for readability purposes, but also because C's operator precedence can be a little strange. It may be evaluating like (-1 || sentBytes) != MSG_SIZE or something silly like that.

Furthermore you should really be checking how much you've sent already and not resending bits you already have...

Code:
// outside loop
int sentTotal=0;

...

// inside loop
sentBytes=send(sock, buf+sentTotal, MSG_SIZE-sentTotal, 0);

// ==-1 is redundant, since -1 < MSG_SIZE
if(sentBytes < (MSG_SIZE-sentTotal))
{
        if(tx > 1000)
        {
                fprintf(stderr, "too many retries, giving up\n");
                break;
        }

        fprintf(stderr, "Timeout #%d, sent %d/%d\n",
                ++tx, sentBytes, MSG_SIZE-sentTotal);

        if(sentBytes > 0)
        {
                sentTotal+=sentBytes;
        }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

-ne 0 doesn't work -le does

Hi, I am using korn shell. until ] do echo "\$# = " $# echo "$1" shift done To the above script, I passed 2 parameters and the program control doesn't enter inside "until" loop. If I change it to until ] then it does work. Why numeric comparison is not working with -ne and works... (3 Replies)
Discussion started by: ab_2010
3 Replies

2. Shell Programming and Scripting

Why my awk doesn't work?

root@SDP_Wildcat_Pass-3-C1:~# cat /proc/driver/rtc rtc_time : 05:29:40 rtc_date : 2014-12-19 alrm_time : 01:51:53 alrm_date : 2014-12-20 alarm_IRQ : no alrm_pending : no update IRQ enabled : no periodic IRQ enabled : no periodic IRQ... (4 Replies)
Discussion started by: yanglei_fage
4 Replies

3. UNIX for Dummies Questions & Answers

Why doesn't this work?

find . -name "05_scripts" -type d -exec mv -f {}/'*.aep\ Logs' {}/.LogFiles \; Returns this failure: mv: rename ./019_0120_WS_WH_gate_insideTEST/05_scripts/*.aep\ Logs to ./019_0120_WS_WH_gate_insideTEST/05_scripts/.LogFiles/*.aep\ Logs: No such file or directory I don't know why it's trying... (4 Replies)
Discussion started by: scribling
4 Replies

4. Shell Programming and Scripting

echo doesn't work right

Hi,when I run my first shell script,I got something that doesn't work right. I wrote this code in the script. echo -e "Hello,World\a\n"But the screen print like this: -e Hello,World The "-e" wasn't supposed to be printed out. Can anyone help me out?:wall: Many thanks!:) (25 Replies)
Discussion started by: Demon
25 Replies

5. Shell Programming and Scripting

my script doesn't work :(

i have this script and when i ejecute it, the console tell me this " sintax error line 41 unexpected element "}" " is the sintaxis ok? #!/bin/bash if ;then { exit 0; } if ; then { sudo /etc/init.d/apache2 start; sudo /etc/init.d/mysql start; php5 & nautilus... (3 Replies)
Discussion started by: keiserx
3 Replies

6. Shell Programming and Scripting

HELP: If Doesn't Work in AWK

Hi! I have a somehow big file (almost 3000 lines long and thirteen columns). Some lines have no value at all or, at least, are incomplete. The columns' values that have no data are marked with a "-" and the corresponding line (the line that owns that value) should be discarded and not used. ... (5 Replies)
Discussion started by: Marcelo de Brit
5 Replies

7. Shell Programming and Scripting

Lipo doesn't work

Hi guys, Am using lipo to merge ppc and i386 version of a static/dylib file based on "file type to load". I am working on Mac OS 10.5.6 and new to shell scripting. Please help me out. This is my code. echo "This file combine ppc and i386 file to form universal library" echo "source... (4 Replies)
Discussion started by: vishwesh
4 Replies

8. Shell Programming and Scripting

if condition doesn't work

i want to get the value for column 4rth when i =4. please guide what i am doing wrong. thanks var=`cat file.csv` for i in $var; do { if ; then var4=$var4+$i fi echo $i } done I am geting this error message "0403-009 The specified number is not valid for this command." (8 Replies)
Discussion started by: sagii
8 Replies

9. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

10. Shell Programming and Scripting

Why doesn't this work?

cat .servers | while read LINE; do ssh jason@$LINE $1 done exit 1 ./command.ksh "ls -l ~jason" Why does this ONLY iterate on the first server in the list? It's not doing the command on all the servers in the list, what am I missing? Thanks! JP (2 Replies)
Discussion started by: jpeery
2 Replies
Login or Register to Ask a Question