开发者

Node.js: send message to server

开发者 https://www.devze.com 2023-04-02 12:50 出处:网络
I have the following node.js running on my server: var net=require(\'net\'); var util=require(\'util\');

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:

Her开发者_StackOverflowe'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');


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

精彩评论

暂无评论...
验证码 换一张
取 消