简体   繁体   中英

ServerSocket and Multiple Clients from Same IP

I understand from earlier SO posts, that you need multiple process (when each process has the same IP) to connect to a ServerSocket. Is this correct?

Then how does a webserver work when you open a website simultaneously in two different tabs of a browser? (Assuming the tabs are running in the same process)

To be exact, I was writing a MJPEG Streamer using ServerSocket, it works fine in one tab of a browser instance, if i open another tab to the same URL, the server never accepts the incoming connection.

How can it be achieved?

  1. No.
  2. Several reasons. Check your server code, debug it when the second client from the same IP is connected.

Does your server work properly, if you open two connections in parallel from different IP addresses? If yes, do you use the client's IP address for something special? If yes, you should consider using IP:PORT of clients as an identifier, not IP only.

Then how does a webserver work when you open a website simultaneously in two different tabs of a browser? (Assuming the tabs are running in the same process)

server serves each request in separate thread.

The basic flow of logic in such a server is this:

while (true) {
    accept a connection ;
    create a thread to deal with the client ;
end while

Go Through Supporting Multiple Clients Section

No, a single process can open multiple sockets. Most browsers can/will open multiple connections a web site to download resources such as CSS and graphics files. There are several techniques which can be used to handle this. Usually threading is used, but multiplexed I/O can also be used.

These have different ports on the browser end. The server differentiates connections by IP Addresses and port. Servers usually use multiple threads, multiple processes, multiplexed I/O, or a combination of these.

The browser should be able to handle multiple tabs connecting to the same web site. I frequently run multiple multimedia tabs to the same site in Firefox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM