高手这段程序为何调不通
求助:怎样传输大文件(非文本问题)的问题,指定发送D盘下 “D:\DB1\db.jpg”文件110K左右至规定目录下
//服务器端
#include "stdafx.h"
#include "asdfsd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
#define PORT 34000 /// Select any free port you wish
void SendFile(LPCTSTR strFilename)
{
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;
if(!myFile.Open(strFilename, CFile::modeRead | CFile::typeBinary))
return;
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();
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
SendFile("D:\\DB1\\DB.JPG");
return nRetCode;
}
//客户端
#include "stdafx.h"
#include "asdfasdfas.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
#define PORT 34000
void GetFile(LPCTSTR strFilename)
{
AfxSocketInit(NULL);
CSocket sockClient;
sockClient.Create();
sockClient.Connect("127.0.0.1", PORT); // "127.0.0.1" is the IP to your server, same 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(strFilename, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
destFile.Write(data, dataLength); // Write it
destFile.Close();
delete data;
sockClient.Close();
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
GetFile("D\\:\\DB1\\tmp33\\db11.jpg");
return nRetCode;
}
问题点数:0、回复次数:3Top
1 楼oyljerry(【勇敢的心】→ ㊣提拉米苏√㊣)回复于 2005-04-04 20:36:53 得分 0
GetFile("D:\\DB1\\tmp33\\db11.jpg");Top
2 楼LLClown(诸葛明)回复于 2005-04-05 09:25:28 得分 0
俺最喜欢有代码的问题了,顺便赚点分
我已调过你的程序了;
接受程序GetFile(LPCTSTR strFilename)中更改:
int result = 0;
while(result < dataLength)
{
result += sockClient.Receive(data+result, dataLength-result); //Get the whole thing
}
还有你用的什么编译器啊,头文件都不对?
#include "stdafx.h"
#include <afxsock.h>
#include <iostream>Top
3 楼nuaawenlin(飘人)回复于 2005-04-05 09:45:24 得分 0
在初始化事例时
afxInitSock()Top




