0

I'm new to programming. Developing an TCP client server application using node.js . Start the TCP server and then start the client.Now client is keep sending the each packet 11k bytes of data for every 10ms..After a while though client is sending packets,server is not receiving data.

TCP server code

   var net = require('net');
    var server = net.createServer();  
    var count=0;
    var totalrcvdbytes =0;
    var totalcount=0
    var conn;
    var port = 9000

    server.on('connection', onClientConnected);

    server.listen(port, function() {  
      //console.log('server listening to client on port %j', server.address());
     console.log('server listening to client on port ' + port)

    });

    function onClientConnected(conn) {  

    var remoteAddress = conn.remoteAddress + ':' + conn.remotePort;
    count=1  
    var time = new Date();
    timestamp = time.getHours() + ":" + time.getMinutes() + ":" + 
    time.getSeconds()  
    conn.write("Server Accepted connection")  
    conn.on('data', onConnData);
    conn.once('close', onConnClose);
    conn.on('error', onConnError);

    setInterval(Callbacktimer,1000)

    function onConnData(d) {

     var time = new Date();
     timestamp = time.getHours() + ":" + time.getMinutes() + ":" + 
     time.getSeconds();
     count = count + 1          
     totalrcvdbytes = totalrcvdbytes + d.byteLength;    
    }
    function onConnClose() {
     console.log("\n")
     console.log('connection from %s closed', remoteAddress);
    }
    function onConnError(err) {
     if(err.code == 'EADDRINUSE') {
        console.log('Connection %s error: %s',remoteAddress ,err.message)
       } 
       else
       {
        console.log('Connection %s error: %s',remoteAddress ,err.message)
       }
    }
    }
    function Callbacktimer() {
    console.log("Data Rcvd:",totalrcvdbytes 
    }

Client side code:

    var net = require('net');
    var sleep = require('sleep');
    var client = new net.Socket();

    var pktbuffer;

    function createpktbuff()
    {
        var str = "t";  
        pktbuffer = str.repeat(11405);
        return pktbuffer;
    }

    client.connect(9000, '10.121.56.99', function() {   
        //client.write('Hello, server! Love, Client.');

    });


    client.on('data', function(data) {

        console.log('Connected and Received: ' + data);
        var count =1;       
        while (1) 
        {
        var pkt = "DataReady" + createpktbuff() + " Datacompleted";             
            client.write(pkt);
            sleep.msleep(10)
            console.log('Sent %d  bytes in  packet Num : %d ',      pkt.toString().length,count);
            count = count +1                                
            } 

        }
        //client.destroy(); //disabled for test purpose

    });

    client.on('close', function() {
        console.log('Connection closed');
    });
Chandan Kumar
  • 63
  • 1
  • 1
  • 5
  • How did you determine that the client is still sending the packets? And can you be a bit more precise about what you thought the client would do? Did you expect it to crash? To get stuck in the "write" function and not return from it? To print an error message (even though I don't see any code to do that)? – David Schwartz Dec 18 '17 at 12:07
  • My guess is that your client stopped sending packets. You just think it's sending packets because it logs that it sent a packet every time it calls `write` whether the call succeeds or fails. – David Schwartz Dec 18 '17 at 12:09
  • Possible duplicate of [Detecting TCP Client Disconnect](https://stackoverflow.com/questions/283375/detecting-tcp-client-disconnect) – CodeCaster Dec 18 '17 at 12:09
  • David,You might be right.As i didn't see the packets on server,I suspected the issue. When i tried with open source tool (Hercules)TCP server ,even there i saw the same issue ,packets are not received at server.Client just does "write" and not sure the packets sent out. Now the question how do i make sure client sent the packet – Chandan Kumar Dec 20 '17 at 04:44

0 Answers0