/* | |
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.
janbiasi
commented
Sep 2, 2015
Cool snippet dude! |
This comment has been minimized.
This comment has been minimized.
ghost
commented
Sep 20, 2015
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.
soletan
commented
Sep 27, 2015
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.
blikenoother
commented
Nov 16, 2015
@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.
goliatone
commented
Nov 20, 2015
On a nix box you can use net cat from terminal
|
This comment has been minimized.
This comment has been minimized.
anthonygore
commented
Dec 21, 2015
I'm not sure you need |
This comment has been minimized.
This comment has been minimized.
shivasurya
commented
Jan 8, 2016
thank you! |
This comment has been minimized.
This comment has been minimized.
i-am-vivek
commented
Jan 13, 2016
Hi,
Can you guys help me in this case? |
This comment has been minimized.
This comment has been minimized.
Mandrewdarts
commented
Jan 22, 2016
@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.
jaggedsoft
commented
Feb 18, 2016
@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.
niwsa
commented
May 12, 2016
•
@tedmiston How do I pass query parameters to a websocket endpoint? |
This comment has been minimized.
This comment has been minimized.
ORESoftware
commented
Oct 13, 2016
what does |
This comment has been minimized.
This comment has been minimized.
fixmycode
commented
Oct 17, 2016
@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.
Kiran-N
commented
Dec 9, 2016
Did any one got this error "net.Socket is not a constructor". |
This comment has been minimized.
This comment has been minimized.
BonsoirDiep
commented
Dec 15, 2016
•
@i-am-vivek
|
This comment has been minimized.
This comment has been minimized.
BonsoirDiep
commented
Dec 15, 2016
•
And i try "Echo server" here: //server:
//client:
|
This comment has been minimized.
This comment has been minimized.
WisterDesigns
commented
Dec 30, 2016
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.
ddavis914
commented
Jan 17, 2017
•
On windows, telnet can be used as the client |
This comment has been minimized.
This comment has been minimized.
wwmahww
commented
Feb 14, 2017
•
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.
nkkodkani
commented
Apr 5, 2017
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.
gxvigo
commented
Apr 11, 2017
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.
peter-atlanta
commented
Apr 14, 2017
Awesome snippet. Thanks for posting. |
This comment has been minimized.
This comment has been minimized.
marckris
commented
Apr 23, 2017
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.
shaoweilee
commented
May 19, 2017
after running the code you showed us.
without "Hello, server! Love, Client."
|
This comment has been minimized.
This comment has been minimized.
suresach
commented
Jun 12, 2017
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.
thunderrun
commented
Jun 25, 2017
@shaoweilee |
This comment has been minimized.
This comment has been minimized.
Tomvictor
commented
Jun 27, 2017
How can I deploy the code on the server,, can I use pm2? Thanks |
This comment has been minimized.
This comment has been minimized.
abhilash007
commented
Jul 27, 2017
•
how to use express with net? please help |
This comment has been minimized.
This comment has been minimized.
agauniyal
commented
Aug 2, 2017
@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.
agauniyal
commented
Aug 2, 2017
@suresach by reading the HTTP headers that were sent along with request. |
This comment has been minimized.
This comment has been minimized.
ghost
commented
Nov 25, 2017
Thank you! |
This comment has been minimized.
This comment has been minimized.
BigDog6432
commented
Dec 20, 2017
•
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.
manuGallegos
commented
Apr 26, 2018
•
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.
ItsGravix
commented
May 6, 2018
How can I do this in TypeScript? |
This comment has been minimized.
This comment has been minimized.
myroncama12
commented
May 23, 2018
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.
hosseinGanjyar
commented
Jun 11, 2018
Why I have error?
|
This comment has been minimized.
This comment has been minimized.
MingqianYang
commented
Jul 22, 2018
•
This comment has been minimized.
This comment has been minimized.
PRAMANJ
commented
Aug 5, 2018
•
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.
amoopoori
commented
Sep 13, 2018
hi |
This comment has been minimized.
This comment has been minimized.
amoopoori
commented
Sep 13, 2018
i need to listen on 5 5 different ports |
This comment has been minimized.
This comment has been minimized.
stochastic-thread
commented
Oct 9, 2018
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.
klys
commented
Nov 10, 2018
•
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.
ajhstn
commented
Feb 17, 2019
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.
whhb
commented
Feb 18, 2019
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.
ajhstn
commented
Feb 18, 2019
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.
rajnisheu
commented
Sep 14, 2019
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.
SicariusNoctis
commented
Nov 29, 2019
•
Is Actually, is anything other than |
This comment has been minimized.
wangyangkobe commentedAug 1, 2015
The output: