MFC局域网文件传输client端

// FileClient.cpp: implementation of the CFileClient class.
//
//

#include "stdafx.h"
#include "FeiQ.h"
#include "FileClient.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
// Construction/Destruction
//

CFileClient::CFileClient()
{
    m_ClientSocket = INVALID_SOCKET;
}

CFileClient::~CFileClient()
{
    if( INVALID_SOCKET != m_ClientSocket )
    {
        closesocket( m_ClientSocket );
    }
}

BOOL CFileClient::InitClient( )
{   //创建SOCKET
    m_ClientSocket = socket( AF_INET, 
        SOCK_STREAM, IPPROTO_TCP );
    if( INVALID_SOCKET == m_ClientSocket )
    {
        return FALSE;
    }
    return TRUE;
}

BOOL CFileClient::ConnectServer( LPCSTR pszSvrIP )
{   //连接服务器端
    SOCKADDR_IN addr = { 0 };
    addr.sin_family = AF_INET;
    addr.sin_port   = htons( PORT_FILESVR );
    addr.sin_addr.S_un.S_addr = 
        inet_addr( pszSvrIP );
    if( SOCKET_ERROR == connect( m_ClientSocket, 
        (SOCKADDR *)&addr, sizeof( addr ) ) )
    {
        return FALSE;
    }

    return TRUE;
}

BOOL CFileClient::SendFile( LPCTSTR pszFilePath,
                            CProgressCtrl * pwndProgress )
{
    //发送数据头
    NETHEADER header = { 0 };
    header.nVersion  = 1;
    header.nCmdID    = NETCMDID_SENDFILE;
    header.nDataLength = sizeof( SENDFILE );
    SendData( &header, sizeof( header) );
    
    //发送文件信息
    CFile file;
    if( FALSE == file.Open( pszFilePath, 
        CFile::modeRead ) )
    {
        return FALSE;
    }

    SENDFILE sendfile = { 0 };
    strcpy( sendfile.szFileName, file.GetFileName( ) );
    sendfile.nFileLength = file.GetLength( );
    SendData( &sendfile, sizeof( sendfile ) );

    //发送文件数据
    if( pwndProgress != NULL )
    {
        pwndProgress->SetRange32( 0, 
            sendfile.nFileLength );
    }
    CHAR  szBuf[4096] = { 0 };
    DWORD nLeft = file.GetLength( );
    while( nLeft > 0 )
    {   //计算读取的数据量
        DWORD nSend = 4096;
        if( nLeft < nSend )
        {
            nSend = nLeft;
        }
        //读取数据
        file.Read( szBuf, nSend );
        //发送数据
        SendData( szBuf, nSend );
        //计算剩余数据量
        nLeft = nLeft - nSend;
        //更新进度条
        if( pwndProgress != NULL )
        {
            pwndProgress->SetPos( 
                sendfile.nFileLength - nLeft );
        }
    }
    
    //关闭文件
    file.Close( );
    return TRUE;
}

BOOL CFileClient::SendData( LPVOID pData, 
                            DWORD nLen )
{
    LPSTR pTmpData = ( LPSTR )pData;
    int   nLeft    = nLen;
    while( nLeft > 0 )
    {
        int nSend = send( m_ClientSocket, 
            pTmpData, nLeft, 0 );
        if( SOCKET_ERROR == nSend )
        {
            return FALSE;
        }
        nLeft = nLeft - nSend;
        pTmpData = pTmpData + nSend;
    }

    return TRUE;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值