professormeowingtons
professormeowingtons

Reputation: 3514

What kind of data can be sent over a socket?

I am learning about Node.js for the first time, and came across the topic of using TCP to send data over a socket in Pedro Teixeira's Hands-On Node.js book.

require('net').createServer(function(socket) {
  // new connection
  socket.on('data', function(data) { 
    // got data
  });
  socket.on('end', function(data) { 
    // connection closed
  });
  socket.write('Some string');
}).listen(4001);

I googled around for examples, and it seems that bytes, UTF-8 strings, etc can be sent over a socket. What I was curious about, and unable to find the answer to, was what sort of (if any) limits are there on sending data over a TCP socket (data-type, size, etc)?

Upvotes: 0

Views: 1096

Answers (1)

David Schwartz
David Schwartz

Reputation: 182819

TCP always provides a stream of bytes with no support for message boundaries. So anything that you can encode as a stream of bytes is fine, just remember that you have to do that encoding in a way that the receiver can decode it.

Upvotes: 1

Related Questions