Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tcp server cannot emit 'end' or 'close' event when 'data' event isn`t listened #35825

Closed
chengzhuo5 opened this issue Oct 27, 2020 · 2 comments
Closed

Comments

@chengzhuo5
Copy link

  • Version: 14.14
  • Platform: Windows
  • Subsystem: Windows 10

Server code:

const server = createServer((socket) => {
  socket.on('end', () => {
    console.log('end');
  });
  socket.on('close', () => {
    console.log('close');
  });

  // Without this handler 'end' and 'close' event cannot be emited.
  // socket.on('data', (data) => {
      // console.log(data.toString());
  // });
})
server.listen(8080);

Client code:

const client = createConnection({ port: 8080 }, () => {
  client.end('Bye');
});

If there is not 'data' event handler in server code, client.end cannot emit 'end' or 'close' event of server.

@lpinca
Copy link
Member

lpinca commented Oct 27, 2020

That's expected, you have to read all the buffered data before the 'end' event is emitted. If you don't want to add a listener for the 'data' event, you can resume the socket.

socke.on('end', () => {
  console.log('end');
});

socket.resume();

@chengzhuo5
Copy link
Author

chengzhuo5 commented Oct 27, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants