/* | |
In the node.js intro tutorial (http://nodejs.org/), they show a basic tcp | |
server, but for some reason omit a client connecting to it. I added an | |
example at the bottom. | |
Save the following server in example.js: | |
*/ | |
var net = require('net'); | |
var server = net.createServer(function(socket) { | |
socket.write('Echo server\r\n'); | |
socket.pipe(socket); | |
}); | |
server.listen(1337, '127.0.0.1'); | |
/* | |
And connect with a tcp client from the command line using netcat, the *nix | |
utility for reading and writing across tcp/udp network connections. I've only | |
used it for debugging myself. | |
$ netcat 127.0.0.1 1337 | |
You should see: | |
> Echo server | |
*/ | |
/* Or use this example tcp client written in node.js. (Originated with | |
example code from | |
http://www.hacksparrow.com/tcp-socket-programming-in-node-js.html.) */ | |
var net = require('net'); | |
var client = new net.Socket(); | |
client.connect(1337, '127.0.0.1', function() { | |
console.log('Connected'); | |
client.write('Hello, server! Love, Client.'); | |
}); | |
client.on('data', function(data) { | |
console.log('Received: ' + data); | |
client.destroy(); // kill client after server's response | |
}); | |
client.on('close', function() { | |
console.log('Connection closed'); | |
}); |
This comment has been minimized.
This comment has been minimized.
Cool snippet dude! |
This comment has been minimized.
This comment has been minimized.
Is it actually cheap to start 5, 10, 100 internal servers? How big is the header of each instance. However, thanks for the example. |
This comment has been minimized.
This comment has been minimized.
Destroying client in handler of event "data" is bad idea since "data" might be emitted multiple times while receiving several chunks of response data ... just consider downloading very large file ... you won't get just one data event on that, but destroy the connection as soon as the first chunk has arrived. |
This comment has been minimized.
This comment has been minimized.
@soletan is there any framework for service oriented architecture which allows to call function of other connected service and registry which handles which services are up and down? |
This comment has been minimized.
This comment has been minimized.
On a nix box you can use net cat from terminal
|
This comment has been minimized.
This comment has been minimized.
I'm not sure you need |
This comment has been minimized.
This comment has been minimized.
thank you! |
This comment has been minimized.
This comment has been minimized.
Hi,
Can you guys help me in this case? |
This comment has been minimized.
This comment has been minimized.
@i-am-vivek if I understand correctly you have to handle the 'error' event or it will kill the process. socket.on('error', function(err) {
console.log(err)
}) Hope this helped! |
This comment has been minimized.
This comment has been minimized.
@i-am-vivek I agree with above, but if you're still having issues,it's likely due to a firewall. If you install 'ufw' it's an easy way to manage your firewall settings, you could just type |
This comment has been minimized.
This comment has been minimized.
@tedmiston How do I pass query parameters to a websocket endpoint? |
This comment has been minimized.
This comment has been minimized.
what does |
This comment has been minimized.
This comment has been minimized.
@ORESoftware it makes the socket writes whatever it reads from the client back to him, hence the "Echo server". |
This comment has been minimized.
This comment has been minimized.
Did any one got this error "net.Socket is not a constructor". |
This comment has been minimized.
This comment has been minimized.
@i-am-vivek
|
This comment has been minimized.
This comment has been minimized.
And i try "Echo server" here: //server:
//client:
|
This comment has been minimized.
This comment has been minimized.
Can someone please give me an example of code about how to use the client in HTML file with Java Scripting? |
This comment has been minimized.
This comment has been minimized.
On windows, telnet can be used as the client |
This comment has been minimized.
This comment has been minimized.
if i set the server file on a server how can i access to that . i mean if i run it on my computer i can access with ip 127.0.0.1 bot if i run it on a server site how can i do that?? |
This comment has been minimized.
This comment has been minimized.
How is the error handling done in the client side? Not sure how it would be done on the client side. |
This comment has been minimized.
This comment has been minimized.
Hi There, var client = new net.Socket(); console log: What would be the best approach to send multiple commands to the same session? Cheers. |
This comment has been minimized.
This comment has been minimized.
Awesome snippet. Thanks for posting. |
This comment has been minimized.
This comment has been minimized.
socket.pipe(socket) is a problem when you don't close the connection for every single request. If you want to receive multiple requests/responses on a socket, the "data" event will be called for "write" too and you will get your own data back. It seems there is no easy way to get around this. |
This comment has been minimized.
This comment has been minimized.
after running the code you showed us.
without "Hello, server! Love, Client."
|
This comment has been minimized.
This comment has been minimized.
Can I send http request on this server? Eg: curl http://127.0.0.1:1337/run_code How do I get this 'run_code' in back-end? |
This comment has been minimized.
This comment has been minimized.
@shaoweilee |
This comment has been minimized.
This comment has been minimized.
How can I deploy the code on the server,, can I use pm2? Thanks |
This comment has been minimized.
This comment has been minimized.
how to use express with net? please help |
This comment has been minimized.
This comment has been minimized.
@abhilash007 why would you use express with net? net is TCP protocol while expressjs serves to http protocol. |
This comment has been minimized.
This comment has been minimized.
@suresach by reading the HTTP headers that were sent along with request. |
This comment has been minimized.
This comment has been minimized.
Thank you! |
This comment has been minimized.
This comment has been minimized.
So I took this example and modified it slightly such that the client writes data to the server. This should be doable since the client socket does support a write. In one of my use cases the client sends a fair amount of data to server in which case I get an ECONNRESET error. Attached are client1 and server1 snippets. I was wondering if anyone has seen this and if they know what is going wrong under the covers. Thanks in advance Client1: var net = require('net'); client.on('close', function() { client.connect(52275, '127.0.0.1', function() { Server1: var server = net.createServer(function(socket) {
hangs after 41020 writes
}); server.listen(52275, '127.0.0.1'); I think I solved my own issue. I believe the issue had to do with not all of the event listeners close, data, drain, end, lookup, timeout were implemented. I have been playing around and did not expect that the client would receive back a data or drain event to be called which in turn I believe resulted in the error. |
This comment has been minimized.
This comment has been minimized.
Hi everyone! i'm connecting to a telnet server: `var net = require('net'); var client = new net.Socket(); client.on('error', function(err) { client.on('data', function(data) { client.on('close', function() { please help!!!!!!. |
This comment has been minimized.
This comment has been minimized.
How can I do this in TypeScript? |
This comment has been minimized.
This comment has been minimized.
Coool. I have my server on C and this connects perfectly with the server. Thanks :D |
This comment has been minimized.
This comment has been minimized.
Why I have error?
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Hi
It works fine , but every time server returns one extra "console.log("!!!!!! promise called");" line. Once in first cycle, twice in second, thrice in third and so on... It keeps adding an client.on listener and crosses the limit of linterers in nodeJS. What is wrong in the above code? How to rectify it? |
This comment has been minimized.
This comment has been minimized.
hi |
This comment has been minimized.
This comment has been minimized.
i need to listen on 5 5 different ports |
This comment has been minimized.
This comment has been minimized.
you could probably set up some kind of nginx proxy_pass redirection scheme that forwards all incoming http requests that come in on a given set of specific ports to a single port that your web server socket has bound to I've done this for a single port to port redirection (in my use case it was coupled with https termination but that is irrelevent to your situation.) in principle I don't see why that wouldn't work. or just run 5 web servers but thats misguided if they are just doing the same work, depending on what your scaling needs are. |
This comment has been minimized.
This comment has been minimized.
client.write do not really send data to the server, it just writes over the same socket of the client. ---edit---- Ok, I just figured it out the server is an ECHO server, which means, it sends what it received. so client.write send information to the server and the server response it, the problem I was having is: the event server.on('data') was not being triggered, that is because server.on('data') do not hold the SOCKET of the server, it held in the moment of the initialization of the server in net.CreateServer callback function. That is it. |
This comment has been minimized.
This comment has been minimized.
Hello, i want to use this to create a 3 way chat service on the command line. I created the server using your code, and connected to it from two terminal windows using the ncat, however the server only echo's each clients request back to that particular client. I want to echo back to each client, so that it feels like a group chat? |
This comment has been minimized.
This comment has been minimized.
This is really fun, I have been playing with this today and you have definitely distracted me. Ignore my previous issue, I have progressed and now have a basic “chat room” with multiple clients.. I have been using ncat as the client, but I need more logic so I’m trying to implement your node client. So far it works, connects to the server and writes the initial text.. But all subsequent message don’t seem to go back to the server? Apart from the very first client.write how do I send a mother client.write after the user hits enter? |
This comment has been minimized.
This comment has been minimized.
This is really fun, I have been playing with this today and you have definitely distracted me. Ignore my previous issue, I have progressed and now have a basic “chat room” with multiple clients.. I have been using ncat as the client, but I need more logic so I’m trying to implement your node client. So far it works, connects to the server and writes the initial text.. But all subsequent message don’t seem to go back to the server? Apart from the very first client.write how do I send a mother client.write after the user hits enter? |
This comment has been minimized.
This comment has been minimized.
newbies who visit this many years later may find this video on youtube helpful to get started with the server side https://www.youtube.com/watch?v=HyGtI17qAjM |
This comment has been minimized.
This comment has been minimized.
Is Actually, is anything other than |
This comment has been minimized.
This comment has been minimized.
Hi, is it possible to use this to connect to a network share (smb1)? |
This comment has been minimized.
This comment has been minimized.
all is fine |
This comment has been minimized.
This comment has been minimized.
this gist was really helpful for me, thank you dude |
This comment has been minimized.
This comment has been minimized.
Hi guys and gals, |
This comment has been minimized.
The output: