create a tcp client in typescript

index.ts
import net from "net";

const HOST = "localhost";
const PORT = 8080;

const socket = new net.Socket();

socket.connect(PORT, HOST, () => {
  console.log("Connected to server at " + HOST + ":" + PORT);
  socket.write("Hello server!");
});

socket.on("data", (data: Buffer) => {
  console.log(`Received data from server: ${data}`);
  socket.end();
});

socket.on("close", () => {
  console.log("Disconnected from server");
});
425 chars
21 lines

This code creates a TCP client using the net module in Node.js. It connects to a server at the specified HOST and PORT and sends a message. When it receives data from the server, it logs the data and closes the socket. The client also logs when it connects and disconnects from the server.

gistlibby LogSnag