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 6528209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:30:48+00:00 2026-05-25T09:30:48+00:00

I have the following node.js running on my server: var net=require(‘net’); var util=require(‘util’); var

  • 0

I have the following node.js running on my server:

var net=require('net');
var util=require('util');

var server=net.createServer(function(conn){
    conn.on('connect',function(){
        util.puts('connect');
    });
    conn.on('data',function(data){
        util.puts(data);
    });
});
server.listen(5000,'172.16.1.197');

This works just fine and i can telnet to 172.16.1.197:5000 and it behaves as expected.

Now, I have a client running on another box with IP address 172.16.1.218 and I simply want to send a message to the server.

function sendAMessage(payLoad){
    //what to put in here?
}

payLoad should get printed on the server side.

I know this seems so simple, but I really can’t get my head around it – I’m quite new to node.js.

Many thanks in advance,


Adding some code:

Here’s the client side (172.16.1.218):

var http = require('http');
var util=require('util');
var net=require('net');

var localHash={};
var chunkLength=100;

http.createServer(function(request, response) {
    var proxy = http.createClient(80, request.headers['host'])
    var proxy_request = proxy.request(request.method, request.url, request.headers);
    proxy_request.addListener('response', function (proxy_response) {
        proxy_response.addListener('data', function(x) {
            var responseData=x.toString("binary");
            var bytes=getBytes(responseData);
            util.puts(bytes.length);
            for(var i=0;i<bytes.length;i++){
                var hash=DJBHash(bytes[i]);
                //util.puts(hash);
                if(hash in localHash){
                    //in hash, send alias
                    //response.write("<EOH>"+hash+"</EOH>",'binary');
                    response.write(bytes[i],'binary');
                }else{
                    //not in hash, send full chunk
                    updateRemoteHash(hash,bytes[i]);
                    response.write(bytes[i],'binary');
                    localHash[hash]=bytes[i];

                }

            }
            //response.write(bytes,"binary");
        });
        proxy_response.addListener('end', function() {
            response.end();
        });
        response.writeHead(proxy_response.statusCode, proxy_response.headers);
    });
    request.addListener('data', function(chunk) {
        util.puts(chunk);
        proxy_request.write(chunk, 'binary');
    });
    request.addListener('end', function() {
        proxy_request.end();
    });
}).listen(8080,'172.16.1.218');


function getBytes(responseData){
    var f=chunkLength;
    var toTransmit="";
    var p=0;
    var bytes=Array();      

    var N=responseData.length;
    if(N>f){
        p=Math.floor(N/f);

        var hash="";
        var chunk="";
        for(var i=0;i<p;i++){
            chunk=responseData.substr(f*i,f);
            toTransmit=toTransmit+chunk;
            bytes[i]=chunk;
            /*hash=DJBHash(chunk);
            if(localHash[hash]==undefined){
                localHash[hash]=chunk;
                //updateRemoteHash(hash,chunk);
                toTransmit=toTransmit+chunk;
            }else{
                sys.puts("***hit"+chunk);
                toTransmit=toTransmit+chunk;//"***EOH"+hash;
            }*/
        }
        //remainder:
        chunk=responseData.substr(f*p);
        toTransmit=toTransmit+chunk;
        bytes[p]=chunk;
        /*hash=DJBHash(chunk);
        if(localHash[hash]==undefined){
            localHash[hash]=chunk;
            //updateRemoteHash(hash,chunk);
            toTransmit=toTransmit+chunk;
        }else{
            toTransmit=toTransmit+chunk;//"***EOH"+hash;
        }*/
    }else{
        toTransmit=responseData;
        bytes[0]=responseData;
    }
    return bytes;
}
function DJBHash(str) {
    var hash = 5381;
    for(var i = 0; i < str.length; i++) {
        hash = (((hash << 5) + hash) + str.charCodeAt(i)) & 0xffffffff;
    }
    if(hash<-1){
        hash=hash*-1;
    }
    return hash;
}

function updateRemoteHash(hash,chunk){
    var c=net.createConnection(5000,'172.16.1.197');
    c.on('connect',function(){
        c.write(hash);
        c.end();
    });
}

And the server side (172.16.1.197):

var net=require('net');
var util=require('util');

var server=net.createServer(function(conn){
    conn.on('connect',function(){
        util.puts('connect');
    });
    conn.on('data',function(data){
        util.puts(data);
    });
});
server.listen(5000,'172.16.1.197');
  • 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-25T09:30:49+00:00Added an answer on May 25, 2026 at 9:30 am

    You’ve used net.createServer to set up a TCP server. You now need to use net.createConnection to connect to a TCP server.

    function sendAMessage(msg) {
      var c = net.createConnection(5000, '172.16.1.218');
      c.on("connect", function() {
        // connected to TCP server.
        c.write(msg);
      });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following node.js server running on 172.16.1.218: var net=require('net'); var server =
i have the following code: var net = require('net'); var server = net.createServer(function (stream)
I have the following simple http server using Node.js: var http = require('http'); var
I am using socket.io to connect to my node server using the following script:
I have the following HTML node structure: <div id=foo> <div id=bar></div> <div id=baz> <div
I need to have following attribute value in my XML node: CommandLine=copy $(TargetPath) ..\..\&#x0D;&#x0A;echo
I have the following input: <node TEXT=txt> <node TEXT=txt> <node TEXT=txt/> <node TEXT=txt/> </node>
I have the following XPATH line: //det[@nItem=1]/prod/cProd That successfully selects the desired node using
I have atleast 16 functions of the following form. bool Node::some_walker( Arg* arg1 )
I have been playing with Node.js for some time. I have the following piece

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.