Click here to Skip to main content
15,942,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi , im learning node , im quite new to command prompt and back ned ,and im trying to build TCP server/client chat
my code so far is

server.js

var net=require("net");
var sockets=[];
var server=net.createServer(function(socket){
	console.log("new socket connected");
	sockets.push(socket);
    var sh="";
	socket.on("data",function(data){
		sh=sh+data;
	});
	socket.on("end",function(){
		sockets.forEach(function(x){
			if(x!==socket){
				x.write("Dakto poveda :"+ sh);
				sh="";
			}
		});
	})
	socket.on("close",function(){
		console.log("Connection closed")
	})
}).listen(8888)

and client.js

var net=require("net");
var client= new net.Socket();
client.connect(8888,function(){
	console.log("Conncted");
	client.write("Hello from client")
});
client.on("data",function(data){
	console.log("data "+data)
});
client.on("close",function(){
	console.log("Client closed");
	client.write("client closed")
})


i start server.js then i open new cmds and start client.js in them . In the server file in writes that client has connected , and in the client cmd it writes "connected" and it doesnt write "hello from client" + the question is , how do i communicate between those clients / server? When i start those files , i cant write to cmd anymore
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900