Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Asynchronous Client-Server Communication (Proxy)  (Read 1260 times)
0 Members and 1 Guest are viewing this topic.
Offline thofoer

Innocent Bystander




Java games rock!


« Posted 2005-03-24 06:52:31 »

Hi!

I need to implement a bidirectional, fully asynchronous client-server
communication. Normally the java.net-API would do fine, but I also have
to deal with firewalls/proxies. So I'm trying to use servlets and HttpClient.

There are many examples of servlet-applet/application communication, but due to the nature
of HTTP everything is a synchronous "client-sends-request server-sends-response".
In my application client and server will have to send and receive data from the other side
whenever they want. So I had the idea of creating two connections, one for upstream and
one for downstream. These connections will have to be kept alive during runtime of the
client session.

That's the theory. In practice I don't get the output-stream client->server to work.
Here's a little code I wrote with the java.net-API:

In the servlet:


public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
   BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream()));
   while(true) {    
       Object l = in.readLine();           // read anything from the input stream and print it
       System.out.println("recv-->"+l);
       if (l==null) return;
   }        
}
   


Client-side (output-stream to server):


       url = new URL( "http://127.0.0.1/url_to_servlet" );

URLConnection con = url.openConnection();  
con.setDoOutput(true);
con.setDoInput(true);          
con.setUseCaches(false);
PrintWriter out   = new PrintWriter(con.getOutputStream());
out.println("Hello?\n");
out.flush();      

This doesn't work. Nothing is received by the servlet. Only when I add a


con.getInputStream();

to the client code (after out.flush()) the servlet receives the string. But then I cannot
use the output stream anymore.


What am I doing wrong?
Is that asynchronous communication possible at all?

Thank you for reading my post. Any help appreciated.

Thomas
Offline kevglass

« JGO Spiffy Duke »


Medals: 221
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2005-03-24 09:32:52 »

Is this another one of the Java URL only supports HTTP 1.0 type bugs? A streaming upstream to HTTP wasn't supported til later?

Kev

Offline vrm

Junior Devvie




where I should sign ?


« Reply #2 - Posted 2005-03-25 06:57:24 »

try with jakarta commons-httpclient

it's more complete than Sun one
http://jakarta.apache.org/commons/httpclient/features.html
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cknoll

Junior Devvie




Flame On!


« Reply #3 - Posted 2005-03-30 15:31:50 »

I'm not so sure this is a bug in the spec or related to 1.1 vs 1.0 implementation of HTTP...I seem to recall reading that when you make a URLConnection to send data to the server, the process of 'writing' to the output stream doesn't mean that you have made a connection to the server, rather that you are preparing the buffer of data that will be sent to the server when the connection is established (which happens when you call getInputStream() (which internally makes the connection to the server, posts the raw data, and then reads the response byte stream).

This is just how http transactions work. I think you may be mis-using the URLConnection class to work around a infrastructure issue (ie: firewalls).  You may want to find another way.  Or re-think how you want your communication architecture.  Webservers aren't built to have a persistant connection open (let alone 2 for inputs and outputs) throughout the life of a session, so you could just be running into a wall with this thought process. What you could do is have a URLConnection that sends data to the server which then queues up requests for that client (and processes it when it's ready) and another connection to read the results of the requests, but in both cases a connection is opened, data sent, and data recieved, and then the connection is closed.  This is the way of HTTP.  But this is where persistant connections could save you the trouble of re-opening physical connections to the server transparently.


-Chris
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (10 views)
2014-12-27 04:03:04

TheDudeFromCI (13 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (84 views)
2014-12-05 12:39:02

SHC (94 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!