Thread: TCP Server: Recieve Images and show on window without saving

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    15

    TCP Server: Recieve Images and show on window without saving

    Hello All,
    I'm working on a project in which I'm making a TCP server that receives the images from a client and shows them on window without saving.
    Code:
    case WM_SOCKET:
            {
                switch(WSAGETSELECTEVENT(lParam))
                {
                    case FD_READ:
                    {
                        
                        char recvbuffer[102400];
                        int filebuflen = DEFAULT_BUFLEN;
                        ZeroMemory(recvbuffer, filebuflen);
                        int bytes_read = recv(Socket, recvbuffer, filebuflen, 0 );
                    
                        if(bytes_read==0)
                    
                    break;
                        if(bytes_read <0)
                        {
                        MessageBox(hWnd,
                            "File Read Error",
                            "Corrupt File",
                            MB_ICONINFORMATION|MB_OK);
                        }
    
                        
                        std::ifstream is;
                        char* pbuffer;
                    
    
    
    
                        
                        /* pbuffer = new char[bytes_read];*/
                        bytes_read = is.tellg();
                    /*    is.seekg(0, std::ios::beg);*/
                        pbuffer = new char[bytes_read];
                        is.read(pbuffer, bytes_read);
                    
                        is.close();
                        tagBITMAPFILEHEADER bfh = *(tagBITMAPFILEHEADER*)pbuffer;
                        tagBITMAPINFOHEADER bih = *(tagBITMAPINFOHEADER*)(pbuffer+sizeof(tagBITMAPFILEHEADER));
    
                        RGBQUAD             rgb = *(RGBQUAD*)(pbuffer+sizeof(tagBITMAPFILEHEADER)+sizeof(tagBITMAPINFOHEADER));
    
    
                        BITMAPINFO bi;
    bi.bmiColors[0] = rgb;
    bi.bmiHeader = bih;
    char* pPixels = (pbuffer+bfh.bfOffBits);
    
    char* ppvBits;
    
    
    hbitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**) &ppvBits, NULL, 0);
    SetDIBits(NULL, hbitmap, 0, bih.biHeight, pPixels, &bi, DIB_RGB_COLORS);
    
    GetObject(hbitmap, sizeof(BITMAP), &cBitmap);
    delete[] pbuffer;
                        
    
                    }
                    break;
    The problem is when I'm sending the image from client it is sent but at that time server freezes and hangs and no data received at server side.

    I need urgent help.
    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int bytes_read = recv(Socket, recvbuffer, filebuflen, 0 );
    This won't hang around until the whole file is received.
    The minimum valid success from recv() is just ONE byte.
    You need to put this in a loop to accumulate data until bytes_read is zero, at which point you know you're done.

    > std::ifstream is;
    Did you edit the code before posting?
    Because this block of code involving is makes no sense at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    I was getting stuck in this so I tried with this new approach:
    Code:
    int i=256;
                BYTE* data = new BYTE[256*i];
      int packetsize, num;
      int newWidth, newHeight;
      int recvimgsize=0;
    
       bool bAwaitingImage = false;
                switch(WSAGETSELECTEVENT(lParam))
                {
                    case FD_READ:
                    {
                          num=recv(Socket, (char*)data, 16, 0);
                    
                    if(num>0)
          {
    
              for(int i=0; i<32; i++)
              {
            packetsize = data[i]*256+ data[i];
    
            num=recv(Socket, (char*)(data+16), packetsize-16, 0);
          }
                    }
                     if(num>0)
                     {
                     switch(data[0])
                     {
                      case 2: //received information about window size (image size)
    
                          for(int i=0; i<16; i++)
                          {
                newWidth = data[i]*256+data[i];
                newHeight = data[i]*256+data[i];
                          }
    but the problem is same,,,when client starts sending file the server part hangs.

    Have you any idea of this?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Apart from the awful formatting, and that you're assuming recv() always fills the buffer, I've no idea.

    Before trying to store anything, just print things out.
    Code:
                switch(WSAGETSELECTEVENT(lParam))
                {
                    case FD_READ:
                    {
                          char data[1000];
                          num=recv(Socket, data, sizeof(data), 0);
                          cout << "Received " << num << " bytes" << endl;  // or write it to a file if you have no console
    Make sure you understand how recv works first.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    Thanks for this,,its really helpful!

    Now I got it,,How to receive it but after that how to store is also a problem..
    Can you help me in that too....

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Since you seem to be working in some kind of windows callback / event thing, you first need a static buffer which will persist over time.

    Say
    static char buff[MAX_SIZE];

    Then have two static variables to track progress.
    static size_t buffUsed = 0;
    static size_t buffFree = MAX_SIZE;


    Then your recv becomes
    Code:
    num=recv(Socket, &buff[buffUsed], buffFree, 0);
    if ( num > 0 ) {
        // progress the buffer
        buffUsed += num;
        buffFree -= num;
    } else if ( num == 0 ) {
        // end of connection, do stuff with buff
    } else {
        // It broke
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    @Salem,

    Sir I actually did according to you in my program and now
    when I'm trying to do this then everything goes right beside the thing that server side hangs.

    Thanks!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I've no idea what that error report means.
    How To Ask Questions The Smart Way

    Use a debugger, or put in some debug "print" statements to find out WHERE it is stuck.

    Put in code to check all the error status returns which you're no doubt ignoring.

    Read the manual pages AGAIN to see if you missed an important detail.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    The thing is when I'm trying to read the buffer finally for a bitmap,
    Bitmap is not being created.

    This is my code to read the buffer for a bitmap..

    Code:
    std::ifstream is;
    
                        is.open(buff, std::ios::binary);
                        is.seekg(0, std::ios::end);
                        num = is.tellg();
                        is.seekg(0, std::ios::beg);
                         
    
                        
                        is.read(buff, num);
    
                        tagBITMAPFILEHEADER bfh = *(tagBITMAPFILEHEADER*)buff;
                        tagBITMAPINFOHEADER bih = *(tagBITMAPINFOHEADER*)(buff+sizeof(tagBITMAPFILEHEADER));
    
                        RGBQUAD             rgb = *(RGBQUAD*)(buff+sizeof(tagBITMAPFILEHEADER)+sizeof(tagBITMAPINFOHEADER));
    
    
                        BITMAPINFO bi;
    bi.bmiColors[0] = rgb;
    bi.bmiHeader = bih;
    char* pPixels = (buff+bfh.bfOffBits);
    
    char* ppvBits;
    
    
    hbitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**) &ppvBits, NULL, 0);
    SetDIBits(NULL, hbitmap, 0, bih.biHeight, pPixels, &bi, DIB_RGB_COLORS);
    
    GetObject(hbitmap, sizeof(BITMAP), &cBitmap);
        
    std::ifstream is;
    
                        is.open(buff, std::ios::binary);
                        is.seekg(0, std::ios::end);
                        num = is.tellg();
                        is.seekg(0, std::ios::beg);
                         
    
                        
                        is.read(buff, num);
    
                        tagBITMAPFILEHEADER bfh = *(tagBITMAPFILEHEADER*)buff;
                        tagBITMAPINFOHEADER bih = *(tagBITMAPINFOHEADER*)(buff+sizeof(tagBITMAPFILEHEADER));
    
                        RGBQUAD             rgb = *(RGBQUAD*)(buff+sizeof(tagBITMAPFILEHEADER)+sizeof(tagBITMAPINFOHEADER));
    
    
                        BITMAPINFO bi;
    bi.bmiColors[0] = rgb;
    bi.bmiHeader = bih;
    char* pPixels = (buff+bfh.bfOffBits);
    
    char* ppvBits;
    
    
    hbitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**) &ppvBits, NULL, 0);
    SetDIBits(NULL, hbitmap, 0, bih.biHeight, pPixels, &bi, DIB_RGB_COLORS);
    
    GetObject(hbitmap, sizeof(BITMAP), &cBitmap);
    can you please point out the problem?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    std::ifstream is;
     
                        is.open(buff, std::ios::binary);
                        is.seekg(0, std::ios::end);
                        num = is.tellg();
                        is.seekg(0, std::ios::beg);
                          
     
                         
                        is.read(buff, num);
    ifstream::open - C++ Reference
    istream::read - C++ Reference
    So you're telling me that buff is simultaneously initialised to contain the filename to open, and large enough to hold the contents of the file?.

    Unless you have this, it just won't work
    char buff[1000000] = "image.bmp";

    I don't think you've learnt enough about the basics of programming to tackle this kind of problem yet.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cliene send and recieve from server and vice versa
    By simona in forum Networking/Device Communication
    Replies: 1
    Last Post: 11-10-2011, 11:46 PM
  2. Creating and saving images
    By patrink in forum C Programming
    Replies: 7
    Last Post: 05-03-2011, 11:35 PM
  3. Button Images and Saving
    By tyler4588 in forum Windows Programming
    Replies: 2
    Last Post: 12-10-2004, 09:20 PM
  4. How do I show images in c++
    By 6arredja in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2004, 09:42 AM
  5. Creating a VCD to Show Images
    By Davros in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2003, 04:02 PM