Reputation: 894
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 ?
Upvotes: 2
Views: 783
Reputation: 11456
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);
});
Upvotes: 1