原版本升级,更稳定功能更强 感谢网友对Beta的意见反馈 一、 使用G-TcpClient模块 二、 可以设定间隔时间发起大规模长、短连接 三、 可以发起密集数据包,包括即时和定时发送 四、 数据发送工作由一个独立线程担当,连接发起工作可设置多个独立的线程担当。 五、 可以多种模式组合连接或发送数据。
2025-11-13 17:20:40 1.13MB IOCP TcpServer 完成端口 破连接限制
1
/******************************************************************************** * * * G-TcpClient:基于完成端口的Tcp客户端通讯模块(IOCP TcpClient) * * * * Copyright © 2009-2010 GuestCode 代码客(卢益贵) * * 版权所有 侵权必究 * * * * QQ:48092788 E-Mail:48092788@qq.com 源码博客:http://blog.csdn.net/guestcode * * * * GSN:34674B4D-1F63-11D3-B64C-11C04F79498E * * * ********************************************************************************/ #pragma once extern "C" { //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 类型定义 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #ifndef _GTYPE #define _GTYPE typedef unsigned char* PGBUF; typedef void(__stdcall *PGFN_ON_CONNECTED)(unsigned int unPerHandle, unsigned char* pBuf, unsigned int unLen); typedef void(__stdcall *PGFN_ON_RECEIVED)(unsigned int unPerHandle, unsigned char* pBuf, unsigned int unLen); typedef void(__stdcall *PGFN_ON_SENDED)(unsigned int unPerHandle, unsigned int unSendID, unsigned int unLen); typedef void(__stdcall *PGFN_ON_DISCONNECTED)(unsigned int unPerHandle, unsigned int unFlag); typedef void(__stdcall *PGFN_ON_THREAD)(unsigned int unThreadContext, unsigned int unThreadHandle, unsigned int unThreadID, BOOL bIsBegin, unsigned int unFlag); /* typedef struct _CONNECTION { unsigned int unPerHandle; }CONNECTION, *PCONNECTION; typedef void(__stdcall *PGFN_ON_CONNECTED)(unsigned int unPerHandle, unsigned char* pBuf, unsigned int unLen); typedef void(__stdcall *PGFN_ON_RECEIVED)(PCONNECTION pConnection, unsigned char* pBuf, unsigned int unLen); typedef void(__stdcall *PGFN_ON_SENDED)(PCONNECTION pConnection, unsigned int unSendID, unsigned int unLen); typedef void(__stdcall *PGFN_ON_DISCONNECTED)(PCONNECTION pConnection, unsigned int unFlag); void __stdcall GTcpClt_OnThread(unsigned int unThreadContext, unsigned int unThreadHandle, unsigned int unThreadID, BOOL bIsBegin, unsigned int unFlag) { } void __stdcall GTcpClt_OnConnected(unsigned int unPerHandle, void* _NULL, unsigned int unNULL) { } void __stdcall GTcpClt_OnReceived(PCONNECTION pConnection, unsigned char* pBuf, unsigned int unLen) { } void __stdcall GTcpClt_OnSended(PCONNECTION pConnection, unsigned int unSendID, unsigned int unLen) { } void __stdcall GTcpClt_OnDisconnected(PCONNECTION pConnection, unsigned int unFlag) { } */ #define _USE_UNICODE 1 #ifndef _DLL //#define _DLL #endif #ifdef _DLL #define DllExport _declspec(dllexport) #else #define DllExport #endif #define VER_FLAG_WIDE_CHAR 0x01 #define VER_FLAG_BETA 0x02 #define VER_FLAG_ZERO_READ 0x04 #define VER_FLAG_TRIAL 0x08 #define VER_FLAG_DEBUG 0x10 #define HNDS_CONNECT 1 #define HNDS_CONNECTED 2 #define HNDS_DISCONNECT 3 #endif //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 类型定义 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 版本信息 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #if(_USE_UNICODE) DllExport wchar_t* __stdcall GTcpClt_GetVersionName(void); #else DllExport char* __stdcall GTcpClt_GetVersionName(void); #endif DllExport float __stdcall GTcpClt_GetVersionNumber(void); DllExport unsigned int __stdcall GTcpClt_GetVersionFlag(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 版本信息 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 功能函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DllExport DWORDLONG __stdcall GTcpClt_GetPhyMemInfo(DWORDLONG* pdwTotal); #if(_USE_UNICODE) DllExport void __stdcall GTcpClt_WriteLog(wchar_t* pstrLog, unsigned int unCode = 0); DllExport void __stdcall GTcpClt_GetHostIP(wchar_t* pstrIP, unsigned int unLen, BOOL bIsInternetIP = FALSE); #else DllExport void __stdcall GTcpClt_WriteLog(char* pstrLog, unsigned int unCode = 0); DllExport void __stdcall GTcpClt_GetHostIP(char* pstrIP, unsigned int unLen, BOOL bIsInternetIP = FALSE); #endif //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 功能函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //>>>>>>>>>>>>>>>>>>>>>>>>>>>> PerIoData函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DllExport unsigned int __stdcall GTcpClt_GetGBufSize(void); DllExport unsigned int __stdcall GTcpClt_GetIoDataSize(void); DllExport unsigned int __stdcall GTcpClt_GetIoDataUse(void); DllExport unsigned int __stdcall GTcpClt_GetIoDataTotal(void); DllExport float __stdcall GTcpClt_GetIoDataUseRate(void); DllExport unsigned int __stdcall GTcpClt_GetIoDataUseMem(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<< PerIoData函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //>>>>>>>>>>>>>>>>>>>>>>>>>>>> PerHndData函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DllExport unsigned int __stdcall GTcpClt_GetHndDataUse(void); DllExport unsigned int __stdcall GTcpClt_GetHndDataTotal(void); DllExport unsigned int __stdcall GTcpClt_GetHndDataSize(void); DllExport float __stdcall GTcpClt_GetHndDataUseRate(void); DllExport unsigned int __stdcall GTcpClt_GetHndDataUseMem(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<< PerHndData函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 信息函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DllExport unsigned int __stdcall GTcpClt_GetThreadNumber(void); DllExport unsigned int __stdcall GTcpClt_GetPageSize(void); DllExport unsigned int __stdcall GTcpClt_GetBlockSize(void); DllExport unsigned int __stdcall GTcpClt_GetConnectCount(void); DllExport unsigned int __stdcall GTcpClt_GetThreadRunCount(unsigned int unThreadContext); DllExport unsigned int GTcpClt_GetState(unsigned int unPerHandle); #if(_USE_UNICODE) DllExport wchar_t* __stdcall GTcpClt_GetThreadName(unsigned int unThreadContext); DllExport BOOL __stdcall GTcpSock_GetPerHandleInfo(unsigned int unPerHandle, wchar_t* pstrIP, unsigned int unIPLen, wchar_t* pstrPort, unsigned int unPortLen); DllExport BOOL __stdcall GTcpSock_GetPerHandleName(unsigned int unPerHandle, wchar_t* pstrName, unsigned int unLen); #else DllExport char* __stdcall GTcpClt_GetThreadName(unsigned int unThreadContext); DllExport BOOL __stdcall GTcpSock_GetPerHandleInfo(unsigned int unPerHandle, char* pstrIP, unsigned int unIPLen, char* pstrPort, unsigned int unPortLen); DllExport BOOL __stdcall GTcpSock_GetPerHandleName(unsigned int unPerHandle, char* pstrName, unsigned int unLen); #endif DllExport unsigned int __stdcall GTcpClt_GetProcesserNumber(void); DllExport BOOL __stdcall GTcpClt_IsActive(); DllExport unsigned int __stdcall GTcpClt_GetUseMem(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 信息函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 操作函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DllExport void* __stdcall GTcpClt_GetPerHandleOwner(unsigned int unPerHandle); DllExport BOOL __stdcall GTcpClt_SetPerHandleOwner(unsigned int unPerHandle, void* pOwner); DllExport PGBUF __stdcall GTcpClt_AllocGBuf(void); DllExport BOOL __stdcall GTcpClt_FreeGBuf(PGBUF pGBuf); DllExport unsigned int __stdcall GTcpClt_PostSendGBuf(unsigned int unPerHandle, PGBUF pGBuf, unsigned int unLen); DllExport unsigned int __stdcall GTcpClt_PostSendBuf(unsigned int unPerHandle, unsigned char* pBuf, unsigned int unLen); DllExport void __stdcall GTcpClt_PostBroadcast(unsigned char* pBuf, unsigned int unLen); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 操作函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 主要函数 DllExport BOOL __stdcall GTcpClt_CloseConnect(unsigned int unPerHandle); #if(_USE_UNICODE) DllExport unsigned int __stdcall GTcpClt_OpenConnect( wchar_t* pstrRemoteIP, wchar_t* pstrRemotePort, wchar_t* pstrLocalIP, PGFN_ON_CONNECTED pfnOnConnected, PGFN_ON_RECEIVED pfnOnReceived, PGFN_ON_SENDED pfnOnSended, PGFN_ON_DISCONNECTED pfnOnDisconnected, void* pOwner = NULL); #else DllExport unsigned int __stdcall GTcpClt_OpenConnect( char* pstrRemoteIP, char* pstrRemotePort, char* pstrLocalIP, PGFN_ON_CONNECTED pfnOnConnected, PGFN_ON_RECEIVED pfnOnReceived, PGFN_ON_SENDED pfnOnSended, PGFN_ON_DISCONNECTED pfnOnDisconnected, void* pOwner = NULL); #endif DllExport BOOL __stdcall GTcpClt_Start(unsigned int unHeartbeatTime = 60, unsigned int unMaxNetDelayTime = 5, unsigned int unGuardThreadSleepTime = 2, PGFN_ON_THREAD pfnOnThread = NULL, unsigned int unHndDataInitNumber = 1000, unsigned int unIoDataInitNumber = 1500, unsigned int unProcesserThreadNumber = 0, unsigned int unWorkerThreadNumber = 0); DllExport void __stdcall GTcpClt_Stop(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 主要函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< } /* ... extern "C" */
2025-11-13 17:20:09 1.3MB
1
网上很多的TIdTCPServer 和 TIdTCPClient ,无奈很多都不支持中文消息,发送中文消息都出现很多?? ,故有空做了2个Demo,以便大家参考!Delphi的开发资料越来越少了,希望大家把自己的技术都公布出来,让越来越多的人选择Delphi开发。
2025-03-27 23:01:41 118KB TIdTCPClient IdTCPClient IdTCPServer TIdTCPServer
1
QTcpServer是Qt库中的一个关键组件,用于创建TCP服务器应用程序,可以接收并处理来自多个客户端的连接。在本文中,我们将深入探讨如何利用QTcpServer实现高并发、多线程处理多个客户端的示例,参考自https://blog.csdn.net/hexuyangguang/article/details/106734349。 了解TCP(传输控制协议)的基本概念是必要的。TCP是一种面向连接的、可靠的、基于字节流的传输层通信协议,它确保数据包按顺序到达目的地,且丢失的数据能够重传。在QTcpServer中,我们可以监听特定端口,等待客户端的连接请求,一旦有连接请求,QTcpServer就会创建一个QTcpSocket对象来处理这个新的连接。 QTcpServer的使用通常包括以下步骤: 1. **初始化服务器**:在程序中实例化QTcpServer对象,并连接其`newConnection()`信号到相应的槽函数,以处理新到来的连接。 ```cpp QTcpServer server; connect(&server, &QTcpServer::newConnection, this, &YourClass::handleNewConnection); ``` 2. **启动服务器**:调用`listen()`方法开始监听指定的端口。 ```cpp if (!server.listen(QHostAddress::Any, 12345)) { qDebug() << "Failed to start the server:" << server.errorString(); return -1; } ``` 3. **处理新连接**:当有新连接时,`handleNewConnection()`槽函数会被调用。在此函数中,我们需要接受新连接并为其创建一个新的工作线程,以确保服务器对每个客户端的处理是异步的,防止阻塞其他客户端。 ```cpp void YourClass::handleNewConnection() { QTcpSocket *socket = server.nextPendingConnection(); if (socket) { socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1); // 可选:保持连接活跃 WorkerThread *thread = new WorkerThread(socket); thread->start(); } } ``` 4. **工作线程**:创建一个工作线程类`WorkerThread`,继承自`QThread`。在这个类中,我们将处理与客户端的通信,读取数据,发送数据,以及处理可能出现的错误。 ```cpp class WorkerThread : public QThread { public: explicit WorkerThread(QTcpSocket *socket) : m_socket(socket) {} protected: void run() override { // 在这里处理客户端的连接 ... } private: QTcpSocket *m_socket; }; ``` 5. **读写操作**:在工作线程中,使用`QTcpSocket`的`read()`和`write()`方法进行数据的读取和发送。确保在读写操作前后检查socket状态,以防止未定义的行为。 ```cpp while (m_socket->state() == QTcpSocket::ConnectedState) { QByteArray data = m_socket->readAll(); // 读取数据 // 处理接收到的数据 ... QByteArray sendData; // 准备要发送的数据 m_socket->write(sendData); // 发送数据 m_socket->flush(); // 确保数据立即发送 } ``` 6. **异常处理**:监控`QTcpSocket`的`error()`信号,以便在发生错误时进行适当的清理工作。 ```cpp connect(m_socket, &QTcpSocket::error, this, &WorkerThread::handleError); ``` 7. **关闭连接**:当不再需要与某个客户端的连接时,调用`QTcpSocket`的`disconnectFromHost()`或`close()`方法关闭连接。 通过以上步骤,我们可以构建一个能处理高并发、多线程客户端连接的QTcpServer应用。QTcpServer的强大之处在于其灵活性和可扩展性,可以根据需求进行定制,比如添加认证机制、处理不同类型的请求等。在实际项目中,我们还需要考虑错误处理、资源管理、性能优化等问题,以确保服务器的稳定性和高效性。
2024-08-19 20:37:33 6KB QTcpServer
1
Delphi7+Indy10.5.8开发的一个通讯小软件示例源码,主要完成TidTCPServer 和 TIdTCPClient的通讯。可以做为一个聊天软件的雏形,里面解决通讯的问题,不过,有些情况下还是会有异常,可以一起探讨。
2023-11-09 13:57:34 40KB
1
Unity scoket 通信 包含TCPClient客户端 项目和TCPServer 服务端 项目亲测好用 优质资源! 两个项目都启动修改IP进行监听,通信。
2023-03-14 09:38:38 7.29MB Unity
1
delphi的IdTCPClient和IdTCPServer的Writestring用法
2023-03-07 18:50:06 2KB delphi
1
本项目是Qt下,使用Tcp协议通讯的例子,使用的类主要是QTcpSocket和QTcpServer。好多博客中,有一个很大的误区,就是发送完数据之后,立即调用Socket.disconnectfromHost(),造成数据无法正常的被对方接收到,因此,应当等待关于这个Socket的所有操作完成之后,才能调用Socket.disConnectFromHost()函数
2023-01-13 16:21:50 4KB QT TCP通讯
1
本资源是基于QT的QTcpSocket和QTcpServer实现简单TCP业务demo,包含客户端和服务端工程源码,可以直接运行测试,也可直接用于项目开发。
1
TCP软件和硬件交互网络控制-.NET Core版本,底层原理是socket传输 默认服务端(Server)端口是8208
2022-12-29 11:14:43 52.68MB 工业控制网络 tcpip 网络控制
1