CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  网络编程

高手这段程序为何调不通

楼主alvinguo(长安客)2005-04-04 18:22:02 在 VC/MFC / 网络编程 提问

求助:怎样传输大文件(非文本问题)的问题,指定发送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

相关问题

  • 一个多线程的程序为何调不通?
  • 第一个servlet程序却一直调不通.HTTP Status 500
  • 下面程序为何编译不通过?请高手帮帮忙,多谢!
  • 这么简单的程序都调不通,请求高手帮助!
  • 郁闷:这个多线程下载程序怎么也调不通
  • 一个想不通的c程序?
  • 为何我的程序不能调试
  • delphi5.x ado/mts/com+第一个程序就调不通。谁帮我摆平它?繁体真烦。
  • vb查询注册表问题 我手里的程序见下但是调不通 请帮忙?
  • 300分的问提 我写一个程序老是调不通 -------- 谁能帮我 300 分相赠

关键词

  • socksrvr
  • myfile
  • port
  • include

得分解答快速导航

  • 帖主:alvinguo

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo