使用CSockets进行文件传送
来源:互联网 发布:网络热议话题 编辑:程序博客网 时间:2024/05/16 14:30
是一对实现在两台计算机间传送文件的函数,我没有看到过使用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();}
- 使用CSockets进行文件传送
- 使用CSockets进行文件传送
- VC 使用CSockets进行小文件传送
- mini2440_x35 使用minicom进行连接,传送文件
- 使用Ip/tcp协议进行文件传送
- CSockets使用步骤
- Java TCP使用Socket进行网络文件传送(5)
- mini2440_x35 ubuntu下使用dnw进行传送文件
- 使用Socket传送文件!
- 使用type传送文件(ALLyeSNO)
- 使用zssh远程传送文件
- android 与struts 使用HttpsUrlConnection进行数据(文件/字符)传送 (客户端例)
- VB中使用WinSock控件传送文件
- .Net中使用FTP传送文件
- SecureCRT 使用Zmodem从ubuntu传送文件
- 使用ckermit通过console传送文件
- 超级终端使用Xmodem协议传送文件
- vb.net使用socket传送文件
- DWZ(JUI) 教程 dwz.frag.xml 相关问题
- Linux 内核参数 和 Oracle相关参数调整
- hdu 3460 字典树
- HTTP Digest Authentication在实际应用中的问题
- 阿里巴巴公司DBA笔试题 .
- 使用CSockets进行文件传送
- Trees and Other Hierarchies in MySQL
- String、StringBuffer与StringBuilder之间区别
- Myeclipse 8.5 配置SVN
- [C++]字节对齐与结构体大小
- UNIX 高手的 20 个习惯 .
- 如何从Internet上有效而稳定地下载文件
- HttpClient使用Post和Get提交参数
- roosephu 考题之二: 进制法贪心