Thread: socket problem

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    socket problem

    I have this function where something very strange happens:

    Code:
    int DGRAMSocketRead_Wait ( int sock, char* iBuffer , unsigned int iToReadByteNumber,  	struct timeval * TimeOut, UINT32 * ReadByteNumber)
    {
    	//to be tested with signaling simulation and partial read (client sends only part of the message or too slowly)
     
    #if WINDOWS || WINDOWSCE
    	int errno;
    #endif
    
     //	int 		ToReadByteNumber;
     	int			RdByteNumber;
     //	unsigned char*		Buffer;
     	
     	fd_set mono_fd_set, read_fd_set;
     	int DataReady;
     	
     	struct timeval  TempTO;
     	
     	Type_LogString LogString;
    	Type_PrefixString Prefix;
    	snprintf(Prefix,sizeof(Prefix),"DGRAMSocketRead_Wait::");
     	
      struct sockaddr from;
      int sizeoffrom;
    
    
     	
        FD_ZERO (&mono_fd_set);
        FD_SET (sock, &mono_fd_set);
     
     	//ToReadByteNumber = iToReadByteNumber;
     	RdByteNumber = -1;
     	//Buffer           = iBuffer;
    
    
    	
        read_fd_set = mono_fd_set; 
     	TempTO=*TimeOut;
        //to be done upper limit on TimeOut in case of signal
    	DataReady= select (FD_SETSIZE, &read_fd_set, NULL, NULL, &TempTO);
        while (DataReady < 0) 
        { 
    		#if WINDOWS || WINDOWSCE
    			errno=WSAGetLastError();
    //{snprintf(LogString,sizeof(LogString),"dbg:: errno %d",errno); Log(&Lg,Prefix,LogString,2,LOG_ON_ERROR_ALSO);}
    			//In the previous line : if I put snprintf out of comment it is not executed but the whole function works correctly reading the packet of the size expected!!??
    			//if I put snprintf under comment strangely enough I get : Log(&Lg,Prefix,"TIME OUT",2,LOG_ON_ERROR_ALSO);return MYSOCKET_USER_DEFINED_TIMEOUT;
    					#endif
    #if LINUX
    		if (errno!=EINTR)
    #endif
    			{
    	     	 		Log(&Lg,Prefix,"Read Error",2,LOG_ON_ERROR_ALSO);
    	           		return MYSOCKET_ERROR;
    	      	}
    	        read_fd_set = mono_fd_set; 
    	     	TempTO=*TimeOut;//TempT0;//it is not POSIX compliant to use TempTO cause it is not decremented in all implementations
        		DataReady=select (FD_SETSIZE, &read_fd_set, NULL, NULL, &TempTO);
        }  
        
        if (!DataReady)
        {
        	Log(&Lg,Prefix,"TIME OUT",2,LOG_ON_ERROR_ALSO);
        	*TimeOut=TempTO;
      		return MYSOCKET_USER_DEFINED_TIMEOUT;
        }
        
        
         while ( RdByteNumber < 0 )
         {        
     		//in case of EINTR chars should not be lost before the first read
    
            
    	    	RdByteNumber = recvfrom( sock , iBuffer , iToReadByteNumber, NULL, &from, &sizeoffrom  );
    		#if WINDOWS || WINDOWSCE
    			errno=WSAGetLastError();
    		#endif
    	    	if 	( RdByteNumber < 0 )
    	       	{
    	    		if (errno==MYEAGAIN || errno==MYEWOULDBLOCK )//some Linux do this if the data was lost after select() for some (strange) reason, like check sum
    	    		{
    	    			*ReadByteNumber=0;
    	    			return MYSOCKET_DATA_FALLEN;
    	    		}
    	    		else
    #if LINUX
    					if	( errno != EINTR )//also SERVER_NON_CATASTROFIC_ERRNO must cause an error return code here
    #endif
    	          	{
    	       			Log(&Lg,Prefix,"Read Error",2,LOG_ON_ERROR_ALSO);
    	          		return MYSOCKET_ERROR;
    	          	}
    	       	}
    	    	else if ( ReadByteNumber == 0 )
    	       	{
    	    		Log(&Lg,Prefix,"Connection closed",2,LOG_ON_ERROR_ALSO);
    	       		return MYSOCKET_CONNECTION_CLOSED;
    	       	}
    	    	else ;
    	    	//{
    		    //	ToReadByteNumber = ToReadByteNumber - ReadByteNumber;
    		    //	Buffer = Buffer + ReadByteNumber;
    	    	//}
    
            
        }
        *ReadByteNumber=RdByteNumber;
     	return MYSOCKET_OK;
     
    }
    where the strange behaviour is this in short:
    Code:
    //{snprintf(LogString,sizeof(LogString),"dbg:: errno %d",errno); Log(&Lg,Prefix,LogString,2,LOG_ON_ERROR_ALSO);}
    			//In the previous line : if I put snprintf out of comment it is not executed but the whole function works correctly reading the packet of the size expected!!??
    			//if I put snprintf under comment strangely enough I get : Log(&Lg,Prefix,"TIME OUT",2,LOG_ON_ERROR_ALSO);return MYSOCKET_USER_DEFINED_TIMEOUT;

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    it is very likely - you have some buffer overrun that corrupts your stack... when sprintf is uncommented - the corruption does not prevent function from executing what it should, when you uncomment that line - something essential is overwritten.

    I would started with checking that all your automatic vars have enough space for storing what you pass into them...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by vart View Post
    it is very likely - you have some buffer overrun that corrupts your stack... when sprintf is uncommented - the corruption does not prevent function from executing what it should, when you uncomment that line - something essential is overwritten.

    I would started with checking that all your automatic vars have enough space for storing what you pass into them...
    i agree

  4. #4
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by vart View Post
    it is very likely - you have some buffer overrun that corrupts your stack... when sprintf is uncommented - the corruption does not prevent function from executing what it should, when you uncomment that line - something essential is overwritten.

    I would started with checking that all your automatic vars have enough space for storing what you pass into them...
    i have disabled the directive /GL (link time code generation) (I am using MS Visual 2008 Express Ed. with Berkley sockets) and the problem seem to have vanished, but perhaps just in a way similar to excluding the comment of the snprintf (perhaps it didn't really solved)..

    i didn't find any memory leak or problem for the moment..
    Last edited by mynickmynick; 06-17-2010 at 01:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  3. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 04:46 AM
  4. sockets problem programming
    By kavejska in forum C Programming
    Replies: 0
    Last Post: 07-25-2005, 07:01 AM
  5. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 10:01 AM