Ronak07
Ronak07

Reputation: 894

Why the streams are not seen as string on client side

I have made a simple server and client program where the server reads the data from file and send to the client through TCP socket But the data I am getting is in object and not a simple string ?

So why I cant see the data as plaintext as it is in my data.txt file.

Explanation with example would be appreciated.

Here is my code :-

SERVER CODE

const fs = require('fs');
const net = require('net');

const readableData = fs.createReadStream('data.txt', 'utf8');

const server = net.createServer(socket => {
    socket.on('data', chunk => {
        console.log(chunk.toString());
        socket.write(JSON.stringify(readableData));
    });

    socket.on('end', () => {
        console.log("done");
    })

    socket.on('close', () => {
        console.log("closed")
    })
});

server.listen(3000);

CLIENT CODE

const fs = require('fs');
const net = require('net');

const client = new net.Socket();

client.connect('3000', () => {
    console.log("connected");
    client.write("Server please send the data");
});


client.on('data', chunk => {
    console.log("Data recieved:" + chunk.toString());
});

client.on('finish', () => {
    console.log("Work completed");
})

client.on('close', () => {
    console.log("connection closed");
})

And here is my data.txt file which has simple data

Hello client how are you ?

And the output I'm getting is here :-

Data recieved:{"_readableState":{"objectMode":false,"highWaterMark":65536,"buffer":{"head":{"data":"Hello client how are you ?","next":null},"tail":{"data":"Hello client how are you ?","next":null},"length":1},"length":26,"pipes":null,"pipesCount":0,"flowing":null,"ended":true,"endEmitted":false,"reading":false,"sync":false,"needReadable":false,"emittedReadable":false,"readableListening":false,"resumeScheduled":false,"paused":true,"emitClose":false,"autoDestroy":false,"destroyed":false,"defaultEncoding":"utf8","awaitDrain":0,"readingMore":false,"decoder":{"encoding":"utf8"},"encoding":"utf8"},"readable":true,"_events":{},"_eventsCount":1,"path":"data.txt","fd":35,"flags":"r","mode":438,"end":null,"autoClose":true,"bytesRead":26,"closed":false}

The question why I won't be able to see the data as plaintext on client side as it is in data.txt file.

Upvotes: 0

Views: 118

Answers (1)

jfriend00
jfriend00

Reputation: 707238

Your variable readableData contains a node.js stream object. That's what that variable is. It's only of use in the current node.js instance so it doesn't do anything useful to try to send that stream object to the client.

If you want to get all the data from that 'data.txt' file, you have several choices.

  1. You can just read the whole file into a local variable with fs.readFile() and then send all that data with socket.write().

  2. You can create a new stream attached to the file for each new incoming request and then as the data comes in on the readStream, you can send it out on the socket (this is often referred to as piping one stream into another). If you use higher level server constructs such as an http server, they make piping real easy.

Option #1 would look like this:

const server = net.createServer(socket => {
    socket.on('data', chunk => {
        console.log(chunk.toString());
        fs.readFile('data.txt', 'utf8', (err, data) => {
            if (err) {
                 // insert error handling here
                 console.log(err);
            } else {
                 socket.write(data);
            }
        });
    });

    socket.on('end', () => {
        console.log("done");
    })

    socket.on('close', () => {
        console.log("closed")
    })
});

FYI, you should also know that socket.on('data', chunk => {...}) can give you any size chunk of data. TCP streams do not make any guarantees about delivering the exact same chunks of data in the same pieces that they were originally sent in. They will come in order, but if you sent three 1k chunks from the other end, they might arrive as three separate 1k chunks, they might arrive as one 3k chunk or they might arrive as a whole bunch of much smaller chunks. How they arrive will often depend upon what intermediate transports and routers they had to travel over and if there were any recoverable issues along that transmission. For example, data sent over a satellite internet connection will probably arrive in small chunks because the needs of the transport broke it up into smaller pieces.

This means that reading any data over a plain TCP connection generally needs some sort of protocol so that the reader knows when they've gotten a full, meaningful chunk that they can process. If the data is plain text, it might be as simple a protocol as every message ends with a line feed character. But, if the data is more complex, then the protocol may need to be more complex.

Upvotes: 1

Related Questions