繁体   English   中英

如何使Indy服务器和客户端在C ++中的单独线程上侦听数据而不是在Delphi中

[英]How to make indy server and client listen for data on separate thread in c++ not Delphi

我正在使用C ++ Builder XE8,而且我只是一个初学者。 我对Delphi一无所知。 为了侦听服务器和客户端上的传入数据,我从Flie->New->Other->C++ builder Files插入了Thread Object (在线程视频演示中看到),并将其命名为TSocketThreard。

在客户端,我将客户端套接字分配给TIdIOHandlerSocket var(fsock),并使用以下代码连续侦听即将到来的数据。 len是int,s是String var,lbox是TListBox。

void __fastcall TSocketThread::Execute()
{
    FreeOnTerminate = True;
    while(1){
        if (Form1->fsock != NULL && Form1->fsock->Connected()) {
            if(Form1->fsock->CheckForDataOnSource(10))
                Form1->len = Form1->fsock->ReadLongInt();
                Form1->s = Form1->fsock->ReadString(Form1->len);
                Form1->lbox->Items->Add(Form1->s);
        }

    }
}

我试图从客户端的onConnect事件和按钮(也用于将客户端连接到服务器)启动它。 但是两次都冻结了。

在服务器端,我尝试以相同的方式执行OnExecute事件。 我的意思是,首先将套接字分配给var,将上面的代码放在线程的(我像以前插入的那样)execute方法上,然后从onExecute事件启动线程的start()方法。对于服务器,我想使其工作在异步模式下(至少处理数百个客户端)。

我可以感觉到我做错了方法,但是在c ++中很难找到一个很好的例子。 请有人告诉我正确的方法。

Indy服务器已经是多线程的,您无需创建自己的读取线程。 TIdTCPServer::OnExecute事件在线程中运行,并且每个客户端都有自己的线程。 只需使用事件提供的套接字,不要使用您自己的变量。

void __fastcall TMyServerForm::IdTCPServer1Execute(TIdContext *AContext)
{
    // use AContext->Connection->IOHandler as needed...
}

在客户端,您的读取线程正在执行非线程安全的操作,这可能导致UI冻结以及其他问题。 尝试类似这样的方法:

class TSocketThread : public TThread
{
private:
    TIdIOHandler *fIOHandler;
    String fString;
    void __fastcall UpdateListBox();
protected:
    virtual void __fastcall Execute();
public
    __fastcall TSocketThread(TIdIOHandler *AIOHandler);
};

__fastcall TSocketThread::TSocketThread(TIdIOHandler *AIOHandler)
    : TThread(false), fIOHandler(AIOHandler)
{
}

void __fastcall TSocketThread::Execute()
{
    while (!Terminated)
    {
        int len = fIOHandler->ReadLongInt();
        fString = fIOHandler->ReadString(len);
        Synchronize(&UpdateListBox);
    }
}

void __fastcall TSocketThread::UpdateListBox()
{
    Form1->lbox->Items->Add(fString);
}

TSocketThread *Thread = NULL;

void __fastcall TMyClientForm::IdTCPClient1Connected(TObject *Sender)
{
    Thread = new TSocketThread(IdTCPClient1->IOHandler);
}

void __fastcall TMyClientForm::IdTCPClient1Disconnected(TObject *Sender)
{
    if (!Thread) return;
    Thread->Terminate();
    if (!IsCurrentThread(Thread))
    {
        Thread->WaitFor();
        delete Thread;
        Thread = NULL;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM