奇怪的错误??高手请进
下面是我编的一个用NMUDP实现文本传输的程序,在执行可执行文件时老是跳出这样的错误:先是一个错误窗口:List index out of bounds(0) ,然后又一个错误窗口提示can’t open file D:\Program Files\Borland\CBuilder6\deploy.txt 但实际上这个文件是肯定存在的!!!!
各组件属性如下:
组件名 属性名 新内容
Label1 Caption IP地址
Edit1
Label2 Caption 端口
Edit2 Text 3000
Label3 Caption 选定的文件
Edit3
Label4 Caption 接收文件预览
Button1 Caption 打开文件
Button2 Caption 发送
Memo1
StatusBar1
OpenDialog1
NMUDP1
在程序中我设定NMUDP1的LocalPort为2033,RemotePort就是为Edit2的内容,默认为3000
对于出现的错误我十分不解,BCB中用到端口是不是有什么需要设置的呢??还是我的程序中有错误???又或者是我设的端口号不对????
源代码如下:
//---------------------------------------------------------------------------
#ifndef textH
#define textH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <Dialogs.hpp>
#include <NMUDP.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TEdit *Edit1;
TEdit *Edit2;
TEdit *Edit3;
TNMUDP *NMUDP1;
TOpenDialog *OpenDialog1;
TButton *Button1;
TButton *Button2;
TStatusBar *StatusBar1;
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
TMemo *Memo1;
TLabel *Label4;
void __fastcall Button2Click(TObject *Sender);
void __fastcall NMUDP1DataReceived(TComponent *Sender,
int NumberBytes, AnsiString FromIP, int Port);
void __fastcall NMUDP1DataSend(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "text.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(Edit3->Text=="")
{
ShowMessage("请选择你要发送的文件");
return;
}
NMUDP1->RemoteHost=Edit1->Text;
NMUDP1->RemotePort=StrToInt(Edit2->Text);
NMUDP1->ReportLevel=Nmudp::Status_Basic;
NMUDP1->LocalPort=2033;
const int ThreshHold=2048;
TFileStream *Strm=new TFileStream(Edit3->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;
Memo1->Lines->Add("");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMUDP1DataReceived(TComponent *Sender,
int NumberBytes, AnsiString FromIP, int Port)
{
char *TmpBuffer=new char[NumberBytes+1];
int i;
NMUDP1->ReadBuffer(TmpBuffer,NumberBytes,i);
TmpBuffer[NumberBytes]='\0';
Memo1->Text=Memo1->Text+TmpBuffer;
delete[] TmpBuffer;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMUDP1DataSend(TObject *Sender)
{
StatusBar1->Panels->Items[0]->Text="文件传送完毕";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(OpenDialog1->Execute())
Edit3->Text=OpenDialog1->FileName;
}
//---------------------------------------------------------------------------