我对SOCKET了解得不多,总认为SOCKET只能用来传送字符串,他能不能传送流文件呢?例如我想将A.doc文件传送给另一方该怎么样做呢?请给出原代码,谢谢
解决方案 »
- CTreeCtrl控件的事件处理函数如何响应?
- 我下面的利用关键事件做的线程同步(通讯)有个“死结”问题:有的时候SetEvent未执行,但是主线程竟然pass,难道ResetEvent 有失败的情况吗?求帮
- waveInOpen函数在EVC中出错。。。。
- mplayer能实现这个 功能吗
- 为什么在向导中没办法通过对话框建CDialogBar类,如果自己从CDialogBar继承做个类,如何能跟对话框绑定,使得话框上的组件可以DDX
- MSDN的启动文件col不能打开
- 请问,在ListCtrl控件中,如何查找指定COLUMN中的内容呢?
- 组合框InsertString()时老出错
- 高分征求用udp实现类似oicq聊天源码(最好是用winsock api的).
- 请问如何在程序中搜集有关系统中的软硬件信息?(CPU,Memory...)
- debug和release区别
- 如何处理这样的快捷方式?
把文件分成一个个小包(缓冲区)发过去就可以了.下面给出服务器端代码: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.doc", 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.doc",
CFile::modeCreate | CFile::modeWrite | CFile::typeBinary); destFile.Write(data, dataLength); // Write it destFile.Close(); delete data;
sockClient.Close();
}