Skip to content

Instantly share code, notes, and snippets.

@tikarammardi
Created January 22, 2023 15:45
Show Gist options
  • Save tikarammardi/e9987d3b6d2ef9e188a7e11e62b187fd to your computer and use it in GitHub Desktop.
Save tikarammardi/e9987d3b6d2ef9e188a7e11e62b187fd to your computer and use it in GitHub Desktop.
const net = require("net");
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const port = process.env.PORT ?? 3000;
const host = process.env.HOST ?? "127.0.0.1";
const client = new net.Socket();
client.connect(port, host, () => {
console.log(`Connected to server at ${host}:${port}`);
sendMessage();
});
client.on("data", (data) => {
console.log(`Data from Server: ${data}`);
sendMessage();
});
client.on("close", () => {
console.log("Connection closed");
rl.close();
});
function sendMessage() {
rl.question("Enter message to send: ", (message) => {
client.write(message);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment