Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6243349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:07:28+00:00 2026-05-24T12:07:28+00:00

[as you’ll see, I don’t understand the basic concepts of a TCP server and

  • 0

[as you’ll see, I don’t understand the basic concepts of a TCP server and client very well and probably socket.emit is not even possible, but I’d like to know the best alternative or similar thing…]

Socket.io has a beautiful thing to emit events and catch them on the other side, it’s in it’s first page (http://socket.io). Can I do something similar like that but with NodeJS’ regular ‘net’ module ? If not then what’s the equivalent ?

I tried:

server.js

var server = net.createServer(function(socket) {
    socket.on("connect",function() {
        socket.emit('test',{msg : 'did you get it ?'});
    });
}).listen(8000);

client.js

var client = net.createConnection(8000, localhost);
client.on('connect',function(){
    client.on('test',function(data) {
        console.log(data.toString());
    });
});

But as you can imagine, it does not work. How can I achieve this ?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:07:28+00:00Added an answer on May 24, 2026 at 12:07 pm

    Well, net is just an interface to TCP. To send and receive messages you need to design and implement your own protocol on top of TCP. TCP is a stream-oriented protocol and not message-oriented. This means that you must invent a way for the reader to separate messages. The simplest way to separate messages are to insert \n characters between them. The simplest way to encode messages as a byte stream is to use JSON.stringify. So:

    client.js

    var Lazy = require('lazy'), net = require('net')
    
    var client = net.createConnection(8000)
    
    new Lazy(client).lines.forEach(function (msg)
    {
        console.log(JSON.parse(msg))    
    })
    

    server.js

    var net = require('net')
    
    var server = net.createServer(function(socket) {
        socket.on("connect",function() {
        var str = JSON.stringify({foo : 'test', msg : 'did you get it ?'}) + "\n"
            socket.write(str)
        });
    }).listen(8000);
    

    You can start from this and elaborate. For example, you can use EventEmitter library class on receiver side and emit different events upon receiving different messages.

    The ‘lazy’ module is available on NPM and is used to split the receiving byte stream into separate lines. The splitting is doable by hand, but it will require like 20 more lines of code. See the sources of ‘dirty’ NPM module for en example implementation of splitting – it’s cumbersome, so having an external dependency is well-grounded in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.