askingtoomuch
askingtoomuch

Reputation: 537

Separate and display bitmap from a socket stream

How do I separate and display bitmap images from a continuous socket stream of multiple images? Below codes will save and display a single image when there's only one bmp received. How can I separate and display images when the socket stream contains multiple bitmap images (e.g BM .... BM .... BM...) ?

DWORD WINAPI CServerDlg::ThreadSocket( LPVOID lpParam )
{
    CServerDlg *pThis = (CServerDlg *)lpParam;

    SOCKET server;

    WSADATA wsaData;
    int wsaret = WSAStartup( MAKEWORD(2,2), &wsaData );
    if( wsaret != 0 )
    {
        return 0;
    }

    sockaddr_in local;
    local.sin_family = AF_INET; //Address family
    local.sin_addr.s_addr = INADDR_ANY; //Wild card IP address
    local.sin_port = htons((u_short)8888); //port to use

    server = socket( AF_INET, SOCK_STREAM, 0 );
    if( server == INVALID_SOCKET )
    {
        return 0;
    }

    if( bind( server, (sockaddr*)&local, sizeof(local) ) != 0 )
    {
        return 0;
    }
    if( listen( server, 10 ) != 0 )
    {
        return 0;
    }

    SOCKET_STREAM_FILE_INFO     StreamFileInfo;
    memset( &StreamFileInfo, 0, sizeof(SOCKET_STREAM_FILE_INFO) );
    SOCKET client;
    sockaddr_in from;
    int fromlen = sizeof( from );
    while( pThis->m_bListen )
    {
        char temp[1024];
        memset( temp, 0, 1024 );
        client = accept( server, (struct sockaddr*)&from, &fromlen );
        int iLen = recv( client, temp, sizeof(SOCKET_STREAM_FILE_INFO), 0 );
        if( iLen == sizeof(StreamFileInfo) )
        {
            memcpy( &StreamFileInfo, temp, sizeof(StreamFileInfo) );
            CFile destFile( StreamFileInfo.szFileTitle, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
            UINT dwRead = 0;
            while( dwRead < StreamFileInfo.nFileSizeLow )
            {
                memset(temp,0,1024);
                UINT dw = recv( client, temp, 1024, 0 );
                destFile.Write(temp, dw);
                dwRead += dw;
            }
            destFile.Close();

            pThis->LoadPicture( StreamFileInfo.szFileTitle );   // Display image in a dialog box
        }
        closesocket(client);
    }
    closesocket(server);
    WSACleanup();

    return 0;
} 

Upvotes: 0

Views: 213

Answers (2)

John Bandela
John Bandela

Reputation: 2436

The Bitmap format described here

http://en.wikipedia.org/wiki/BMP_file_format

Includes a file size field in the header. You can read that for each bitmap and know where the end of the current bitmap is.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409166

You need to design a protocol that differentiates between the different images.

A simple one that send a header (containing the size (in bytes) of the image, and possibly image type), followed by the actual image data is enough.

Upvotes: 0

Related Questions