怎么说也得让我知道怎么死的吧!!
下面是我今天写的用TIDUDPServer实现文件传输的代码
我的本意是:用户输入主机IP,点击连接按钮连接主机同时向主机发送消息"1",服务器端接受到消息后将指定文件发送给用户,用户将所接受到的文件显示在RichEdit中.
但是我进行测试时却是两边都没反应好象什么都没发生一样??不知什么原因,哪位高手看看下面的程序哪儿有错,怎么说也得让我知道怎么死的吧!!(我估计是用户发送消息时有问题但不知怎么改)
服务器端:
#include <vcl.h>
#pragma hdrstop
#include "vod1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(OpenDialog1->Execute())
{
Edit1->Text=OpenDialog1->FileName;
StatusBar1->SimpleText="上载"+Edit1->Text+"成功!";
}
else
Edit1->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(!IdUDPServer1->Active)
{
IdUDPServer1->Active=true;
StatusBar1->SimpleText="监听...";
Button2->Caption="终止";
}
else
{
IdUDPServer1->Active=false;
StatusBar1->SimpleText="停止服务!";
Button2->Caption="监听";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdUDPServer1UDPRead(TObject *Sender,
TStream *AData, TIdSocketHandle *ABinding)
{
if(StrToInt(IdUDPServer1->ReceiveString(IdTimeoutDefault)))
{
StatusBar1->SimpleText="收到"+ABinding->PeerIP+"的消息";
TFileStream *Strm= new TFileStream(Edit1->Text,fmOpenRead);
char buff[8192];
int buffsize=Strm->Size;
int buffers=buffsize/8192;
for(int i=0;i<=buffers;i++)
{
if(buffsize>8192)
{
Strm->Read(buff,8192);
IdUDPServer1->SendBuffer(ABinding->PeerIP,ABinding->PeerPort,buff,8192);
}
else
{
Strm->Read(buff,buffsize);
IdUDPServer1->SendBuffer("10.10.224.77",4003,buff,buffsize);
}
}
delete[] buff;
delete Strm;
ShowMessage("发送完毕!!");
}
}
//------------------------------------------------------------------------
用户端:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "vod2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(!IdUDPServer1->Active)
{
IdUDPServer1->Active=true;
StatusBar1->SimpleText="连接"+Edit1->Text+"...";
Button1->Caption="断开";
IdUDPServer1->Send(Edit1->Text,4000,"1");
}
else
{
IdUDPServer1->Active=false;
StatusBar1->SimpleText="断开与"+Edit1->Text+"的连接";
Button1->Caption="连接";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdUDPServer1UDPRead(TObject *Sender,
TStream *AData, TIdSocketHandle *ABinding)
{
StatusBar1->SimpleText="连接"+ABinding->PeerIP+"成功!";
char buff[8192];
int buffsize=AData->Size;
int buffers=buffsize/8192;
for(int i=0;i<=buffers;i++)
{
if(buffsize>8192)
{
AData->ReadBuffer(buff,8192);
RichEdit1->Lines->Text=buff;
}
else
{
AData->ReadBuffer(buff,buffsize);
RichEdit1->Lines->Text=buff;
}
}
delete[] buff;
}
//---------------------------------------------------------------------------