Let's start with creating a new Java project.
I'm going to go to the package explorer area, right click -> New -> Java Project. Let's name it "Simple_TCP" for
example. Open that, right click on the source package, New -> Package. "simple_tcp". And then I'm going to do
a right click on that, and create our first class.
Our class is going to be called "Server", and that's actually going to be all the code we're going to need
for our server side of our application.
Now, I'm going to explain every single line of code we use here in a separate lesson, so if you're familiar
with Java you can just skip that, but for now, I'm going to give you just enough information so that you
can understand what we're doing here.
The first thing we're going to do is we're going to create a main method. A main method is an entry point
to your application, and there's actually a shortcut for this.
In Java, you just type in "main", press Ctrl + Space,
press Enter, and there you have it.
Next, let's add a try-catch block. A try-catch block is basically a piece of code that Java will try to
run, and then if it fails it's going to go to the catch code.
So the way you do this - there's also a shortcut for this - you type in "try" and then press Ctrl + Space
and then press Enter. In the try block,
I'm going to call new Server();
Server() is going to be a method - public Server() - where pretty much all of our code is going to be located,
and then if anything goes wrong in this area here, we're going to be thrown to the catch block.
And what I want the catch block to do is - I want to use this "e" object.
That's the exception object.
And I want it to print out what happens.
So I want to know what went wrong.
And that's what I'm going to get by using this method.
All right.
Now let's focus on creating sockets.
On the server side of the application, we're going to have two types of sockets: a server socket and then
a regular socket. The server socket is just going to hold a port in the operating system for our application.
And here it goes: we're going to use ServerSocket, and then we're going to name it server_socket.
server_socket = new ServerSocket
And then we're going to forward the number of our port.
I'm going to use 2020.
Looking into the future here ;) - and I'm going to comment that with "opening a new port". We're getting this
error because we need to import a packet that contains this ServerSocket method.
And that packet is java.net.
But don't worry, there's a shortcut for this too.
You just need to press Ctrl + Shift + O,
and here it is - java.net.ServerSocket.
Now this second error is because we need to throw an exception here
and we're just going to skip this because I'll explain that later, in this explanatory lesson.
I'm just gonna use this to avoid the markups that Java shows when when you have an error like that.
All right. Here I'm going to say "print" - or I'm not gonna do that,
I'm also going to use a shortcut: "syso" + Ctrl + Shift.
And there we have System.out.println.
We're going to print out in the console section, that port 2020 has been opened.
All right.
Now we are ready to open this regular communication socket that I've talked about. We're going to use
the Socket method for it.
So it's going to be called "socket",
and we're going to use our ServerSocket, and then call accept(). accept() is a blocking method that accepts
incoming connections.
And what a "blocking method" means -
it means that Java is going to go through our lines of code, and then it's going to stop at this line,
and it's going to say "OK now I'm waiting for an incoming connection to come up", and it's not going to
continue to go through the other lines until an incoming connection actually appears and it is accepted.
Let's quickly import java.net.socket.
So again, Ctrl + Shift + O should do it.
There it is.
Now I'm going to print out a message saying "Client ", and then I'm going to add this client's IP address.
I'm gonna say socket.getInetAddress(),
"Inet" is short for Internet.
And what this method does - it retrieves the client's IP address from the socket object. So socket
object was created right here, when we got an incoming connection that was accepted.
So naturally, this object contains information such as - IP address of our client.
And then I'm going to say "Client " and then their IP address " has connected."
Now we're going to do something a bit more complex.
So we're going to create input and output buffers.
I'm going to add a comment here just to separate this section.
In this lesson we're just going to go real quick through the code and then I'll explain the details
later, but for now, you just need to know that we need a place to store our data - the data that is coming
out of our socket, and the data that is coming into our socket.
We call that place - a buffer. For the input buffer,
we're going to use the Buffered Reader.
And we'll import to these methods or the packets that contain these methods later.
So, the BufferedReader object is going to be "in_socket", since that's going to read the data that's coming
into our socket -
- from the client. new BufferedReader, and then we're going to forward "new InputStreamReader -
- that's going to read data from the socket.
So we need to .getInputStream().
For our outgoing buffer, we're going to use PrintWriter. We're going to name that out_socket = new PrintWriter,
and then we're going to forward new OutputStreamWriter.
So here we are actually writing the data into the buffer, and then we're going to go socket dot -
socket.getOutputStream()
And here I'm just going to add "true" real quick, and I'll explain what that means
later, I promise. The only thing left to do here is to import the packets containing these methods,
and we're going to get that by pressing
Ctrl + Shift +O
There we go.
That's java.io
java.io
and then all of these methods.
And now we've got our input and output buffers.
Now we can actually move on with the code. From this moment on, we are deciding what kind of communication
we're going to have between our server and our client.
The first thing I'm going to send as a server is going to be "Welcome!" for example.
So I'm going to use my out socket, out_socket.println -
so our client is going to receive this - I'm going to comment this real quick.
All right.
And then when I receive the message back from the client, I'm going to store that in a string object.
I'm going to call that "message", and I'm going to say read from in_socket.
All right.
Now, I'm going to print out this message in the console.
It's going to be
"Client says:" and then add the message from the client.
This is going to be our entire client-server communication for now.
So, after they're done communicating, the only thing left to do is to close the socket.
So that's what you should always do when the communication is over.
So once the application is going to shut down, we're going to do "socket.close();"
I'm going to comment that too, or - it's not going to be a comment but rather a notification in our console.
I'm going to say "Socket is closed."
So the purpose of these notifications - as I called them throughout this lesson - is to know when we have
passed a certain line of code.
So if we are for any reason stuck in this line, we will never get to this next line.
So if I get a printout "Socket is closed" in my console, I'm going to know that I've - that Java not me - that
Java went through this line, and that everything worked correctly, and that now I'm at line twenty nine.
So this is pretty much it for the server side.
The next lesson is going to be about the client, and then I'm going to do an explanatory lesson where
we're going to explain all of these lines of code even further.