Y
ynhhr
Unregistered / Unconfirmed
GUEST, unregistred user!
主线程:
TGroupThread * ctThread ;
void __fastcall TfmServer::BBtnStartTalkClick(TObject *Sender)
{
ctThread = new TGroupThread(参数);
}
void __fastcall TfmServer::MMWaveIn1BufferReady()
{
ctThread->BufferLoad();
}
我创建的线程内函数如下:
void __fastcall TGroupThread::Execute()
{
Synchronize(UpdateMemoSetup);//显示启动信息
WSRecieve = new TWSocket(NULL);
WSRecieve->Addr = "0.0.0.0";
WSRecieve->Port = iRecvPort + 1;
WSRecieve->Proto = "udp";
WSRecieve->OnDataAvailable = OnDataAvailable;
WSRecieve->Listen();
FSendSocket = new TWSocket(NULL);
FSendSocket->Addr = "192.168.168.255";
FSendSocket->Port = iRecvPort;
FSendSocket->Proto = "udp";
FSendSocket->Connect();
//MMWaveOut1->Started();
WSRecieve->MessageLoop();
}
//---------------------------------------------------------------------------
void __fastcall TGroupThread::OnDataAvailable(TObject *Sender, WORD Error)
{
int Len;
char Baff[BUFF_SIZE];
TSockAddrIn Src;
int SrcLen;
TInAddr FServerAddr ;
String Str;
SrcLen = sizeof(Src);
Len = WSRecieve->ReceiveFrom(Buff, sizeof(Buff), Src, SrcLen);
TList *pList = ThLStud->LockList();
pList->Add(Buff);
//memcpy(Baff, (char *)pList->Items[pList->Count-1], BUFF_SIZE);
ThLStud->UnlockList();
//FSendSocket->Send(Baff,Len);
iLRecvBufCount++;
......
}
//---------------------------------------------------------------------------
void __fastcall TGroupThread::BufferLoad()
{
char Buff1[MAX_GROUP_STUDENT][BUFF_SIZE];
int i ;
long lBuff[BUFF_SIZE] ;
TList *pList = ThLStud->LockList();
if (pList->Count == 0)
{
ThLStud->UnlockList();
return;
}
memcpy(Buff1, (char *)pList->Items[0], BUFF_SIZE);
pList->Delete(0);
ThLStud->UnlockList();
FSendSocket->Send(Buff1[0],BUFF_SIZE);
}
/************************************************************************/
主线程只是调用TGroupThread的函数,来访问TGroupThread的私有变量ThLStud、FSendSocket、Buff等,这样共享数据有无问题?程序运行正常,只是客户端接收到的声音不太像,写到文件查看发现有数据丢失和重复现象。
TGroupThread * ctThread ;
void __fastcall TfmServer::BBtnStartTalkClick(TObject *Sender)
{
ctThread = new TGroupThread(参数);
}
void __fastcall TfmServer::MMWaveIn1BufferReady()
{
ctThread->BufferLoad();
}
我创建的线程内函数如下:
void __fastcall TGroupThread::Execute()
{
Synchronize(UpdateMemoSetup);//显示启动信息
WSRecieve = new TWSocket(NULL);
WSRecieve->Addr = "0.0.0.0";
WSRecieve->Port = iRecvPort + 1;
WSRecieve->Proto = "udp";
WSRecieve->OnDataAvailable = OnDataAvailable;
WSRecieve->Listen();
FSendSocket = new TWSocket(NULL);
FSendSocket->Addr = "192.168.168.255";
FSendSocket->Port = iRecvPort;
FSendSocket->Proto = "udp";
FSendSocket->Connect();
//MMWaveOut1->Started();
WSRecieve->MessageLoop();
}
//---------------------------------------------------------------------------
void __fastcall TGroupThread::OnDataAvailable(TObject *Sender, WORD Error)
{
int Len;
char Baff[BUFF_SIZE];
TSockAddrIn Src;
int SrcLen;
TInAddr FServerAddr ;
String Str;
SrcLen = sizeof(Src);
Len = WSRecieve->ReceiveFrom(Buff, sizeof(Buff), Src, SrcLen);
TList *pList = ThLStud->LockList();
pList->Add(Buff);
//memcpy(Baff, (char *)pList->Items[pList->Count-1], BUFF_SIZE);
ThLStud->UnlockList();
//FSendSocket->Send(Baff,Len);
iLRecvBufCount++;
......
}
//---------------------------------------------------------------------------
void __fastcall TGroupThread::BufferLoad()
{
char Buff1[MAX_GROUP_STUDENT][BUFF_SIZE];
int i ;
long lBuff[BUFF_SIZE] ;
TList *pList = ThLStud->LockList();
if (pList->Count == 0)
{
ThLStud->UnlockList();
return;
}
memcpy(Buff1, (char *)pList->Items[0], BUFF_SIZE);
pList->Delete(0);
ThLStud->UnlockList();
FSendSocket->Send(Buff1[0],BUFF_SIZE);
}
/************************************************************************/
主线程只是调用TGroupThread的函数,来访问TGroupThread的私有变量ThLStud、FSendSocket、Buff等,这样共享数据有无问题?程序运行正常,只是客户端接收到的声音不太像,写到文件查看发现有数据丢失和重复现象。