Skip to content

Instantly share code, notes, and snippets.

@ramesaliyev
Created May 20, 2022 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramesaliyev/48d904203ce93f52b81670a371692e8a to your computer and use it in GitHub Desktop.
Save ramesaliyev/48d904203ce93f52b81670a371692e8a to your computer and use it in GitHub Desktop.
tcp-conn
var net = require("net");
process.stdin.setEncoding('utf-8');
var connection = net.connect(10000, "192.168.137.1", function() { // connect to the server we made
console.log("client connected");
// When receive user input data in console.
process.stdin.on('data', function (message) {
// If user do not input data in command line then send default message.
if (message == null || message.length == 0) {
message = "Hello udp server.";
}
// console.log("User input : " + message);
// Create a node Buffer object to wrap message object.
message = Buffer.from(message, 'utf-8');
connection.write(message)
// Send message to udp server through client socket.
});
});
var net = require("net"); //nodejs version of imports
var server = new net.Server(); // a new raw tcp server
server.on("connection", function(client) { // on connection event, when someone connects
console.log("server connections: " + server.connections); // write number of connection to the command line
client.on("data", function (data) { //event when a client writes data to the server
console.log("Server received data: " + data); // log what the client sent
});
});
server.on("message", function (message) {
process.stdout.write(output);
});
server.listen(10000, "192.168.137.1"); // listen on port 10000, localhost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment