关于ServerSocket多线程的问题
class SrvThread : public Thread
{
private:
TClientSocket* ClientSocket;
protected:
void __fastcall Execute();
public:
__fastcall SrvThread(TServerClientWinSocket*);
};
每当有一个客户连接时,在OnGetThread事件中开辟一线程:
void __fastcall TForm::ServerSocket1GetThread(TObject *Sender,
TServerClientWinSocket *ClientSocket,
TServerClientThread *&SocketThread)
{
SocketThread=new SrvThread(ClientSocket);
}
我想问的是:在OnGetThread事件中开辟线程的时候 把TServerClientThread 的指针传给了线程类,线程类在构造函数SrvThread中怎么才能把 TServerClientThread 赋值给 TClientSocket; 直接复制编译不过去,还想请教 serversocket多线程的注意事项。
[解决办法]
貌似楼上把事情给搞反了。
TServerSocket的OnGetThread处理应该创建一个新的线程。然后把ClientSocket 传给 SocketThread 不是把TServerClientThread 赋值给 TClientSocket
而且赋给TServerClientThread *&SocketThread的线程类指针的 应该是从TServerClientThread 继承下来的类的指针。不应该直接从TThread派生。
如此:
.h
#include <Classes.hpp>
#include <ScktComp.hpp>
class SrvThread : public TServerClientThread
{
protected:
void __fastcall ClientExecute(void); //重载TServerClientThread的ClientExecute 而不是TThread的Execute
public:
__fastcall SrvThread(bool CreateSuspended,TServerClientWinSocket* ASocket);
};
.cpp
__fastcall SrvThread::SrvThread(bool CreateSuspended,TServerClientWinSocket* ASocket)
: TServerClientThread(CreateSuspended,ASocket)
{
}
void __fastcall SrvThread::ClientExecute()
{
//处理代码
}