tencent cloud

Tencent Cloud Observability Platform

Conn Overview

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 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);
}


도움말 및 지원

문제 해결에 도움이 되었나요?

피드백