主题
创建TCP服务端 - TcpServerCreate
函数简介
创建TCP服务端(基于回调的事件驱动模式)。
接口名称
TcpServerCreateDLL调用
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 | 整数型 | 端口号。 |
| callback | TcpServerCallback | 事件回调函数。 |
| user_data | 长整数型 | 用户自定义数据,会在回调时传回。 |
| enable_packet_protocol | 整数型 | 是否启用消息分包协议:1=启用(推荐),0=禁用(原始模式)。 |
使用场景
| 场景 | bind_addr | 说明 |
|---|---|---|
| 本机测试 | 127.0.0.1 | 仅本机可连 |
| 局域网服务 | 0.0.0.0 | 所有网卡监听 |
| Echo 服务端 | 0.0.0.0 + 回调回显 | event_type=1 收到后 TcpServerSend |
回调函数类型定义
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, 1);
if (server != 0) {
// 回调 event_type: 0新连接 1收包 2断开
// 服务端已在监听,等待客户端连接
ola.TcpServerDestroy(server);
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
long server = ola.TcpServerCreate("0.0.0.0", 9000, null, 0, 1);
if (server != 0)
{
// 回调处理新连接与收包
ola.TcpServerDestroy(server);
}python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
server = ola.TcpServerCreate("0.0.0.0", 9000, None, 0, 1)
if server:
# 回调 event_type: 0新连接 1收包 2断开
ola.TcpServerDestroy(server)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
long server = ola.TcpServerCreate("0.0.0.0", 9000, null, 0, 1);
if (server != 0) {
// 服务端已在监听
ola.TcpServerDestroy(server);
}go
import "github.com/ola/olaplug/olaplug"
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
long server = ola.TcpServerCreate("0.0.0.0", 9000, nil, 0, 1);
if (server != 0) {
// 服务端已在监听
ola.TcpServerDestroy(server);
}rust
use olaplug::OLAPlugServer;
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
long server = ola.TcpServerCreate("0.0.0.0", 9000, None, 0, 1);
if (server != 0) {
// 服务端已在监听
ola.TcpServerDestroy(server);
}cpp
var ola = com("OlaPlug.OlaSoft")
long server = ola.TcpServerCreate("0.0.0.0", 9000, 0, 0, 1);
if (server != 0) {
// 服务端已在监听
ola.TcpServerDestroy(server);
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
long server = ola.TcpServerCreate("0.0.0.0", 9000, 0, 0, 1);
if (server != 0) {
// 服务端已在监听
ola.TcpServerDestroy(server);
}text
.局部变量 ola, OLAPlug
ola.创建 ()
long server = ola.TcpServerCreate("0.0.0.0", 9000, 0, 0, 1);
if (server != 0) {
' 服务端已在监听
ola.TcpServerDestroy(server);
}aardio
import OLAPlugServer;
var ola = OLAPlugServer();
long server = ola.TcpServerCreate("0.0.0.0", 9000, null, 0, 1);
if (server != 0) {
// 服务端已在监听
ola.TcpServerDestroy(server);
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
long server = ola.TcpServerCreate("0.0.0.0", 9000, 0, 0, 1);
if (server != 0) {
// 服务端已在监听
ola.TcpServerDestroy(server);
}cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long server = ola.TcpServerCreate("0.0.0.0", 9000, nullptr, 0, 1);
if (server != 0) {
// 服务端已在监听
ola.TcpServerDestroy(server);
}原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long server = TcpServerCreate(instance, "0.0.0.0", 9000, 0, instance, 1);
if (server != 0) {
// 注册 OnServerEvent 回调
// server != 0 表示监听成功
TcpServerDestroy(instance, server);
}csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long TcpServerCreate(long ola, string bind_addr, int port, long callback, long user_data, int enable_packet_protocol);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int TcpServerDestroy(long ola, long server_handle);
long instance = CreateCOLAPlugInterFace();
long server = TcpServerCreate(instance, "0.0.0.0", 9000, 0, instance, 1);
if (server != 0) {
// 注册 OnServerEvent 回调
// server != 0 表示监听成功
TcpServerDestroy(instance, server);
}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 = ola.TcpServerCreate(instance, b"0.0.0.0", 9000, 0, instance, 1)
if server:
// 注册 OnServerEvent 回调
// server != 0 表示监听成功
ola.TcpServerDestroy(instance, server)返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 长整数型,返回服务端句柄,失败返回0。 |
注意事项
| 项目 | 说明 |
|---|---|
| 消息分包协议 | enable_packet_protocol=1:格式为 [4字节长度前缀(小端序)][消息体],自动解决 TCP 粘包。 |
| 原始模式 | enable_packet_protocol=0:不添加协议头,适用于与第三方系统通信。 |
| 回调线程 | 回调函数在独立线程中执行,注意线程安全。 |
| 资源释放 | 使用完毕后需要调用 TcpServerDestroy 释放资源。 |
| 端口占用 | 端口被占用时创建会失败。 |
