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

求一个UDP通信的例子。

楼主jim2001999(乐天)2005-05-07 17:44:50 在 C++ Builder / 网络及通讯开发 提问

RT。用IDUDP和NMUDP的都行,最好能够多线程传输文件。  
  直接贴出来或者发到邮箱:jim2001999@163.com都行。先谢谢大虾了~  
  问题点数:50、回复次数:6Top

1 楼pdkcprogram(penter)回复于 2005-05-07 17:55:08 得分 5

你去www.vckbase.com看看吧,那里面有很多的exampleTop

2 楼h98458(零点起飞)回复于 2005-05-07 17:56:58 得分 45

不要客气,拿分来:  
  //---------------------------------------------------------------------------  
  #include    
  #pragma   hdrstop  
   
  #include   "Unit1.h"  
  //---------------------------------------------------------------------------  
  #pragma   package(smart_init)  
  #pragma   resource   "*.dfm"  
  TForm1   *Form1;  
  //---------------------------------------------------------------------------  
  __fastcall   TForm1::TForm1(TComponent*   Owner)  
  :   TForm(Owner)  
  {  
  ValidStream   =   false;  
  }  
  //---------------------------------------------------------------------------  
  void   __fastcall   TForm1::NMUDP1DataReceived(TComponent   *Sender,  
  int   NumberBytes,   AnsiString   FromIP)  
  {  
  char   *TmpBuffer   =   new   char[NumberBytes   +   1];  
   
  int   i;  
  NMUDP1->ReadBuffer(TmpBuffer,   NumberBytes,   i);  
  TmpBuffer[NumberBytes]   =   '\0';  
  Output->Text   =   Output->Text   +   TmpBuffer;  
   
  delete   []   TmpBuffer;  
  }  
  //---------------------------------------------------------------------------  
  void   __fastcall   TForm1::BtSendMsgClick(TObject   *Sender)  
  {  
  //   UDP   does   not   timeout   because   it   does   not   connect.  
  NMUDP1->RemoteHost   =   Host->Text;  
  NMUDP1->RemotePort   =   StrToInt(Port->Text);  
  NMUDP1->ReportLevel   =   Nmudp::Status_Basic;  
   
  NMUDP1->SendBuffer(UDPMsg->Text.c_str(),   strlen(UDPMsg->Text.c_str()),  
  strlen(UDPMsg->Text.c_str()));  
  Output->Lines->Add("");  
  }  
  //---------------------------------------------------------------------------  
  void   __fastcall   TForm1::BtLoadFileClick(TObject   *Sender)  
  {  
  if(OpenDialog1->Execute())  
  {  
  UDPStream->Text   =   OpenDialog1->FileName;  
  ValidStream   =   true;  
  }  
  else  
  {  
  UDPStream->Text   =   "";  
  ValidStream   =   false;  
  }  
  }  
  //---------------------------------------------------------------------------  
  void   __fastcall   TForm1::BtSendStreamClick(TObject   *Sender)  
  {  
  if(!ValidStream)  
  {  
  MessageBox(NULL,   "Need   a   file   to   send.",   "Invalid   input",   MB_ICONERROR);  
  return;  
  }  
   
  NMUDP1->RemoteHost   =   Host->Text;  
  NMUDP1->RemotePort   =   StrToInt(Port->Text);  
  NMUDP1->ReportLevel   =   Nmudp::Status_Basic;  
  const   int   ThreshHold   =   2048;  
   
  //   Load   the   stream.  
  TFileStream   *Strm   =   new   TFileStream(UDPStream->Text,   fmOpenRead);  
  char   *Buffer   =   new   char[ThreshHold];  
  int   BuffSize   =   Strm->Size;  
  int   Buffers   =   BuffSize   /   ThreshHold;  
   
  for(int   i=0;   i   <=   Buffers;   i++)  
  {  
  if(BuffSize   >   2048)  
  {  
  Strm->Read(Buffer,   ThreshHold);  
  NMUDP1->SendBuffer(Buffer,   ThreshHold,   ThreshHold);  
  }  
  else  
  {  
  Strm->Read(Buffer,   BuffSize);  
  NMUDP1->SendBuffer(Buffer,   BuffSize,   BuffSize);  
  }  
   
  BuffSize   -=   ThreshHold;  
  }  
   
  delete   Buffer;  
  delete   Strm;  
   
  Output->Lines->Add("");  
  }  
  void   __fastcall   TForm1::NMUDP1DataSend(TObject   *Sender)  
  {  
  StatusBar1->SimplePanel   =   TRUE;  
  StatusBar1->SimpleText   =   "Data   Sent";  
  }  
  //---------------------------------------------------------------------------  
  void   __fastcall   TForm1::NMUDP1InvalidHost(bool   &handled)  
  {  
  AnsiString   TmpStr;  
   
  if   (InputQuery("Invalid   Host!",   "Specify   a   new   host:",   TmpStr))  
  {  
  NMUDP1->RemoteHost   =   TmpStr;  
  handled   =   TRUE;  
  }  
  }  
  //---------------------------------------------------------------------------  
  void   __fastcall   TForm1::NMUDP1Status(TComponent   *Sender,   AnsiString   status)  
  {  
  if   (StatusBar1   !=   0)  
  StatusBar1->SimpleText   =   status;  
  }  
  //---------------------------------------------------------------------------  
  void   __fastcall   TForm1::NMUDP1StreamInvalid(bool   &handled,   TStream   *Stream)  
  {  
  ShowMessage("Invalid   Stream:   Stream   contains   no   data");  
  }  
  //---------------------------------------------------------------------------  
  Top

3 楼icwin(www.cat898.com.cn)回复于 2005-05-07 20:29:17 得分 0

upTop

4 楼coffee6340()回复于 2005-05-07 21:48:38 得分 0

我碰到一个很怪的问题呢。,我做了一个类A,里面封装了一个TNMUDP的对像和一个B函数,当产生这个类A的对象的时候,new   了一个TNMUDP   对象,将其ondatareceived事件的函数指定为B,当我产生很多A类的对象时,每个类A里面都有一个TNMUDP对象,这样做可以实现通信吗?,麻烦指点一下,会不会有问题?  
  我不是用的TNMUDP的控件,通信的时候发不出数据,怎么回事啊。  
  帮帮我,各位Top

5 楼jim2001999(乐天)回复于 2005-05-07 23:13:04 得分 0

to:   h98458(零点起飞)   仁兄,能不能把你的程序给我EMAIL一份?有些定义的类型没写出来。看起来好费力。谢谢先。Top

6 楼Yans(跟贴是一种友谊)回复于 2005-07-18 21:18:40 得分 0

学习Top

相关问题

  • 求利用UDP通信的windows应用程序例子,共同学习
  • 等不及了,百元求例子:tcp 和udp内网和外网的通信问题
  • 谁有udp的例子?
  • 100分求UDP例子
  • 求jsp与ejb通信的例子
  • TCP与UDP通信问题!
  • 谁能给我找个进程通信的例子?
  • 有那位老大编写过QOS通信的例子?
  • 寻找用pb做的串行通信的例子。急!多谢!
  • 求一个VC控制台下WINSOCK 通信例子

关键词

  • nmudp
  • tmpbuffer
  • threshhold
  • numberbytes
  • buffsize
  • fastcall tform
  • strm
  • statusbar
  • tcomponent
  • tmpstr

得分解答快速导航

  • 帖主:jim2001999
  • pdkcprogram
  • h98458

相关链接

  • CSDN Blog
  • 技术文档
  • 代码下载
  • 第二书店
  • 读书频道

广告也精彩

反馈

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