0
votes

I know one socket connection are established by both Server Socket and Client Socket. And I read some documents said one Server Socket could serve many Client Sockets, means one Server Port could server multi Client Ports.

1.But I wonder that does Server use random ports to server different Clients after connection under hood, or Server just uses the same port listening and serving many client's connections ?

2.If so, when I implement a Server and Client Socket Connection, could I random a new port to establish a new Server Socket and tell Client to reconnect to new Server Socket, and the listening Server Socket just keep listening other clients ? it means use different port to server different clients ?

3.And what is the advantage of using one Server Socket(port) to server many Client? and advantage of using multi Server Sockets(ports) to server different Clients?

Thank you

1

1 Answers

2
votes

The two value that idenify each end point, ip address and port number often called socket.

  • A server socket listens on a single port. All established client connections on that server are associated with that same listening port on the server side of the connection.Multiple connections on the same server can share the same server-side IP/Port pair as long as they are associated with different client-side IP/Port pairs, and the server would be able to handle as many clients as available system resources allow it to.

    var express = require('express');
    var app = express(); var server = require('http').createServer(app); var io = require('socket.io')(server); server.listen(4200);

Here u can attach your http port with socket.io.

  • by using a random client-side port, in which case it is possible to run out of available ports if you make a lot of connections in a short amount of time.

for more detail visit this site