I am trying to make a Java application in a client/server way. The client is a GUI in SWT which displays data from the server. The server is connected to a database.
Ok, sorry for that, it's a classic question I'm sure, but I don't know how to start.
In a project I worked for they implemented a lot of magic with Proxy.newProxyInstance()
to invoke the Glassfish server transparently.
I don't want to use a Glassfish server. I just want something simple in plain Java. But the concept of proxies seems pretty cool.
Do you have ideas or examples of such thing? How do I write the server part to handle the requests of clients?
Thanks in advance
Fluminis
I'm going to explain TCP:
The basic concept is that you have to run a "Server" on a machine. That server accepts clients waiting for a connection. Each connection goes over a port (you know, I hope...).
Always use ports above 1024 because ports lower than 1025 are most of the time reserved for standard protocols (like HTTP (80), FTP (21), Telnet, ...)
However, creating a Server in Java is done this way:
"Socket" is the word you are probably looking for if you want to do research.
And to connect your client to a server you have to write this:
But now, there isn't still a connection. The server has to accept the waiting client (as I noticed here above):
Done! Your connection is established! Communicating is just like File-IO. The only thing you have to keep in mind is that you have to decide when you want to flush the buffer and really send the data through the socket.
Using a PrintStream for text-writing is very handy:
A BufferedReader for text-reading is the good (best*) option:
Hopefully you can start with networking with this information!
PS: Of course, all networking code have to be try-catched for IOExceptions.
EDIT: I forgot to write why it isn't always the best option. A BufferedReader uses a buffer and read as much as it can into the buffer. But sometimes you don't want that the BufferedReader steals the bytes after the newline and put them into his own buffer.
Short example:
But the BufferedReader has already that byte, you want to read, in his buffer. So calling
in.read()
will return the byte following on the last byte in the buffer of the reader.So, in this situation the best solution is to use
DataInputStream
and manage it your own way to know how long the string will be and read only that number of bytes and convert them into a string. Or: You useThis method doesn't use a buffer and reads byte by byte and checks for a newline. So this method doesn't steal the bytes from the underlying InputStream.
EDIT: You can develop your own protocol in which you can request a method call using Java Reflexion. For example:
Once you have your object you can send it to the other side of the connection with Serialization:
ObjectOuputStreams
andObjectInputStreams
. With these two classes you can write and read objects through a stream.And on the other side of the connection:
(***)
: Check out my question for further information aboutreset()
and when to use it.Glassfish may be more firepower than you need, but I wouldn't give up on leveraging an existing library.
Some options for sockets:
If you decide on going more towards web services than sockets, Jetty is the way to go for small footprint HTTP.
One thing that comes to mind is why do you want to write it in 'simple java' rather than using an existing application server? some EJB3 thing to abstract and manage communication, security, transactional integrity, etc?
If you want to write a simple client server type programme you can follow the advice given in this tutorial.
If you want to do something a bit more involved, in plain Java as you say then you may need to look at RMI.
However, these days, web services are all the rage and you may find that in the long run it is simpler.