jQuery in Action, 2nd edition*
The moose likes Sockets and Internet Protocols and the fly likes Client / Server communicate smoothly only when on same machine 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 "Client / Server communicate smoothly only when on same machine" Watch "Client / Server communicate smoothly only when on same machine" New topic
Author

Client / Server communicate smoothly only when on same machine

Jay Crawf
Greenhorn

Joined: Sep 15, 2013
Posts: 10
I'm creating a simple server client / server. The client has a list of names and sends them one at a time to the server, the server then tries to locate that name within an arrayList "WhatsWhat.srvrList". If the server finds the name it then returns some additional info from the arrayList to the client.

The communication works perfectly when both client and server are on the same machine, but I'm running into problems when the two are run on separate boxes. When they are run on separate machines, the client receives a seemingly random set of replies. If the client requests info on 10 names, I only end up with replies for 2 to 4. If on the other hand, I run client and server on the same box, I get all 10 replies.

Either the client or the server seems to be dropping information over the network, or somehow lagging behind. Is it unwise to be pulling data out of an arrayList as I've done? How can I ensure that the client waits to get a response for each name it sends?

Server:

Client:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Jay Crawf wrote:Either the client or the server seems to be dropping information over the network, or somehow lagging behind.


What that suggests to me is that when you run the two things on the same machine, they are somehow sharing data in some way other than over the network. I say that because even when you run the client and the server on the same machine, the connection is still made through the operating system's networking code.

When you run the client and server on the same machine, are you running them in separate processes?
Jay Crawf
Greenhorn

Joined: Sep 15, 2013
Posts: 10
Paul Clapham wrote:
When you run the client and server on the same machine, are you running them in separate processes?


I would think so, I don't even know how I could get them to run in the same process.
What I can tell you is that I built both the client and server, and moved the jar files from the development machine to another computer. Running out of separate folders on this new machine, the client and server are able to communicate without issue. But, if I move the client to yet a third machine, the communication is hit and miss... mostly miss, but some of the information does come across.
Jay Crawf
Greenhorn

Joined: Sep 15, 2013
Posts: 10
This is my suspicion, the server is replying to the client with data it is pulling from an ArrayList "WhatsWhatUI.srvrList" - this arraylist is not static and is being actively and continuously updated in another thread... I'm getting the feeling this isn't too safe of a thing to do. When run on the same machine everything happens fast enough so it doesn't matter, but when done over an actual network connection it's slower, and the results get wonky.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Jay Crawf wrote:This is my suspicion, the server is replying to the client with data it is pulling from an ArrayList "WhatsWhatUI.srvrList" - this arraylist is not static and is being actively and continuously updated in another thread... I'm getting the feeling this isn't too safe of a thing to do.


Really? I don't see any threads at all in the code you posted.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41060
    
  43
Seeing the code, and noticing that it causes problems only if run on disparate hosts, I advise to read to read Don't println to a Socket.


Ping & DNS - my free Android networking tools app
Jay Crawf
Greenhorn

Joined: Sep 15, 2013
Posts: 10
Ulf Dittmer wrote:Seeing the code, and noticing that it causes problems only if run on disparate hosts, I advise to read to read Don't println to a Socket.


Hrm, none of the hosts used was a Mac, but worth a shot though. The server's in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); seems totally unable to receive from the clients out.print(anything at all); though.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41060
    
  43
The problem described in the article is not Mac-specific (or OS X-specific); it would happen on any Unix/Linux-based OS.
 
wood burning stoves
 
subject: Client / Server communicate smoothly only when on same machine
 
Similar Threads
Socket : MultiClient Server
Server sending data to Clients Randomly
Socket exception
Server Hangs up after single communication with Client
making Thread of methods