买了本微软的Windows网络编程一书,看了看,写我第一个网络程序:文件传送程序
2006-01-29 11:05
369 查看
这程序很简单,主要目的是:验证我对Winsock,select的理解,半年后写的第一个程序,练手为主
原理:发送端:获取某个文件,将这文件分解,用循环将这文件发送出去;接受端:循环接受那文件,将其组合,存入某个文件里.
服务端:
void SendFile()
{
#define PORT 34000 /// 端口
AfxSocketInit(NULL);
CSocket sockSrvr;
sockSrvr.Create(PORT);
sockSrvr.Listen();
CSocket sockRecv;
sockSrvr.Accept(sockRecv);
CFile myFile;
myFile.Open("C://ANYFILE.EXE", CFile::modeRead | CFile::typeBinary);
int myFileLength = myFile.GetLength(); // Going to send the correct File Size
sockRecv.Send(&myFileLength, 4); // 4 bytes long
byte* data = new byte[myFileLength];
myFile.Read(data, myFileLength);
sockRecv.Send(data, myFileLength); //Send the whole thing now
myFile.Close();
delete data;
sockRecv.Close();
}
客户端:
void GetFile()
{
#define PORT 34000
AfxSocketInit(NULL);
CSocket sockClient;
sockClient.Create();
sockClient.Connect("127.0.0.1", PORT);
int dataLength;
sockClient.Receive(&dataLength, 4);
byte* data = new byte[dataLength];
sockClient.Receive(data, dataLength);
CFile destFile("C://temp//ANYFILE.EXE",
CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
destFile.Write(data, dataLength); // Write it
destFile.Close();
delete data;
sockClient.Close();
}
这上面是使用了MFC地CSocket类,我想使用Socket API来写一个.
原理:发送端:获取某个文件,将这文件分解,用循环将这文件发送出去;接受端:循环接受那文件,将其组合,存入某个文件里.
服务端:
void SendFile()
{
#define PORT 34000 /// 端口
AfxSocketInit(NULL);
CSocket sockSrvr;
sockSrvr.Create(PORT);
sockSrvr.Listen();
CSocket sockRecv;
sockSrvr.Accept(sockRecv);
CFile myFile;
myFile.Open("C://ANYFILE.EXE", CFile::modeRead | CFile::typeBinary);
int myFileLength = myFile.GetLength(); // Going to send the correct File Size
sockRecv.Send(&myFileLength, 4); // 4 bytes long
byte* data = new byte[myFileLength];
myFile.Read(data, myFileLength);
sockRecv.Send(data, myFileLength); //Send the whole thing now
myFile.Close();
delete data;
sockRecv.Close();
}
客户端:
void GetFile()
{
#define PORT 34000
AfxSocketInit(NULL);
CSocket sockClient;
sockClient.Create();
sockClient.Connect("127.0.0.1", PORT);
int dataLength;
sockClient.Receive(&dataLength, 4);
byte* data = new byte[dataLength];
sockClient.Receive(data, dataLength);
CFile destFile("C://temp//ANYFILE.EXE",
CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
destFile.Write(data, dataLength); // Write it
destFile.Close();
delete data;
sockClient.Close();
}
这上面是使用了MFC地CSocket类,我想使用Socket API来写一个.
相关文章推荐
- socket网络间通信传送任意大小和格式的文件
- c#的第一个程序,读出D:/下面的所有文件
- 3种下载文件程序的思考,为何使用NIO进行异步网络通讯
- C#传送文件接收端程序
- 基于TCP网络通信的自动升级程序源码分析-客户端接收文件
- C# 检查网络是否连通 判断远程文件是否存在 C#获取程序路径的方法中需要注意的地方
- DOS下串口通信程序来传送文件的源代码 分类: DOS 2013-07-22 16:30 465人阅读 评论(0) 收藏
- DOS下串口通信程序来传送文件的源代码
- NDK 下第一个boost asio 网络程序测试成功
- symbian 程序的cpp文件的第一个函数老显示没有定义 undefined identifier 'CZFirstTest_Login_Form'
- 第一个windows网络程序
- apache的httpd.conf文件配置和第一个php程序
- 第一个java网络程序,有客户端和服务器端,用socket实现
- 前不久,微软发布了其网络硬盘的正式版本:Windows Live Skydrive。它面向公共用户免费提供5G空间,最大单个文件支持到50M,还提供个人文件夹。微软的东西,当然会让人更放心一些,而且最重要的是它提供永久存储服务,还提供超链接!
- Socket通信程序——传送文件
- 基于TCP网络通信的自动升级程序源码分析-客户端接收文件
- 练习 7-6 编写一个程序,比较两个文件并打印它们第一个不相同的行
- (1)第一个程序(使用配置文件和注解)
- WinSock实现多线程网络文件传输程序(二)(MFC+WinSock 附源代码)
- JWebFileTrans- 一款可以从网络上下载文件的小程序(一)