郁闷的ServerSocket错误????
以下是我写的一段代码,目的是在server端用SOCKET的SendText()发送一条消息,并在client端的memo中显示.郁闷的是运行后服务器端点击监听按钮后,跳出一个错误提示:"list index out of bouds(0)"这是什么意思??下标越界?????
//---------------------------------------------------------------------------
#ifndef sosH
#define sosH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ScktComp.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TStatusBar *StatusBar1;
TClientSocket *ClientSocket1;
TServerSocket *ServerSocket1;
TButton *Button2;
TButton *Button3;
TMemo *Memo1;
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall cc(TObject *Sender, TCustomWinSocket *Socket);
void __fastcall cr(TObject *Sender, TCustomWinSocket *Socket);
void __fastcall sc(TObject *Sender, TCustomWinSocket *Socket);
void __fastcall scc(TObject *Sender, TCustomWinSocket *Socket);
private: // User declarations
String Server;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "sos.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ServerSocket1->Active=true;//监听
StatusBar1->Panels->Items[0]->Text = "Listening...";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if (InputQuery("Computer to connect to", "Address Name:", Server))
{
if (Server.Length() > 0)
{
ClientSocket1->Host = Server;//指定地址
ClientSocket1->Active = true;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cc(TObject *Sender, TCustomWinSocket *Socket)
{
StatusBar1->Panels->Items[0]->Text = "Connect to: " + Socket->RemoteHost; //连接主机成功
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cr(TObject *Sender, TCustomWinSocket *Socket)
{
Memo1->Lines->Add(Socket->ReceiveText()); //显示主机发送的消息
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sc(TObject *Sender, TCustomWinSocket *Socket)
{
StatusBar1->Panels->Items[0]->Text = "Connect to: " + Socket->RemoteAddress; //连接客户端成功
}
//---------------------------------------------------------------------------
void __fastcall TForm1::scc(TObject *Sender, TCustomWinSocket *Socket)
{
Socket->SendText("Hello,this is hawk.Can you hear me?");//发送消息
}
//---------------------------------------------------------------------------