Click to See Complete Forum and Search --> : Connecting to discrete Java apps
I was wondering how you could connect two discrete Java applications.
I was thinking of devloping a fourm in Java and this was the structure I thought would be simplest and most efficent. A java program that is the fourm. It has subclass for categories and threads and contains all the nessecary data. Occasionally it saves to a file in case of a crash.
I then have the JSP pages that will generate the HTML. They are completly separate from what I can tell.
How would I go about having these two separate entities communicate. Pass the reverence through a txt file? A server client concept? I would like to avoid alot of reading and writing to files if I can help it. Would it be best to store the treads and such as files and the save and access them for every request?
I am sorry for all the questions but any answers would be great.
SSMI
agent_x91
09-26-2006, 06:01 AM
To connect two clients you'd need a server for them both to connect to, or if you're just planning on having one as the server and one as the client that'll work too.
Use ServerSocket and Socket objects to do this, for example part of the server might look like this:
ServerSocket server=new ServerSocket(PORT_NUM);
Socket client=server.accept();
BufferedReader input=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter output=new PrintWriter(client.getOutputStream(),true);
server.close();
//handle communication here
Socket and ServerSocket are both in the package java.net
If the server needs to accept multiple client connections, make the main server a thread and create an object which uses an array of these server threads and starts a new one to listen on the same port whenever a new client connects.
As for communication, text files are insecure and entirely unnecessary. You can use the input and output streams of the Socket object (as seen in the code above) to directly send messages to the other program. Anything sent through the output stream on one program can be read by listening to the input stream on the other.
I just though of a possible problem handling it this way. How would I run a Java app on a hosted server? I know you can run JSP and servlets but a regular java application I am not sure about. Do they let you do this?
SSMI
agent_x91
10-09-2006, 08:35 AM
I've had this problem as well. Unless you're hosting the server from your own computer, it's likely to be impossible. ou could always just hsot the java server from your computer and leave the rest, though.
chazzy
10-09-2006, 08:47 AM
if you have telnet/shell/ssh access, you should be able to with no problem.
also, another way to handle this is to use RMI.
agent_x91
10-10-2006, 08:14 AM
if you have telnet/shell/ssh access, you should be able to with no problem.
Generally you're unlikely to have this unless you are the owner of the PC on which the server is hosted though...
Waylander
10-10-2006, 08:42 PM
You can get virtual private servers that run on vmware type stuff now a days. Shouldnt be a problem to get whatever you want on a web server, you just gotta be able to do it all yourself.
Virtual machines hack, also good for running unix based operating systems without dual boot or messing with partitions.
http://en.wikipedia.org/wiki/Vps
Waylandzor.
chazzy
10-10-2006, 10:57 PM
Generally you're unlikely to have this unless you are the owner of the PC on which the server is hosted though...
really? because the bulk of the places i've seen with any sort of hosting are giving shell access. these are typically VPS or dedicated hosts though.
agent_x91
10-11-2006, 03:26 AM
really? because the bulk of the places i've seen with any sort of hosting are giving shell access. these are typically VPS or dedicated hosts though.
Really? I've never seen those. I'm more of a stand-alone java programmer than a web-based one though.