1

I am new in TCP IP client server communication.

import net from "net";
private static server: net.Server;
private static socket: net.Socket | null = null;

private static createTCPServer(): Promise<{ error: boolean; message?: string; result?: any }> {
    return new Promise((resolve, reject) => {
      const port = 8192;
      this.server = net.createServer((socket) => {
        // Connection Keep alive forever
        socket.setKeepAlive(true);
        console.log(false, "TCP IP client connected." , { ClientIp: socket.remoteAddress , Port: socket.remotePort });
        
        this.socket = socket;
        this.promiseResolve = resolve;
        this.promiseReject = reject;

        this.handleIncomingData(this.promiseResolve, false);

        socket.on('error', (err) => {
          if (err) {
            console.log(true, "TCP IP Client connection reset, Try again later...", err);
          } else {
            console.log(true, "TCP IP Client Socket error: Try again later...", err);
          }
          reject({error: true, message: "TCP Client connection problem, Try again later...", result : err });
        });

        // Handle Client Disconnection
        socket.on('end', () => {
          console.log(false, "TCP IP Client Disconnected ..", { ClientIp: socket.remoteAddress , Port: socket.remotePort });
          reject({ error: true, message: "TCP IP Client Disconnected." });
          }
        );

        socket.on('close', hadError => {
          if (hadError) {
            console.log(true, "TCP IP Client Disconnected with error.", hadError);
          } else {
            console.log(false, "TCP IP Client Disconnected gracefully.", hadError);
          }
          reject({ error: true, message: "TCP IP Client Disconnected.", result: hadError});``
        });
      });

      this.server.listen(8192, () => {
        console.log(false, `TCP Server Listen at port 8192`);
        resolve({ error: false, message: `TCP Server Listen at port ${port}` }); // Resolve the promise when the server is successfully listening
      });

      this.server.on("error", (err) => {
        console.log(true, `TCP Server error: ${err}`);
        reject({ error: true, message: "TCP Server Error.", result: err }); // Reject the promise if there's an error starting the server
      });
    });
  }

This is my TCP IP Server code which is listen on 8192 port when my nodejs application starts. I have only single client server communication only. After this the TCP client connects and communication starts. But after some attempts when i send TCP packet from my server to client. socket is destroying. How i identify this ?

if (!socket.destroyed) {
      socket.write(packet);
   } else {
      reject({ error: true, message: "Socket Destroyed Try again later." });
   }
let onResponseReceived;
onResponseReceived = (response) => {
        if (response) {
          console.log(false, `Received data from NMDL Client, Data ${this.socket.listenerCount("data")}.`, response.toString().trim());
          resultResponse = Client_Response(response.toString().trim(), resolve);
            this.socket.removeListener("data", onResponseReceived);
}
}

And this is how i am handling the TCP IP response which i am received from NMDL client.

I don't want to destroy the socket. I want to reuse the same socket.

To temporary resolve this issue, I restart my nodejs server or a client to solve this issue.

Application flow is : -

Step 1 - Nodejs server starts <------------------- TCP client connects

Ways to receive response from client : -

Step 2

  1. I am receiving continuously response packets from client.
  2. When i send some packet then i am receiving response. (During this i am facing socket destroying issue).

Any idea about this issue ? I am not able to identify why this issue comes.

I don't want to destroy the socket. I want to reuse the same socket.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.