用c++开发基于tcp协议的文件上传功能
2005我正在一家游戏公司做程序员,当时一直在看《Windows网络编程》 这本书,把里面提到的每种IO模型都试了一次,强烈推荐学习网络编程的同学阅读,比 APUE 讲的更深入
这是某个银行广告项目(p2p传输视频)的一部分
IO模型采用的阻塞模式,文件一打开就直接上传
用vc 2003编译,生成win32 dll
麻雀虽小五脏俱全,CSimpleSocket,CReadStream
dll 输出一虚类
- extern "C" __declspec(dllexport)
ISendFileInterface * CreateSendFile()
{
return new CFileClient();
}
接口定义如下
- typedef struct
{
unsigned int uploaded; //已经上传的字节数
bool finished;
}TransInfo,*PTransInfo;
struct ISendFileInterface
{
virtual ~ISendFileInterface(){};
//return>0:file size
//return=0:zero byte file or error
virtual unsigned int ChoseFile(const char *file,int type)=0;
//连接服务器
virtual bool Connect(const char *host,unsigned short port)=0;
//阻塞调用
//-1:socket 错误
//0:上传完成
virtual int SendFile(PTransInfo info)=0;
virtual void Stop()=0;
virtual const char *GetError()=0;
};