Created
January 22, 2023 15:45
-
-
Save tikarammardi/e9987d3b6d2ef9e188a7e11e62b187fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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