How to read large data from socket

Dear All,

I'm working with socket programming (client) to read large data size from server. the server will be sending the data in bellow format

First 2 digits - length of the packet in hex

Example,
000A60000400000200203805 (000A is the message length in hex)

The problem is that when message length is more than 7000, it's not reading entire data in socket tunnel. Further I have just checked in java using readfully() method then it's working properly (read entire length of message)

Please anyone has expert for this ?


Regards,

Comments

  • If you are talking about Java you should have put this in the Java area of the forums. This is the C++ area.
  • I'm here for C++ (my explanation was that it's working for Java)
  • Hi,

    I'm trying in bellow code. Anyway, it's working for me without any issue. May I know any optimization that can be applied more ?
    	   int message_buffer_size_with_hd = 0;
    	   unsigned char *message_hd = new unsigned char[2];
    	   memset(message_hd,'\0',2);
    	   unsigned char *message_buffer = NULL;
    
    
    	   
    	   int actual_message_size = read(sfd, message_hd, 2);
    
    
    	   if(actual_message_size >0){
    
    		   if(actual_message_size < 65535){
    
    			   char message_size_in_hex[5];
    			   memset(message_size_in_hex,'\0',5);
    			   sprintf(message_size_in_hex,  "%02x",message_hd[0]);
    			   sprintf(message_size_in_hex+2,"%02x",message_hd[1]);
    
    			   int message_size_in_decimal = 0 ;
    			   sscanf(message_size_in_hex, "%x", &message_size_in_decimal);
    
    			   unsigned char *message_buffer_without_hd = new unsigned char[message_size_in_decimal];
    			   memset(message_buffer_without_hd, '\0',message_size_in_decimal);
    
    
    			   int bytesRead = 0;
    			   int result;
    			   while (bytesRead < message_size_in_decimal)
    			   {
    			         result = read(sfd, message_buffer_without_hd + bytesRead, (message_size_in_decimal - bytesRead));
    			         if (result < 1 )
    			         {
    
    			             break;
    			         }
    
    			         bytesRead += result;
    			   }
    
    
    			   message_buffer_size_with_hd = message_size_in_decimal + 3;
    			   message_buffer = new unsigned char[message_buffer_size_with_hd];
    			   memset(message_buffer,'\0',message_buffer_size_with_hd);
    
    			   memcpy(message_buffer,message_hd,2);
    			   memcpy(message_buffer+2,message_buffer_without_hd,message_size_in_decimal);
    
    			   int p;
    			   for (p = 0; p < message_buffer_size_with_hd; p++) {
    
    				printf("%02X", message_buffer[p]);
    
    			   }
    
    			   delete []message_buffer;
    			   delete []message_buffer_without_hd;
    			   delete []message_hd;
    			   message_hd = NULL;
    
    		   }
    	   }
    
    	   if (message_hd !=NULL) delete []message_hd;
    
    
    	}
    
    
  • This is not C++ or Java it is C. I might be able to help if you provide the Header Files that go with the code.
  • Strange
     This is the C++ area.
    

    Anyway thanks
  • Yes I know but the code you posted is C. Not C++. Never the less it is to late now. What are the header files for the code, so we can get on to hopefully fixing it.
  • I've done small changers as bellow

    int message_buffer_size_with_hd = 0;
    unsigned char *message_hd = new unsigned char[2];
    memset(message_hd,'\0',2);
    unsigned char *message_buffer = NULL;
    
    int message_hd_size = read(sfd, message_hd, 2);
    
    if(message_hd_size >0){
    
       char message_size_in_hex[5];
       memset(message_size_in_hex,'\0',5);
       sprintf(message_size_in_hex,  "%02x",message_hd[0]);
       sprintf(message_size_in_hex+2,"%02x",message_hd[1]);
    
    
       int message_size_in_decimal = 0 ;
       sscanf(message_size_in_hex, "%x", &message_size_in_decimal);
    
    
    if(message_size_in_decimal < 65535){
    
       unsigned char *message_buffer_without_hd = new unsigned char[message_size_in_decimal];
       memset(message_buffer_without_hd, '\0',message_size_in_decimal);
    
    
       int bytesRead = 0;
       int result;
       while (bytesRead < message_size_in_decimal)
       {
    	result = read(sfd, message_buffer_without_hd + bytesRead, (message_size_in_decimal - bytesRead));
    	if (result < 1 )
    	{
    
    		break;
    	}
    
    	bytesRead += result;
       }
    
    
       message_buffer_size_with_hd = message_size_in_decimal + 3;
       message_buffer = new unsigned char[message_buffer_size_with_hd];
       memset(message_buffer,'\0',message_buffer_size_with_hd);
    
       memcpy(message_buffer,message_hd,2);
       memcpy(message_buffer+2,message_buffer_without_hd,message_size_in_decimal);
    
    
       delete []message_buffer;
       delete []message_buffer_without_hd;
       delete []message_hd;
    
       message_hd = NULL;
    
    }
     
    }
    if (message_hd !=NULL) delete []message_hd;
    
    


    In fact, the connection mode is the established one and handling above chuck inside of ' while loop'

    And point out me anything wrong above ? other stuff is linked different modules (queue , threading etc.. )

    Anyway as u request here is header file
    #include "../pool/PoolRequestQueue.h"
    #ifndef PREQUESTRECIVER_H_
    #define PREQUESTRECIVER_H_
    
    namespace std {
    
    class PRequestReciver {
    
    public:
    	PRequestReciver(LISTENER_INFOR *listner,int socket,int lisid,char *ip);
    	virtual ~PRequestReciver();
    
    private:
    	static void *prequestReciver(void *ptr);
    
    	bool isPrequestRunner;
    	int clientsocket;
    	int listId;
    	char *ip;
    	LISTENER_INFOR *listenerData;
    
    };
    
    }
    #endif
    
    
  • By header files I was hoping for the non multiple kinds. Sure I have done them before but it has been, so long my brain might disintegrate doing it this late at night. Hopefully a moderator chips in soon. The code does look a bit cleaner.
Sign In or Register to comment.