0

i need your help... i am new to TCP.. I had implemented TCP server and client... tcp server write data when connection is established and wait for kernal buffer getting empty then i emit end event from TCPserver and same as client received data from server in end event...

TCP server.js

  const TCPserver = net.createServer().listen(4040);
var recvData='';

TCPserver.on('connection',(TCPclient)=>{
  TCPclient.setKeepAlive(true); 
      console.log(`TCP Client Connected ${TCPclient.localAddress}:${TCPclient.localPort}`);
      var strData=JSON.stringify({"msg":"testRequest from Cortana"})

      var is_buffer_null=TCPclient.write(strData);
        if(is_buffer_null){ 
          TCPclient.end();
         }

        TCPclient.on('data',(data)=>{
        recvData+=data;
     })

     TCPclient.on('end',()=>{
       console.log("Packet is received : "+recvData);
       recvData='';
     })

     TCPclient.on('drain',()=>{
      TCPclient.end();
     })

     TCPclient.on('error',(err)=>{
       console.log(err);
     })

     TCPclient.on('close',(hardError)=>{

       console.log("Client disconnected");
       if(hardError){ console.log("TCP error"); }
     })

})

TCPclient.js

var TCPcortana = net.connect({ port: 4040, host:'127.0.0.1',allowHalfOpen:true},()=>{
  console.log(`server connected to ${TCPcortana.localAddress}:${TCPcortana.localPort} `);
});

  TCPcortana.setKeepAlive(true); 

 TCPcortana.on('data',(data)=>{
    recvData+=data;
 })

 TCPcortana.on('end',()=>{
   console.log("Packet is received : "+recvData);
   processReq(recvData);
    recvData='';  
 })

 TCPcortana.on('drain',()=>{
   TCPcortana.end();
 })

 TCPcortana.on('error',(err)=>{
   console.log(err);
   setTimeout(()=>{
    TCPcortana.connect({ port: 4040, host:'127.0.0.1',allowHalfOpen:true});
   },10000)
 })

 TCPcortana.on('close',(hardError)=>{
   console.log("connection disconnected");
   if(hardError){ console.log("TCP error"); }
 })

function processReq(data) {
  console.log("Packet being processed");
   var stringData=JSON.stringify(data);
 var is_buffer_empty=TCPcortana.write(stringData);
 if(is_buffer_empty){
    TCPcortana.end();

}

}

the Tcp client send responses then the client connection close, i am expecting the client was not closed and keep alive the connection between and server and client... please help me...

  • 2
    The server explicit calls end on “drain”.. why would it stay open after such? Search for “TCP keepalive” to see what it _really_ does: it does not prevent a connection from being explicitly closed, nor does it auto-reconnect. – user2864740 May 02 '20 at 05:39
  • 2
    If you don't want the socket to close, then don't call `.end()` in the client and server. YOU are explicitly telling the socket to close in both client and server code. – jfriend00 May 02 '20 at 05:41
  • without calling end event how can i received full data in data event..? I am confused, tcp sends data as packets , how can i know the data fully received without calling end event explicitly? – Mahesh Kalakattu May 02 '20 at 06:31
  • The code needs to wait until it receives a full _logical_ packet, whatever this means. This is sometimes called “Framing”. For example, to send a command, the on-wire payload might look like FRAMETYPE (int) | NBYTES (int) | BYTES (byte x nbytes). Thus the server knows when a complete chunk of data is sent (and how to handle it at a high level). A number of protocols work on a line-oriented framing (see how HTTP headers are sent and separated from the body). – user2864740 May 02 '20 at 17:21

1 Answers1

0

If you are trying to frequently send messages to the client without closing the connection, you need to use server send events.

Server Sent Events continue to keep the connection alive and the server can send "events" containing some data.

See this answer for more details.

Mubashir AR
  • 86
  • 1
  • 5
  • Server-send events are unidirectional (https://en.wikipedia.org/wiki/Server-sent_events) and not designed for a client to send data back to the server in a bidirectional stream. The accepted answer on the linked question has -17 **downvotes**. – user2864740 May 02 '20 at 17:28