Run a Server App Always on an http?

Hey guys,

I’m making a simple server->multi client program to make a room for people to find other users available for my online games. I want to gt a prototype applet version up first, where there is a server application running on my website (at all times), that clients will connect to and recieve/forward chat/stat information to. The server basically manages all connections and sends each one char information and available players. The clients have the GUI and let the user enter/save information to be sent to the server.

At this point, I simply want to create a Java application to represent the server, rather than going into SQL or anything like that. How can I have a Java application constantly running on a server that I can’t see? (I access it thropugh ftp) Also, would there be a way to perhaps have clients share server load, like how AIM makes chat rooms? If one person is in the room they count as the server, and once they leave another person takes their place, etc.

Data sent is realtively small and there shouldn’t be too many users, so bandwidth and the works shouldn’t be an issue. What’s the best way to do this?

The easiest solution would be to create a dynamic web page (e.g. .jsp or .php…).
The web page appears as a server. With the client you can send requests to
the web site as it would be a real server. e.g.


> open the web page: Open http://x.com/server.jsp?username=x&password=y
>     -> stuff after the "?" is the data you send to the page
> server.jsp sends an "answer" back such as "OK" or "NOK"

There are some problems to this approach (server.jsp only answers to requests,
never sends out data by itself)

To have an actual server running, you will need access to a server so you
can install a java program and run it at startup. You need a static IP address.
The dynamic web page solution is much cheaper.

Longterm wise, I would opt for a server, rather than .jsp though. However, playing
around with some dynamic web pages will give you some easy start and then you
just go from there …

Okay, great. JSP sounds good, except I need it to at least be able to send all connection IPs back to the Client’s. What about php or sql? Are there any good Java packages that can easily connect to these, or something of the sort?

Or alternatively, can you think of a way I could somehow connect only clients to one another? In this case every client would need to know every other client, then you would connect to any single client and have it forward to you all the other one’s connected. This would put more strain on each client than I would like, but I’d rather not learn all of SQL or PHP to do this.

Thanks.

Have a look at JXTA, it’s dedicated to peer discovery and data exchange between peers (p2p).

It’s all java, and works through firewalls, it’s been used to build IM clients, file sharing applications and many other things.

Lilian

Clients that are behind a router, firewall, proxy … are hard to connect. The only reliable way to connect clients to each other is to have a server running with global IP address. Some clients do have global IP addresses, but you can’t count on that. Unless you just want your game to run on a local area network?

SQL? You don’t really need a database. Longterm wise, you will eventually need one, but don’t worry about it now. Once you got it working, start playing around with a database.

That’s the code you need to get started:

On the server you can have PHP, JSP, Perl, … whatever you like. You can test it really easy. Create a file such as test.jsp with the follwoing text:


  Hello, I am the ONLY text in test.jsp

Then, with the Java Applet, connect to “test.jsp” and read the content:


URL url = new URL("http://mysite.com/test.jsp");
DataInputStream is = new DataInputStream(url.openStream());
System.out.println("That's what came from the server: " + is.readLine());

Note: That’s pretty much all you need to connect to the server. The Java code for the Applet is really short (sorry, it won’t compile, I don’t quite recall the details but it’s around 3-5 lines of code). Now, to go further, you can add code to “test.jsp”. Or PHP, Perl etc will do as well.

Okay, very nice, very nice. Can I create an output stream to write into the .jsp document? I’ll definitely start with that, then perhaps put some Perl into it. If I make this an applet that runs from a website, it’s all the same, correct?

No, you cannot get an output stream, however you can pass arguments to the page. You are familiar with HTTP_GET and HTTP_POST?

HTTP_GET: http://myhost.com/test.jsp?login=account1&password=somepass
You can obtain “account1” and “somepass” by checking the form fields “login” and “password”. It’s like submitting a html form.

An yes, the Java code goes into the applet! Note: if you put your Applet onto http://myhost.com/applet.html, you can ONLY connect to that host (http://myhost.com). You cannot connect to “http://www.cnn.com” from the Applet. So make sure the PL script is on the same web site as the Applet.

Ah, okay, that’s very important to know then. So I technically could not do this with a Java application then?

I am familiar with HTTP_GET and HTTP_POST, but don’t know their usage. I’ll look them up, however, shouldn’t be very difficult.

Yes you can use a Java application but it has to run on the web server. That would infact be the best option if it is available for you.