Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Transfers image through TCP

1.36/5 (11 votes)
10 Jun 2007 1   1.4K  
TCP clients Transfers image to TCP server, TCP server show image throuth OleLoadPicture
Screenshot - images.jpg

Introduction

The original article proposed to demonstrate elementary network programming by designing a very simple TCP client/server and a simple image viewer. The client select an image file it will send to the server. The server will save and show the image file.

The Client Mainly Code

SOCKET_STREAM_FILE_INFO     StreamFileInfo;
WIN32_FIND_DATA             FindFileData;
FindClose( FindFirstFile( strFileName, &FindFileData ) );
memset( &StreamFileInfo, 0, sizeof(SOCKET_STREAM_FILE_INFO) );
strcpy( StreamFileInfo.szFileTitle, myFile.GetFileTitle() );
StreamFileInfo.dwFileAttributes     =       FindFileData.dwFileAttributes;
StreamFileInfo.ftCreationTime       =       FindFileData.ftCreationTime;
StreamFileInfo.ftLastAccessTime     =       FindFileData.ftLastAccessTime;
StreamFileInfo.ftLastWriteTime      =       FindFileData.ftLastWriteTime;
StreamFileInfo.nFileSizeHigh        =       FindFileData.nFileSizeHigh;
StreamFileInfo.nFileSizeLow         =       FindFileData.nFileSizeLow;
GetDlgItem( IDC_IPADDRESS1 )->GetWindowText( strIpAddress );
//////////////////////////////////////////////////////////////////////////
SOCKET server;
WSADATA wsaData;
int wsaret = WSAStartup( MAKEWORD(2,2), &wsaData );
if( wsaret != 0 )
{
    return;
}
sockaddr_in local;
local.sin_family = AF_INET; //Address family
local.sin_addr.s_addr = inet_addr( strIpAddress ); //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;
}
connect( server,(struct sockaddr *)&local, sizeof(local));
send( server, (char *)&StreamFileInfo,sizeof(SOCKET_STREAM_FILE_INFO), 0 );
Sleep( 10 );
UINT dwRead=0;
char temp[1024];
while( dwRead < StreamFileInfo.nFileSizeLow )
{
   memset( temp, 0, 1024 );
   UINT dw = myFile.Read( temp, 1024 );
   send( server, temp, dw, 0 );
   dwRead += dw;
   Sleep( 10 );
}
myFile.Close();
closesocket(server);
WSACleanup();

The Server Mainly Code

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;
        }
        SetFileTime( (HANDLE)destFile.m_hFile, 
            &StreamFileInfo.ftCreationTime, &StreamFileInfo.ftLastAccessTime, 
            &StreamFileInfo.ftLastWriteTime );
        destFile.Close();
        SetFileAttributes(StreamFileInfo.szFileTitle,
            StreamFileInfo.dwFileAttributes);
        pThis->LoadPicture( StreamFileInfo.szFileTitle );
    }
    closesocket(client);
}
closesocket(server);
WSACleanup();

Load Image File

CFile file( strPictureName, CFile::modeRead );
if (file.GetLength() == 0)
{
    return;
}
CArchive loadArchive( &file, CArchive::load | CArchive::bNoFlushOnDelete);
loadArchive.m_bForceFlat = FALSE;
CArchiveStream arcstream(&loadArchive);
m_spIPicture.Release();
HRESULT hr = OleLoadPicture( &arcstream, 0, FALSE, IID_IPicture, 
    (void**)&m_spIPicture);
loadArchive.Close();
file.Close();

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here