2

Server Side code

var net = require('net'); var server = net.createServer((connection) => { console.log('server connected'); connection.on('data', (data) => { console.log('data received'); console.log('data is: \n' + data); }); }); var HOST = '127.0.0.1'; var PORT = '8000' server.listen(PORT, HOST, function() { //listening console.log('server is listening to ' + PORT + '\n'); server.on('connection', function(){ console.log('connection made...\n') }) }); 

Client Side Code

var client = new net.Socket() //connect to the server client.connect(PORT,HOST,function() { 'Client Connected to server' //send a file to the server var fileStream = fs.createReadStream(__dirname + '/readMe.txt'); // console.log(__dirname + '/readMe.txt'); fileStream.on('error', function(err){ console.log(err); }) fileStream.on('open',function() { fileStream.pipe(client); }); }); //handle closed client.on('close', function() { console.log('server closed connection') }); client.on('error', function(err) { console.log(err); }); 

I want to know how can we achieve creating a client and a TCP server and sending multiple data from only one client to server.

I know there can be multiple clients that can connect to server that request to server and get response back but I don't want that, I want to know is it possible that a single client can send multiple data streams to a server in node.js.

The thing is suppose there is a file in which 200 lines of chunk data is present so I know we can read that file using createReadStream but suppose there are multiple files which has 200 lines of data (example) so how to send these multiple files over TCP server

Any example would be appreaciated.

Please give an explanation using a example as I am new to node.js

The example above is sending the data of one file to the server, My question what if the client want to send hundreds of files (or any data streams), So how can he send to through a single medium to TCP server ?

1 Answer 1

1

This is possible using the net module, the fs module, and a basic forEach construct for looping over the files:

server.js

const net = require('net'); const host = "localhost"; const port = 3000; const server = net.createServer((connection) => { console.log('server connected'); connection.on('data', (data) => { console.log(`data received: ${data}`); }); }); server.listen(port, host, function () { console.log(`server is listening on ' + ${port}`); server.on('connection', function () { console.log('connection made...\n') }) }); 

client.js

const net = require("net"); const fs = require("fs"); const port = 3000; const host = "localhost"; const files = [ "file1.txt", "file1.txt", "file1.txt" // As many files as you want ] const client = new net.Socket() client.connect(port, host, function () { files.forEach(file => { const fileStream = fs.createReadStream(file); fileStream.on('error', function (err) { console.log(err); }) fileStream.on('open', function () { fileStream.pipe(client); }); }); }); client.on('close', function () { console.log('server closed connection') }); client.on('error', function (err) { console.log(err); }); 
Sign up to request clarification or add additional context in comments.

7 Comments

I want tcp server, the files should be sent to tcp server
Okay, do you need to set up the TCP server yourself as well? Or does it exist already?
yeah I need to setup it myself
Could you give me some more information about this tcp server? Why should it be a tcp server? Do you have to write it from scratch, or is it an existing software package? If it is an existing software package, which one is it and what version will you use? Will it be secured and will authorization be required to connect to it?
It should be simple tcp server created using net modules. I want a simple demo example where I can send multiple files from client to tcp server
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.