tencent cloud

Tencent Cloud Observability Platform

Conn Overview

下载
聚焦模式
字号
最后更新时间: 2025-03-11 20:05:06
By using the new socket.Conn method, you can create a Socket instance. The parameters of this method include the protocol name (tcp or udp), service address, and service port.

Constructor

new Conn(): Conn

Parameters

Parameter
Type
Description
network
string
The protocol name used to establish the connection (tcp or udp).
host
string
The IP address of the service.
port
number
The port of the service.

Methodology

Methodology
Return Type
Description
send()
number
Send request data.
recv()
ArrayBuffer
Receive response data.
close()
void
Close the connection.

Samples

Establish a socket connection to initiate a tcp/udp request.
import socket from "pts/socket";
import util from 'pts/util';
import {sleep} from 'pts';

export default function () {
const tcp_socket = new socket.Conn('tcp', '127.0.0.1', 80);
const send_data = `GET /get HTTP/1.1
Host: 127.0.0.1
User-Agent: pts-engine
\\r\\n`;
tcp_socket.send(util.toArrayBuffer(send_data));
const bytes_read = tcp_socket.recv(512);
tcp_socket.close();
console.log(bytes_read);
sleep(1);
}


帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈