Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-27-2013, 12:04 PM   #1
LQ Newbie
Registered: Feb 2013
Posts: 1

Rep: Reputation: Disabled
Getting O_NONBLCOK to work on Redhat linux TCP C

Hi I am new to this forum, but I look forward to asking and answering questions. I have just recently started doing development on linux. I have taken some classes on linux in the past, but it has been a while. Please bare with me if some of my questions are noobish.



Lately I have been trying to get O_NONBLOCK working on Redhat linux to no success. I am using FNCTL() to turn on O_NONBLOCK right before the initial connection. After the connection I check and read out the ERRNO for debugging purposes. My problem is that I am not getting the expected ERRNO of EAGAIN. I am inconsistently getting back 5, 40, and 11. Is there something I am doing wrong? This is all so I can use KEEPALIVE to reset my connection if a probe times out.

I have listed out snippets my code below:

Connection Function
long   ConnectSocket(long Socket, char *pServerAddress, char Family, ULONG ServerPort )
   long    rc;
   struct sockaddr_in ServerAdd;
   //Var for O_NONBLOCKING
   int flags;
   //var to re-set socket's timeout value ~WB
   struct timeval timeout;
   //creates a variable for KEEPALIVE's optval parm', WB
   int optval;
   //creates a variable for KEEPALIVE's optlen parm', WB
   socklen_t optlen = sizeof(optval); 

   memset( &ServerAdd, 0, sizeof( ServerAdd ) );
   ServerAdd.sin_family = Family;
   ServerAdd.sin_port   = htons( ServerPort );

   ServerAdd.sin_addr.s_addr = inet_addr( pServerAddress );
   if ( (long) ServerAdd.sin_addr.s_addr == -1)
      if (  (pHost = s_gethostbyname(pServerAddress))) //does not return NULL
         memcpy(&ServerAdd.sin_addr, pHost->h_addr, pHost->h_length);
         if (ConnectRetries == 0  &&  ErrorsBeforeHandshake == 0)
            LogMessage(&LogStruct, ERROR, LOGQUEUE + LOGSCREEN,
               "Could not resolve host name\n");
            LogMessage(&LogStruct, ERROR, LOGSCREEN,
               "Could not resolve host name\n");
         CloseSocket( Socket, 0 );

   rc = s_connect( Socket, (struct sockaddr *)&ServerAdd, sizeof( ServerAdd ));
   //initialize flags var, WB      
   flags = fcntl(Socket, F_GETFL, 0);
   //turns on O_NONBLOCKING, WB
   fcntl(Socket, F_SETFL, flags | O_NONBLOCK);

  LogMessage(&LogStruct, INFO, LOGQUEUE + LOGSCREEN,
			"right after the initial connection, WB and errno is %d\n", errno);

  LogMessage(&LogStruct, INFO, LOGQUEUE + LOGSCREEN,
            "right after the initial connection, WB, O_NONBLOCK is %ld", fcntl(Socket,F_SETFL,flags));

   if(fcntl(Socket,F_SETFL,flags) < 0)
		LogMessage(&LogStruct, INFO, LOGQUEUE + LOGSCREEN,
            "O_NONBLOCK is not on, errno is %ld", errno);

   // sets KEEPALIVE parms, WB
   optval = 1;
   optlen = sizeof(optval);

	// turns on KEEPALIVE property on socket, WB
	setsockopt (Socket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen);

   if ( rc == 0)
      if (ErrorsBeforeHandshake == 0)
         LogMessage(&LogStruct, INFO, LOGQUEUE + LOGSCREEN,
            "Socket Established id =  %ld", Socket);
         LogMessage(&LogStruct, INFO, LOGSCREEN,
            "Socket Established id =  %ld", Socket);
      ConnectRetries = 0;
      if (ConnectRetries == 0  &&  ErrorsBeforeHandshake == 0)
         LogMessage(&LogStruct, INFO, LOGQUEUE + LOGSCREEN,
            "Connection Error rc = %ld (40 = Connection refused by host)", errno);
         LogMessage(&LogStruct, ERROR, LOGSCREEN,
            "Connection Error rc = %ld", errno);
      CloseSocket( Socket, 0 );
   return( rc );
void R_main(void *arg)
   long len;

   printf("R thread - starting\n");

   MainTcpipIndex = TcpipIndex = 0;
//   printf("MainTcpipIndex = %d\n", MainTcpipIndex);
   while ( fMainShutdown == FALSE )

      s_MuxWait(5000, "", &hevTcpipBuffAvail[MainTcpipIndex], 0);
      if ( hevTcpipBuffAvail[MainTcpipIndex] == 0)

      len = s_recv( lSocket, TcpipRecord[MainTcpipIndex].TcpipBuffer,
	  LogMessage(&LogStruct, INFO, LOGQUEUE + LOGSCREEN,
			"right after the recv(), WB and len is %d\n", len);

      if (len < 0 && errno != 11 || len == 0)  //0 = connection broken by host
			 if (errno == ETIMEDOUT)
				LogMessage(&LogStruct, INFO, LOGQUEUE + LOGSCREEN, "keepalive probe timed out, WB\n");
			 ReceiveError = errno;
			 hevReceiveError = 1;
			 printf("R_main - set hevReceiveError ON\n");
			 //LogMessage( pMpsStructMain->pLogStruct, ERROR, LOGQUEUE + LOGSCREEN,
				   //"R_main - set hevReceiveError ON" );
	  if (len == -1 && errno == 11)
			 LogMessage(&LogStruct, INFO, LOGQUEUE + LOGSCREEN, "Caught the O_NONBLOCKING\n");
			 len = 0;
	  // Record Length
	  TcpipRecord[MainTcpipIndex].ulTcpipBufLen = len;

	 //printf("Received message on index %d\n", MainTcpipIndex);
	  // Tell T Thread we have a message
	  hevTcpipBuffUsed[MainTcpipIndex] = 1;
	  hevTcpipBuffAvail[MainTcpipIndex] = 0;

	  // Maintain Index
	  if ( ++MainTcpipIndex >= MAX_TCPIP_BUFFS )
		 MainTcpipIndex = 0;
      }//end while
Old 02-27-2013, 07:48 PM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,707

Rep: Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497
I'd recommend using the Report button to ask the Mods to move this to the Programming forum.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to open tcp port 8080 in redhat linux shankarpanda003 Linux - Newbie 2 01-04-2013 03:04 AM
not work: iptables -I INPUT 5 -m state --state NEW -m tcp -p tcp --dport 3306 -j DROP abefroman Linux - Security 1 07-18-2007 09:19 AM
Linux change TCP kernel Parameter for TCP DELAY ACK TICKS linux_mando Linux - Networking 5 08-22-2006 09:20 AM
DG/UX and RedHat Linux Having TCP/IP Stack Issues TonyBerry Linux - Networking 2 01-25-2006 06:55 PM
"Error on Listen function : The TCP/IP- Lotus Domino 6.5 on Redhat Linux vsadvilkar General 0 01-12-2005 05:21 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:05 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration