How to read large data from socket

    • 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,
    • If you are talking about Java you should have put this in the Java area of the forums. This is the C++ area.
      HP Laptop / AMD VISION A4 APU / AMD Radeon HD 6480G discrete-class /
      Windows 7 Home Premium 64-Bit
      Microsoft Visual Studio Express 2013 for Windows Desktop
      Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
    • 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 ?

      Source Code

      1. int message_buffer_size_with_hd = 0;
      2. unsigned char *message_hd = new unsigned char[2];
      3. memset(message_hd,'\0',2);
      4. unsigned char *message_buffer = NULL;
      5. int actual_message_size = read(sfd, message_hd, 2);
      6. if(actual_message_size >0){
      7. if(actual_message_size < 65535){
      8. char message_size_in_hex[5];
      9. memset(message_size_in_hex,'\0',5);
      10. sprintf(message_size_in_hex, "%02x",message_hd[0]);
      11. sprintf(message_size_in_hex+2,"%02x",message_hd[1]);
      12. int message_size_in_decimal = 0 ;
      13. sscanf(message_size_in_hex, "%x", &message_size_in_decimal);
      14. unsigned char *message_buffer_without_hd = new unsigned char[message_size_in_decimal];
      15. memset(message_buffer_without_hd, '\0',message_size_in_decimal);
      16. int bytesRead = 0;
      17. int result;
      18. while (bytesRead < message_size_in_decimal)
      19. {
      20. result = read(sfd, message_buffer_without_hd + bytesRead, (message_size_in_decimal - bytesRead));
      21. if (result < 1 )
      22. {
      23. break;
      24. }
      25. bytesRead += result;
      26. }
      27. message_buffer_size_with_hd = message_size_in_decimal + 3;
      28. message_buffer = new unsigned char[message_buffer_size_with_hd];
      29. memset(message_buffer,'\0',message_buffer_size_with_hd);
      30. memcpy(message_buffer,message_hd,2);
      31. memcpy(message_buffer+2,message_buffer_without_hd,message_size_in_decimal);
      32. int p;
      33. for (p = 0; p < message_buffer_size_with_hd; p++) {
      34. printf("%02X", message_buffer[p]);
      35. }
      36. delete []message_buffer;
      37. delete []message_buffer_without_hd;
      38. delete []message_hd;
      39. message_hd = NULL;
      40. }
      41. }
      42. if (message_hd !=NULL) delete []message_hd;
      43. }
      Display All
    • 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.
      HP Laptop / AMD VISION A4 APU / AMD Radeon HD 6480G discrete-class /
      Windows 7 Home Premium 64-Bit
      Microsoft Visual Studio Express 2013 for Windows Desktop
      Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
    • 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.
      HP Laptop / AMD VISION A4 APU / AMD Radeon HD 6480G discrete-class /
      Windows 7 Home Premium 64-Bit
      Microsoft Visual Studio Express 2013 for Windows Desktop
      Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
    • I've done small changers as bellow


      Source Code

      1. int message_buffer_size_with_hd = 0;
      2. unsigned char *message_hd = new unsigned char[2];
      3. memset(message_hd,'\0',2);
      4. unsigned char *message_buffer = NULL;
      5. int message_hd_size = read(sfd, message_hd, 2);
      6. if(message_hd_size >0){
      7. char message_size_in_hex[5];
      8. memset(message_size_in_hex,'\0',5);
      9. sprintf(message_size_in_hex, "%02x",message_hd[0]);
      10. sprintf(message_size_in_hex+2,"%02x",message_hd[1]);
      11. int message_size_in_decimal = 0 ;
      12. sscanf(message_size_in_hex, "%x", &message_size_in_decimal);
      13. if(message_size_in_decimal < 65535){
      14. unsigned char *message_buffer_without_hd = new unsigned char[message_size_in_decimal];
      15. memset(message_buffer_without_hd, '\0',message_size_in_decimal);
      16. int bytesRead = 0;
      17. int result;
      18. while (bytesRead < message_size_in_decimal)
      19. {
      20. result = read(sfd, message_buffer_without_hd + bytesRead, (message_size_in_decimal - bytesRead));
      21. if (result < 1 )
      22. {
      23. break;
      24. }
      25. bytesRead += result;
      26. }
      27. message_buffer_size_with_hd = message_size_in_decimal + 3;
      28. message_buffer = new unsigned char[message_buffer_size_with_hd];
      29. memset(message_buffer,'\0',message_buffer_size_with_hd);
      30. memcpy(message_buffer,message_hd,2);
      31. memcpy(message_buffer+2,message_buffer_without_hd,message_size_in_decimal);
      32. delete []message_buffer;
      33. delete []message_buffer_without_hd;
      34. delete []message_hd;
      35. message_hd = NULL;
      36. }
      37. }
      38. if (message_hd !=NULL) delete []message_hd;
      Display All



      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

      Source Code

      1. #include "../pool/PoolRequestQueue.h"
      2. #ifndef PREQUESTRECIVER_H_
      3. #define PREQUESTRECIVER_H_
      4. namespace std {
      5. class PRequestReciver {
      6. public:
      7. PRequestReciver(LISTENER_INFOR *listner,int socket,int lisid,char *ip);
      8. virtual ~PRequestReciver();
      9. private:
      10. static void *prequestReciver(void *ptr);
      11. bool isPrequestRunner;
      12. int clientsocket;
      13. int listId;
      14. char *ip;
      15. LISTENER_INFOR *listenerData;
      16. };
      17. }
      18. #endif
      Display All
    • 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.
      HP Laptop / AMD VISION A4 APU / AMD Radeon HD 6480G discrete-class /
      Windows 7 Home Premium 64-Bit
      Microsoft Visual Studio Express 2013 for Windows Desktop
      Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary