各位大大好 小弟正在學用Thread寫聊天室 會出現"List index out of bounds(-1) "的錯誤訊息
有大大知道這是什麼問題阿...........感謝 以下是我的Srever端部分程式:(綠色部分是Thread程式)
############################(Unit1)##################################
//--------------------------------------------------------------------
#include
#include "jpeg.hpp"
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include "Unit3.h"
//--------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TThread *Char1;
TThread *Char2;
AnsiString CharNo,Username;
AnsiString User1[8]={""};
AnsiString User2[8]={""};
int i=0,x=0,y=0;
//--------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Char1=new Char1Thread(true);
Char2=new Char2Thread(true);
}
//--------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
TStrings* RecMsg;
AnsiString test,id,pw,login;
RecMsg = new TStringList();
RecMsg->Text = Socket->ReceiveText();
id = RecMsg->Values["ID"];
pw = RecMsg->Values["PW"];
login = RecMsg->Values["TYPE"];
delete RecMsg; if(login==0)
{
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->Parameters->CreateParameter("checkID",ftString,pdInput,30,id);
ADOQuery1->Parameters->CreateParameter("checkPassword",ftString,pdInput,30,pw);
ADOQuery1->SQL->Add("Select * From 資料表1 Where ID=:checkID and Password=:checkPassword");
ADOQuery1->Open(); if(ADOQuery1->RecordCount==1)
{
test="登入成功";
Label1->Caption=test;
ServerSocket1->Socket->Connections[i-1]->SendText(test);
}
else
{
test="登入失敗";
Label1->Caption=test;
ServerSocket1->Socket->Connections[i-1]->SendText(test);
}
}
else if(login == 1)
{
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->Parameters->CreateParameter("checkID",ftString,pdInput,30,id);
ADOQuery1->Parameters->CreateParameter("checkPassword",ftString,pdInput,30,pw);
ADOQuery1->SQL->Add("Select * From 資料表1 Where ID=:checkID");
ADOQuery1->Open();
if(ADOQuery1->RecordCount==1)
{
test="此帳號有人使用";
Label1->Caption=test;
ServerSocket1->Socket->Connections[i-1]->SendText(test);
}
else
{
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->Parameters->CreateParameter("checkID",ftString,pdInput,30,id);
ADOQuery1->Parameters->CreateParameter("checkPassword",ftString,pdInput,30,pw);
ADOQuery1->SQL->Add("Insert Into 資料表1 Values(:checkID,:checkPassword)");
ADOQuery1->ExecSQL();
test="此帳號註冊成功";
Label1->Caption=test;
ServerSocket1->Socket->Connections[i-1]->SendText(test);
}
}
else
{
test="無登入和註冊(遊客)";
Label1->Caption=test;
ServerSocket1->Socket->Connections[i-1]->SendText(test);
}
}
//--------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ServerSocket1->Active=true;
ServerSocket2->Active=true;
ServerSocket3->Active=true;
ServerSocket4->Active=true;
ServerSocket5->Active=true;
Char1->Resume();
Char2->Resume();
Button1->Enabled=false;
Button2->Enabled=true;
}
//--------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ServerSocket1->Active=false;
ServerSocket2->Active=false;
ServerSocket3->Active=false;
ServerSocket4->Active=false;
ServerSocket5->Active=false;
Char1->Suspend();
Char2->Suspend();
Button1->Enabled=true;
Button2->Enabled=false;
}
//--------------------------------------------------------------------
void __fastcall TForm1::ServerSocket2ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
AnsiString mag,port1;
TStrings* RecMsg;
RecMsg = new TStringList();
RecMsg->Text = Socket->ReceiveText();
Username=RecMsg->Values["Name"];
CharNo=RecMsg->Values["CharNo"];
delete RecMsg;
TMemoryStream *msFile=new TMemoryStream;
MYPACK mp;
int j,k;
if(CharNo==1)
{
port1=8003;
mag="Port1=" port1 "\r\n";
Socket->SendText(mag);
msFile->LoadFromFile("Char1.jpg");
for(k=0;k<8;k )
{
if(User1[k]=="")
{
User1[k]=Username;
break;
}
}
}
else
{
port1=8004;
mag="Port1=" port1 "\r\n";
Socket->SendText(mag);
msFile->LoadFromFile("Char2.jpg");
for(k=0;k<8;k )
{
if(User2[k]=="")
{
User2[k]=Username;
break;
}
}
}
int block=(msFile->Size%MAX_DATASIZE==0)?
msFile->Size/MAX_DATASIZE: msFile->Size/MAX_DATASIZE 1;
for(j=0; jSize;
mp.dwThisDataSize=((j 1)*MAX_DATASIZE>msFile->Size)?
msFile->Size-j*MAX_DATASIZE: MAX_DATASIZE;
msFile->Position=j*MAX_DATASIZE;
msFile->Read(mp.pbThisData, mp.dwThisDataSize);
ServerSocket3->Socket->Connections[i-1]->SendBuf(&mp, sizeof(MYPACK));
Sleep(10);
}
delete msFile;
}
//--------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
i=i 1;
}
//--------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
i=i-1;
}
//--------------------------------------------------------------------
void __fastcall TForm1::ServerSocket4ClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
x=x 1;
}
//--------------------------------------------------------------------
void __fastcall TForm1::ServerSocket4ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
x=x-1;
}
//--------------------------------------------------------------------
void __fastcall TForm1::ServerSocket5ClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
y=y 1;
}
//--------------------------------------------------------------------
void __fastcall TForm1::ServerSocket5ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
y=y-1;
}
//--------------------------------------------------------------------
#####################(Unit2)Thread#####################
//--------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit2.h"
#include "Unit1.h"
#pragma package(smart_init)
//--------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall Char1Thread::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//--------------------------------------------------------------------
__fastcall Char1Thread::Char1Thread(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//--------------------------------------------------------------------
void Char1Thread::SetName()
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = "Char1";
info.dwThreadID = -1;
info.dwFlags = 0; __try
{
RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(DWORD),(DWORD*)&info );
}
__except (EXCEPTION_CONTINUE_EXECUTION)
{
}
}
//--------------------------------------------------------------------
void __fastcall Char1Thread::Execute()
{
SetName();
//---- Place thread code here ----
AnsiString Username,Message,Nouser,msg;
TStrings* RecMsg;
RecMsg = new TStringList();
RecMsg->Text = Form1->ServerSocket4->Socket->ReceiveText();
Username=RecMsg->Values["User"];
Message=RecMsg->Values["Message"];
delete RecMsg;
int k,j;
for(k=0;k<8;k )
{
if(Form1->User1[k]==Username)
{
Nouser=IntToStr(k);
break;
}
}
msg="NoUser=" Nouser "\r\nMessage=" Message "\r\n";
for(j=0;j<Form1->x;j )
{
Form1->ServerSocket4->Socket->Connections[j]->SendText(msg);
}
}