Click here to Skip to main content
Licence 
First Posted 10 Jun 2007
Views 11,739
Downloads 340
Bookmarked 18 times

Transfers image through TCP

By | 10 Jun 2007 | Article
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

About the Author

Leon/wzhihai



China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralQuery from Trheiu PinstaffGenevieve Sovereign5:08 4 Oct '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 11 Jun 2007
Article Copyright 2007 by Leon/wzhihai
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid