Networking - Java Sockets and Server-Client Communication
Hello it's a me drifter1. Today we continue with another part of our Networking series, where we will talk about Java Sockets and Server-Client Communication. It would be helpful if you have watched the C Linux part, but its not necessary! So, let's get started!
In Java everything we use is in libraries. If you remember from Datastructures in C we had to write all the Code, but in Java not, and that makes our life easier, cause we simply use a already made Object! When talking about Networking tho C in Linux had also many libraries. This doesn't change in Java too! We will use java.io, because the Sending and Receiving work like Writing and Reading from the Socket (the same way it did in C Linux too!) and java.net for the socket stuff. You can read about this library here and we will get into some stuff to create simple server-client communication programms!
The main functions for a Socket (java.net) that we will use are:
- ServerSocket() -> that returns a ServerSocket and depending on the arguments can be already binded to an port or even address, but doesn't specify a protocol (we can also use a DatagramSocket for making a UDP Connection!)
- bind() -> called from a ServerSocket object and is for binding to an port or even setting up a backlog for maximum number of clients in queue
- accept() -> called from a ServerSocket and accepting an incoming connection
- close() -> called from a ServerSocket and closing the socket
For communicating with "Files" (java.io) we will use:
- PrintWriter -> that first will bind to the ServerSocket OutputStream using getOutputStream() and then simply sends using the PrintWriter Object's functions for printing like println()
- BufferedReader -> that first will bind to the ServerSocket InputStream using getInputStream() and then simply reads using the BufferedReader Object's functions for reading like readLine()
And this is actually it! Off course there are many others that you can check out in the java.net documentation, but these are the main things that we will need for the purpose of demonstration in this post!
The first Example I have in mind is an Echo Service. The client will send something to the server and the server will send it back as is. This is pretty easy to Code and looks like this:
You can see that the client simply sends to the localhost the same way we did in C Linux, cause the server we set up runs on our local machine. You can also see that the bind() function was not needed, cause it's already done when creating the socket using Serversocket(). Something that you also might guessed is that this Server-Client program is UDP-like and doesn't create a real connection (we don't have any accept!) And yeah you're right, cause here we didn't specified a protocol, but it all depended on how we used the socket it! Off course we could create a DatagramSocket in this example, but here we simply make the Server wait for input and send it back when he receives something!
Let's now try creating our own protocol! We will create a KnockKnock Service, where the client sends answer to questions the server gave him. But, depending on the answer the server will also send a different reply. The client simply has to send and receive strings, but the server will use a KnockKnockProtocol to check what he needs to send depending on the clients answer! The protocol will be a simply object that has a function that creates strings to send depending on what was send and what the reply was.
And this is actually it!
I hope you enjoyed this series so far, cause this is actually all I had ready for now! :P
Off course we will get into more later on, but I have to set it up first...So, next time we will now return to our main category of Programming.
Until next time...Bye!