UDP控件问题

MiT 2001-05-27 04:50:00
能不能写个利用UDP控件发送和接受的程序给我参考一下!谢谢了!
...全文
70 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
rh 2001-05-27
  • 打赏
  • 举报
回复
不要客气,拿分来:
//---------------------------------------------------------------------------
#include <vcl.h>
#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");
}
//---------------------------------------------------------------------------
Wingsun 2001-05-27
  • 打赏
  • 举报
回复
兄弟们,知道的快来啊!

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧