In this lesson we're going to work on the client side.
Let's create our class by clicking on simple_tcp -> New -> Class.
I'm going to name it simply "Client". Let's start with adding our main method, the same way we did it for
the server.
So I'm going to type in main, press Ctrl + Space, and then Enter, and then I'm going to add the try-catch
block - I'm going to type in "try" and then press Ctrl + Space, and then Enter. And then we're going to initiate
new Client() in the try block. And then in the catch block I'm going to say e.PrintStackTrace(). We
can create our client method
now. It's going to be public Client() throws Exception
- and in this method is going to be our application.
So this is going to be our client side of our program.
And the first thing we need to do here is to create a socket.
This time we only have one type of socket.
On the server side, we had two types of sockets: we had the server socket and then the regular communication
socket.
So on the client side we're only going to have a regular socket.
This socket is going to be - we're going to use the Socket() method again.
So it's going to be Socket socket = new Socket(); and then we're going to forward two arguments to this
method.
It's going to be the IP address of the server and the listening port
- so the port number where our server is expecting our connection. I'm going to use localhost, since my
server is on this machine. And I'm going to use 2020, which we defined as our port number.
Additionally, I'm going to press Ctrl + Shift + O, to import the java.net.socket packet, so
that we don't have that error anymore. And I'm going to print out a message in the console, saying
"Successful connection to the server". This printout right here is useful to us because if something fails
in this line we're going to be thrown to the catch block.
So, by reaching this line of code - by seeing this sentence in our console - we know that we have successfully
passed this line of code.
So, this really reassures us that everything went well and that we actually are connected to our server.
Next thing we need to do here is create the input and output buffers.
Now luckily for us, they're exactly the same as on the server side.
So you can just copy them, but I'm going to type them out real quick. For the incoming data,
we're going to need BufferedReader.
We're going to name that in_socket = new BufferedReader (new InputStreamReader - and then socket.getInput
- socket.getInputStream -
and then for the outgoing socket, it's going to be PrintWriter -
PrintWriter = out_socket (new PrintWriter...
There it is.
I'm going to add a comment here just to separate this section,
and press Ctrl + Shift + O to import all the all the required packets. All right.
These are the basic requirements for our client-server communication.
And now what we need to worry about is the protocol.
A protocol is pretty much just a set of rules by which communication between two or more sides occurs.
So we need to define who is the first to send a message, what kind of message, what kind of responses
are expected, etc etc.
For our simple little program we're going to have a simple little protocol.
So the client is going to connect to the server, the server is going to say "Welcome!", the client is then
going to say "Thanks!", and then they're going to disconnect.
We have already programmed the server to do all of these things.
So it's going to be listening for connections,
and then once the connection arrives, it's going to accept the connection, and then right away it's going
to send out "Welcome!"
It's going to then receive the message from the client.
It's going to display it in its console, and then it's going to close the socket.
So socket.close() is actually the line where we initiate disconnection or - interruption of the connection.
All right.
Let's go back to our client.
So this is where the protocol actually takes place. First,
we need to receive the message from the server. We're going to store that message in a string called
"message" - very original.
We're going to read that message from our socket, with "readLine()", and then we can display that message
in our console.
We're going to say "Server says:" and then add the message.
Now we need to send "Thank you" back to the server.
We're going to do that by using our out_socket object and println,
and we're going to say "Thanks!"
That's actually pretty much it.
So according to our protocol, we need to connect to the server, receive the "Welcome!" message, send a
"Thanks!" message, and then disconnect.
So we're going to disconnect by closing the socket: socket .close(), and we're going to
print out a message in our console, saying "Socket closed". Now I'm going to save this and try and run it
and see if it works.
First, we're going to run the server, so you run the server by pressing - or choosing - the server class, and
then pressing "Play" or "Run" in Eclipse.
And then I'm going to choose the client class, and also press Run.
So the program is going to just go through all of the code by itself, since we've already typed in all the
messages that need to be sent.
But let's just quickly go over what happened.
In order to switch between console messages for server and client, you need to press this little icon
of a display here.
So right now we're looking at the server console, and we can see all of our messages.
So ports 2020 is open.
We got that right here.
So we get that message
once we successfully reserve a new port in our operating system, and then when a client connects, we get
this message.
This is the IP address of our client - 127.0.0.1 - that's the loopback address,
and then we get "Client says: Thanks!"
So we have read the client's message.
But before that, we sent "Welcome!" to the client.
So this actually happened before this.
All right.
As soon as these messages were exchanged, we've closed the sockets on both sides.
So that's when we got "Socket is closed."
message, both on the server and on the client.