Chinaunix首页 | 论坛 | 博客
  • 博客访问: 367665
  • 博文数量: 715
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:46
文章分类

全部博文(715)

文章存档

2011年(1)

2008年(714)

我的朋友

分类:

2008-10-13 16:40:20

使用CSockets进行文件传送
作者:Vicken Simonian 翻译:DY



这是一对实现在两台计算机间传送文件的函数,我没有看到过使用CSocket进行文件传送的代码,希望此代码对你有用.代码中包含两个函数,第一个用于服务器端,第二个用于客户端.
需要说明的是本文提供的方法并不适用于大型文件的传送.

下面给出服务器端代码:
void SendFile()
{
 #define PORT 34000 /// Select any free port you wish

 AfxSocketInit(NULL);
 CSocket sockSrvr; 
 sockSrvr.Create(PORT); // Creates our server socket
 sockSrvr.Listen(); // Start listening for the client at PORT
 CSocket sockRecv;
 sockSrvr.Accept(sockRecv); // Use another CSocket to accept the connection


 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 /// Select any free port you wish

 AfxSocketInit(NULL);
 CSocket sockClient;
 sockClient.Create();

 // "127.0.0.1" is the IP to your server, same port
 sockClient.Connect("127.0.0.1", PORT); 

 int dataLength;
 sockClient.Receive(&dataLength, 4); //Now we get the File Size first
		
 byte* data = new byte[dataLength];
 sockClient.Receive(data, dataLength); //Get the whole thing

 CFile destFile("C:\\temp\\ANYFILE.EXE", 
  CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);

 destFile.Write(data, dataLength); // Write it

 destFile.Close();

 delete data;
 sockClient.Close();
}
最好确认服务器端函数在客户端函数之前运行,本文的代码可以方便地添加到工程中,解决服务器/客户模型中的文件传送问题.
感谢大家!
--------------------next---------------------

不适合exe,不适合bmp等格式,原理应该是chuiyun所说的
( pluto026 发表于 2008-7-2 11:01:00)
 
你的代码我下载了 编译通过 
但是不会使用  
我在一太电脑上执行2个程序
先SERVER 后 Client 
但是报错  为什么 ( thinkz 发表于 2005-12-2 11:35:00)
 
基本理论:收发一次数据包不能大于1024字节,可以收发大数据包不会发生错误是因为系统自已帮你分开数个包了。 ( chuiyun 发表于 2004-12-22 14:40:00)
 
好象不能传exe文件,和大文件。那为朋友给个更好的代码? ( scm 发表于 2004-12-12 15:57:00)
 
其实根本不能实现,你传一个exe文件,看收到的能不能运行???不行吧,主要是里面的'\0'字符的问题没有解决好。 ( luolovegui 发表于 2004-10-26 15:54:00)
 
源程序可改为:
// sockRecv.Send(data, myFileLength); //Send the whole thing now
for(int i=0;i<=myFileLength;i+=1024)
sockRecv.Send(data+i,1024); //Send the whole thing now
if(myFileLength%1024!=0)
sockRecv.Send(data+myFileLength-myFileLength%1024,myFileLength%1024);



// sockClient.Receive(data, dataLength); //Get the whole thing
for(int i=0;i<=dataLength;i+=1024)
sockClient.Receive(data+i,1024); //Send the whole thing now
if(dataLength%1024!=0)
sockClient.Receive(data+dataLength-dataLength%1024,dataLength%1024);

即可。 ( sinall 发表于 2004-4-25 16:18:00)
 
h
( h 发表于 2002-9-20 9:53:00)
 
编译的时候错误:fatal error C1083: Cannot open precompiled header file: 'Debug/asdfsd.pch': No such file or directory
Error executing cl.exe.
为什么?
( new_coming 发表于 2002-9-12 15:05:00)
 
有这么麻烦吗?
我曾经直接用WINSOCK API还有ReadFile、WriteFile传送过100多M的文件
也用命名管道和ReadFile、WriteFile传送过同样大的文件
都很稳定 ( snowbird 发表于 2002-7-9 9:16:00)
 
为了安全起见最好 自己定义一个类 处理Socket通信 因为文件传输一般是时间较长数据较多 最好传输一次以后服务器端等待客户端一个响应信息或者等待发送缓冲区有足够的空间以后再发送 不然会产生错误 CSocket不是很好用 我已经作了一个十分好用的SOCKET编程框架 是基于包标志的自动调用机制 我用它实现了12个客户端与一个服务器之间的同时通信 基本没有问题 我现在正用它实现远程计算机管理  我希望通过不断的测试改进 将它完善 现在基本上只要写功能相关的函数 网络通信的问题 由我的框架解决了 哈哈 N天辛苦 终于换来了高效率 ( iamwuge 发表于 2002-6-5 17:52:00)
 
.......................................................

--------------------next---------------------

阅读(150) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~