包长度
文件类型1(gif)
文件长度
数据
文件类型2(txt)
文件长度
数据
------------------
用socket发送和接收
源代码

解决方案 »

  1.   

    // data.h: interface for the CData class.
    //
    //////////////////////////////////////////////////////////////////////#if !defined(AFX_DATA_H__F849DA10_B160_11CF_BC5B_5254AB4B2098__INCLUDED_)
    #define AFX_DATA_H__F849DA10_B160_11CF_BC5B_5254AB4B2098__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    #include "define.h"class CData
    {
    public:
    CData();
    virtual ~CData();
    //methods and operations
    public:
    virtual void Build();             //将数据缓冲区中的数据解码到成员中
    virtual BOOL Load();              //填充数据缓冲区
    //data members
    public:
    BYTE    type ;                    //消息类型
    BYTE    WndIndex;                 //处理该消息的窗口句柄索引
    DWORD   flag    ;                 //消息标记 UINT    dstPort ;                 //目的IP地址
    UINT    dstIP ;                   //目的端口号
    char    buffer[Max_Data_Length] ; //数据缓冲区
    WORD    buf_len ;                 //数据缓冲区中数据长度
    WORD    buf_offset;               //数据缓冲区中特殊数据的偏移private:protected:

    };inline void CopyData(CData *dst,CData *src)
    {
    for ( int i = 0 ;i < src->buf_len ; i ++ )
    dst->buffer[i] = src->buffer[i] ;
    dst->type       = src->type       ;
    dst->dstIP      = src->dstIP      ;
    dst->dstPort    = src->dstPort    ;
    dst->buf_len    = src->buf_len    ;
    dst->buf_offset = src->buf_offset ;
    dst->WndIndex   = src->WndIndex   ;
    }
    #endif // !defined(AFX_DATA_H__F849DA10_B160_11CF_BC5B_5254AB4B2098__INCLUDED_)
    /////////////////////////////////////////////////////////////////
    // data.cpp: implementation of the CData class.
    //
    //////////////////////////////////////////////////////////////////////#include "stdafx.h"
    #include "data.h"
    #include "define.h"#ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #define new DEBUG_NEW
    #endif//////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////CData::CData()
    {
    type       = MSG_USER_DEFINE ;
    WndIndex   = 0 ;
    dstPort    = DEFAULT_DST_PORT ;
    dstIP      = inet_addr(DEFAULT_DST_IP);
    buf_len    = 0 ;
    buf_offset = 0 ;
    WndIndex   = -1;
    flag       = 0 ;
    memset(buffer,0,Max_Data_Length);
    }CData::~CData()
    {}
    void CData::Build()
    {
    char *cp = NULL ; cp = buffer ; //开头是固定数据
    *(BYTE *)cp = type ;      //消息类型
    cp += sizeof(BYTE) ; *(BYTE *)cp = WndIndex ;  //窗口句柄索引
    cp += sizeof(BYTE); *(DWORD *)cp = flag ;     //消息标记
    cp += sizeof(DWORD) ;    //以下是附加数据的开始地址
    buf_len    = cp - buffer ;
    buf_offset = cp - buffer ;//用户数据的偏移量
    }BOOL CData::Load()
    {
    char *cp  = buffer ; type = *(BYTE *)cp ;      // 消息类型
    cp += sizeof(BYTE) ; WndIndex = *cp ;          //窗口句柄索引
    if( WndIndex < 0 && WndIndex >= MaxRecvWndCount )
    return FALSE ;
    cp += sizeof(WndIndex); flag = *(DWORD *)cp;
    cp += sizeof(DWORD); buf_offset = cp - buffer ;//用户数据的偏移量 if( buf_offset <= buf_len )return TRUE ;
    return FALSE ;
    }//////////////////////////////////////////////////////
    class CMsgChat : public CData
    {
    public:
    CMsgChat();
    virtual ~CMsgChat()   ;
    public:
    virtual BOOL Load()   ;
    virtual void Build() ;
    public:
    WORD    srcID    ;     //source ID
    WORD    dstID    ;     //destination ID
    WORD    nLen     ;     //发送内容长度
    char    content[Max_Content_Length];  //内容
    };
    //////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////
    //       implementation of the CMsgChat class.         //
    /////////////////////////////////////////////////////////
    /********************************************************
    Construction CMsgRegist Class's
    *********************************************************/
    CMsgChat::CMsgChat()
    {
    nLen = 0 ;
    srcID= 0 ;
    memset(content,0,Max_Content_Length);
    }CMsgChat::~CMsgChat()
    {}
    /********************************************************
    Encode CMsgChat Class's buffer
    *********************************************************/
    void CMsgChat::Build()
    {
    CData::Build();
    char *cp = (char *)(buffer + buf_offset );
    *(WORD *)cp = srcID ;
    cp += sizeof(WORD)   ;
    *(WORD *)cp = dstID ;
    cp += sizeof(WORD)   ;
    *(WORD *)cp = nLen   ;
    cp += sizeof(WORD)   ;
    memcpy(cp,content,nLen);
    cp += nLen ;
    buf_len = cp - buffer ;
    }
    /********************************************************
    Decode CMsgChat Class's buffer
    *********************************************************/
    BOOL CMsgChat::Load()
    {
    if( !CData::Load() )
    return FALSE ;
    char *cp = (char *)(buffer + buf_offset );
    srcID = *(WORD *)cp ;
    cp += sizeof(WORD)   ;
    dstID = *(WORD *)cp ;
    cp += sizeof(WORD)   ;
    nLen = *(WORD *)cp   ;
    cp += sizeof(WORD)   ;
    if(nLen > Max_Content_Length )
    memcpy(content,cp,Max_Content_Length);
    else memcpy(content,cp,nLen);
    cp += nLen ; if( cp - buffer > buf_len )
    return FALSE ;
    return TRUE ;
    }
    如果需要更为详细的代码,mail:[email protected]