This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes ServerSocket - requests are duplicated Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "ServerSocket - requests are duplicated" Watch "ServerSocket - requests are duplicated" New topic
Author

ServerSocket - requests are duplicated

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Howdy!

I have the following piece of code:



I am connected to the router and I have external IP. I have set port forwarding for 8080 port.

When someone performs GET request from the browser with my IP address:
A.B.C.D:8080

I see:

JavaRanch Rocks!
JavaRanch Rocks!

or even:
JavaRanch Rocks!
JavaRanch Rocks!
JavaRanch Rocks!

but there was only one singular hit.

What can be the root cause of such behaviour?


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

Hi Lucas,

Yes, it is an interesting issue. I'm not much into sockets and all, but out of curiosity, I tried to run the code and faced the same issue.

And I would really like to know why this happens

Meanwhile, I tried to put a counter as part of message (and the counter is increased during each iteration), and below is what I got:

1) When I run the code on a new port (e.g. 5555), then for first time, I get this message 3 times. The last counter on console is 3, however, the counter on browser (which we write via PrintWriter) is 1.

2) If I refresh browser page (i.e. send request again), then counter is 4 on both - console and browser. After this, it remains in sync (on console and browser).

3) I checked after inserting print statements and found that the loop is actually executing multiple times. The only reason I can think of is due to the way browser sends the request, accept method is being invoked more than once (but then, I'm still not sure why we do not get last counter's value '3' on browser).

4) Once those counter values are in sync (i.e. after first refresh), this problem never occurs with that particular port (I also tried restarting the program, in which case, the values were in sync right from counter = 1). To reproduce the problem on the same port, I had to restart the machine.

I hope someone knowledgeable in this might find above analysis helpful.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

but there was only one singular hit.


You're assuming that a single high-level browser activity results in a single connection to your socket. I would say the fact that you're seeing the output multiple times is evidence that that assumption is incorrect. Java code doesn't just execute on its own for no reason. Maybe the browser retries since it didn't get a valid HTTP response. Maybe there's something else unexpected going on in the browser. Whatever the case, if you're seeing that output 3 times, there are definitely 3 connections being made.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

I totally agree.

However, I'm still not getting why there's inconsistency in browser and console data during first request.

When I tried to add counter's value to the message, browser displayed the value as 1 (which means that browser was able to establish the connection in first attempt, and also read the response). So, why there were extra attempts for connection?

Also, for same socket, why this behaviour never happens again (unless I bounce the machine)?

Surprisingly, it works totally fine when I connect to ServerSocket via other Java code instead of browser.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

I believe that Firefox uses some optimisations, e.g. connection redundancy in case of the connection loss.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServerSocket - requests are duplicated
 
Similar Threads
how to get client ip from socket
Sockets!
ServerSocket and thread
ServerSocket problem w/ Jeode EVM on iPaq
How does server make a Socket connection back to the client