C++Builder-使用TIdTCPServer以编程方式创建TCP服务器连接
C++ Builder - Creating a TCP Server Connection programmatically with TIdTCPServer
我需要用TIdTCPServer
组件实现TCP Server
连接。我已经用GUI
(拖放)和它的工作方式做过了。但我需要拆分表单和TCP
实现。到目前为止,我看到的示例代码总是使用TIdTCPServer
作为TForm
类的成员。(拖放的结果)。
如何调用我从TForm实现的TCPConnection
类
TCPConnection CConnection = new TCPConnection(Owner, this);
以下是我尝试创建TCP Server
连接的方法。
TCPConnection::TCPConnection(TComponent* Owner, TForm4* TSuperForm){
IdTCPServer1 = new TIdTCPServer(Owner);
IdTCPServer1->Bindings->Clear();
//IdTCPServer1->Bindings->Add()->SetBinding("10.10.2.103", 774);
IdTCPServer1->OnConnect = (TIdServerThreadEvent)(&OnConnect);
IdTCPServer1->OnExecute = (TIdServerThreadEvent)&OnExecute;
IdTCPServer1->OnDisconnect = (TIdServerThreadEvent)&OnConnect;
IdTCPServer1->OnException = (TIdServerThreadExceptionEvent)&OnException;
IdTCPServer1->DefaultPort = 774;
IdTCPServer1->Bindings->Add();
IdTCPServer1->Bindings->Items[0]->IP="10.10.2.103";
IdTCPServer1->Bindings->Items[0]->Port=774;
IdTCPServer1->ListenQueue = 15;
IdTCPServer1->MaxConnections = 15;
IdTCPServer1->TerminateWaitTime = 5000;
IdTCPServer1->Active = true;
this->TSuperForm = TSuperForm;
}
到目前为止,代码是有效的。但当我试图到达上下文时,连接丢失,throws
是一个异常
void TCPConnection::OnConnect(TIdContext *AContext){
String IP = AContext->Binding()->PeerIP;
}
void TCPConnection::OnException(TIdContext *AContext, Exception *AException)
{
ShowMessage("Error:" + AException->ToString());
}
Error
说TIdTaskThreadWork
(我会编辑错误,可能是错误的)如果我不尝试访问AContext,连接将保持不变。
可能是线程、锁定列表。。。
有什么建议吗?
那些函数类型转换看起来很臭。您确定已将函数定义为__fastcall
吗,因为根本不需要函数类型转换。
如果定义正确的话,这应该就是你所需要的。
IdTCPServer1->OnConnect = &OnConnect;
// ... etc...
这就是我所做的:
TIdTCPServer *TCPServer = new TIdTCPServer( this );
TCPServer->Active = false;
TCPServer->OnExecute = MyExecute;
TCPServer->DefaultPort = XXX;
TCPServer->Active = true;
然后我的MyExecute调用定义如下:
void __fastcall MyExecute( TIdContext* AContext );
其他回调也以相同的方式处理,不要忘记__fastcall,它应该可以工作。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 如何在没有侦听器的情况下创建 TCP 连接?
- 如何使用已经存在的 tcp::socket 创建 tcp::iostream?
- boost::asio::async_connect 不仅创建一个 TCP 连接,还创建两个
- 正在创建多个TCP套接字连接
- C++Builder-使用TIdTCPServer以编程方式创建TCP服务器连接
- 如何在不使用QTcpSockets的情况下创建插槽并将其连接到TCP recv()
- 如何在c++ /QT中使用TCP服务器套接字创建Http服务器
- 创建tcp::acceptor时出现段错误
- 在c++中使用Node.js创建TCP连接
- 编辑TCP客户端/服务器创建反向波兰符号计算器服务器
- 如何确保TCP套接字在创建时连接
- 需要一个散列函数来创建ipv6 16字节地址和TCP 2字节端口号的32位值
- 创建一个控制台应用程序,打开TCP/IP连接并用C++、C#或Java打印输入的字符串
- TCP - 如何关闭在线程 -C++ 中创建的客户端