Skip to content

创建TCP服务端 - TcpServerCreate

函数简介

创建TCP服务端(基于回调的事件驱动模式)。

接口名称

TcpServerCreate

DLL调用

c
int64_t TcpServerCreate(int64_t instance, const char* bind_addr, int32_t port, TcpServerCallback callback, int64_t user_data, int32_t enable_packet_protocol);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
bind_addr字符串绑定地址,空或"0.0.0.0"表示所有接口。
port整数型端口号。
callbackTcpServerCallback事件回调函数。
user_data长整数型用户自定义数据,会在回调时传回。
enable_packet_protocol整数型是否启用消息分包协议:1=启用(推荐),0=禁用(原始模式)。

回调函数类型定义

c
void TcpServerCallback(int64_t server_handle, int64_t conn_id, int32_t event_type, int64_t data, int32_t data_len, int64_t user_data);
参数名类型说明
server_handle长整数型服务端句柄
conn_id长整数型连接ID(用于标识不同的客户端连接)
event_type整数型事件类型(详见下方事件类型表)
data长整数型数据指针(仅当 event_type=1 时有效)
data_len整数型数据长度(仅当 event_type=1 时有效)
user_data长整数型用户自定义数据,由创建服务端时传入

事件类型说明

event_type事件名称说明
0新连接有新客户端连接到服务器(conn_id为新连接的ID)
1接收数据从客户端接收到数据(data指向数据,data_len为长度)
2连接断开客户端断开连接(conn_id为断开的连接ID)
3发送完成数据发送完成

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0);
csharp
using OLAPlug;

var ola = new OLAPlugServer();
long server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0);
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
long server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0);
cpp
var ola = com("OlaPlug.OlaSoft")
var server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0)
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0)
text
.局部变量 ola, OLAPlug
ola.创建 ()
server = ola.TcpServerCreate(“0.0.0.0“, 9000, nullptr, 0, 0)
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0);
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0)
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 0);

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long server = TcpServerCreate(instance, "0.0.0.0", 9000, nullptr, 0, 0);
csharp
long instance = CreateCOLAPlugInterFace();
long server = TcpServerCreate(instance, "0.0.0.0", 9000, nullptr, 0, 0);
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)

返回值

返回值说明
(返回值)长整数型,返回服务端句柄,失败返回0。

注意事项

项目说明
格式消息分包协议(enable_packet_protocol=1):格式为[4字节长度前缀(小端序)][消息体],自动解决TCP粘包问题,推荐用于自定义协议通信。
原始模式(enable_packet_proto原始模式(enable_packet_protocol=0):不添加任何协议头,可能出现粘包问题,适用于与第三方系统通信。
回调函数在独立线程中执行回调函数在独立线程中执行,注意线程安全。
使用完毕后需要调用 `TcpServerDest使用完毕后需要调用 TcpServerDestroy 释放资源。
端口被占用时创建会失败端口被占用时创建会失败。