怎么说也得让我知道怎么死的吧!!

borninchina 2007-05-09 10:00:06
下面是我今天写的用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;
}
//---------------------------------------------------------------------------

...全文
189 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
constantine 2007-05-10
  • 打赏
  • 举报
回复
好好看你的代码逻辑吧,别自己写的代码自己都不清除。盒子上我记得有例子,参考一下吧。
做网络传送前最好把协议定义一下。然后理清除第一步做什么,第二步又做什么...不然就祈祷上帝吧,如果有上帝的话
borninchina 2007-05-10
  • 打赏
  • 举报
回复
可惜!我的电脑用不起来,它需要能支持64位的CPU
yctin 2007-05-09
  • 打赏
  • 举报
回复
关闭防火墙...

如果不是防火墙问题

介绍你用 Microsoft Network Monitor 3
可以监视网络封包,对网络编程很有帮助的 ^_^
最重要是完全免费...
http://www.microsoft.com/downloads/details.aspx?familyid=AA8BE06D-4A6A-4B69-B861-2043B665CB53&displaylang=en
Waiting4you 2007-05-09
  • 打赏
  • 举报
回复
会不会是防火墙的原因?

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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