0

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?

Azlam
  • 2,052
  • 3
  • 24
  • 28
  • This answer clears question 1 http://stackoverflow.com/questions/3638953/do-tcp-connections-get-moved-to-another-port-after-they-are-opened/3639017#3639017 – Azlam Nov 12 '10 at 20:13
  • I guess it is something to do with Windows 7 , something with the same process (as in tabs) try to connect to the port – Azlam Nov 14 '10 at 20:45

3 Answers3

2
  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.

khachik
  • 28,112
  • 9
  • 59
  • 94
  • The server is working perfectly from two different IP address, I am not using the client's IP for anything special :) – Azlam Nov 12 '10 at 20:03
  • @Azlam quite strange... And two different instances of the same browser work properly, but the second tab doesn't reach the server? Try firefox with Live HTTP Headers – khachik Nov 12 '10 at 20:17
1

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

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Server is already serving using different threads, i use something like this "new RequestThread(serverSocket.accept()).start();" but serverSocket.accept is not getting called, if I try to open a different tab in the browser, however it gets called if its a different browser instance – Azlam Nov 12 '10 at 19:57
  • @Azlam I assume that the code you have given is continuously running – jmj Nov 12 '10 at 20:01
  • @Azlam And why do you think that the problem is on the server side? – khachik Nov 12 '10 at 20:04
  • @khachik i have another HTTP based MJPEG Streamer running on two tabs at the same time, So I think it has something to do with the server – Azlam Nov 12 '10 at 20:10
  • @Azlam let me clear the point, you mean that when you try from different browser it accepts but if you try from different tab server doesn't accept ?? – jmj Nov 12 '10 at 20:12
  • @Azlam what does the browser say in the second tab? You need to find out does the second connection reach the server. What is the `backlog` value you use for the server socket? – khachik Nov 12 '10 at 20:23
  • @Azlam I doubt it's not the issue of browser cache it shouldn't be also, but for a try cleaning up your cache and try from different tab – jmj Nov 12 '10 at 20:26
  • @khachik, backlog is at 100, I am checking with live headers now, will post the info about it soon – Azlam Nov 12 '10 at 20:28
  • @Azlam Does `RequestThread` constructor have heavy operations inside? Have you overridden `start()` method? – khachik Nov 12 '10 at 20:34
  • @Azlam try putting some SOPs in run method and trace it down – jmj Nov 12 '10 at 20:37
  • @Khachik, org if any of you are free, could you have a look at the sources at http://kitedragon.com/java/ – Azlam Nov 12 '10 at 20:53
  • @Azlam I downloaded you code, and couldn't reproduce your problem. I copied a JPEG file 35 times, corrected the path to the files. Here is what does it do: I open a tab, type http://localhost:8000/, it display the jpeg, and continues loading the page. I open the second tab, do the same there, it displays the image, and continues loading. The same in the third tab. In the forth as well. – khachik Nov 12 '10 at 21:16
  • @Khachik, strange it doesnt work on my browser both in FF and Chrome, Thx it looks more interesting now :) – Azlam Nov 12 '10 at 21:20
  • @Azlam FYI: I use Safari on Mac and java 1.6.0_22. Try on different OSes (client and server). – khachik Nov 12 '10 at 21:27
  • @Azlam now the doubt is bit stronger about cache – jmj Nov 14 '10 at 08:19
0

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.

BillThor
  • 7,306
  • 1
  • 26
  • 19