主题
获取所有连接ID - TcpServerGetAllConnectionIds
函数简介
获取所有连接的ID列表。
接口名称
TcpServerGetAllConnectionIdsDLL调用
c
const char* TcpServerGetAllConnectionIds(int64_t instance, int64_t server_handle);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| server_handle | 长整数型 | 服务端句柄。 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0);
string ids = ola.TcpServerGetAllConnectionIds(server);csharp
using OLAPlug;
var ola = new OLAPlugServer();
long server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0);
string ids = ola.TcpServerGetAllConnectionIds(server);python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0)
ids = ola.TcpServerGetAllConnectionIds(server)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
long server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0);
String ids = ola.TcpServerGetAllConnectionIds(server);cpp
var ola = com("OlaPlug.OlaSoft")
var server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0)
var ids = ola.TcpServerGetAllConnectionIds(server)vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0)
ids = ola.TcpServerGetAllConnectionIds(server)text
.局部变量 ola, OLAPlug
ola.创建 ()
server = ola.TcpServerCreate(“0.0.0.0“, 9000, nullptr, 0, 0)
ids = ola.TcpServerGetAllConnectionIds(server)aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0);
var ids = ola.TcpServerGetAllConnectionIds(server);text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0)
文本型 ids = ola.TcpServerGetAllConnectionIds(server)cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0);
string ids = ola.TcpServerGetAllConnectionIds(server);原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long server = TcpServerCreate(instance, "0.0.0.0", 9000, nullptr, 0, 0);
long idsPtr = TcpServerGetAllConnectionIds(instance, server);
if (idsPtr != 0) {
char ids[512] = {0};
GetStringFromPtr(idsPtr, ids, sizeof(ids));
FreeStringPtr(idsPtr);
}csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
long instance = CreateCOLAPlugInterFace();
long server = TcpServerCreate(instance, "0.0.0.0", 9000, nullptr, 0, 0);
long idsPtr = TcpServerGetAllConnectionIds(instance, server);
if (idsPtr != 0) {
StringBuilder ids = new StringBuilder(GetStringSize(idsPtr) + 1);
GetStringFromPtr(idsPtr, ids, ids.Capacity);
FreeStringPtr(idsPtr);
string idsStr = ids.ToString();
}python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
server = TcpServerCreate(instance, "0.0.0.0", 9000, nullptr, 0, 0)
idsPtr = TcpServerGetAllConnectionIds(instance, server)
if idsPtr:
buf = create_string_buffer(512)
ola.GetStringFromPtr(idsPtr, buf, 512)
ola.FreeStringPtr(idsPtr)
ids = buf.value.decode("utf-8")返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 字符串指针,连接ID之间用逗号分隔(如"1,2,3"),失败返回NULL。需调用 FreeStringPtr 释放内存。 |
注意事项
| 项目 | 说明 |
|---|---|
| 释放内存 | 返回的字符串需要调用 FreeStringPtr 释放内存。 |
| 如果没有连接 | 如果没有连接,返回空字符串""。 |
| 可用于广播消息、统计连接数等 | 可用于广播消息、统计连接数等。 |
