Java Chapter -15
Hello guys Hope you all doing well In this article we are going to talk about how to make connections in java. So How it works. Let’s take an simple client server architecture to explain these stuff.
To do this we need to learn three main things,
- Establishing Connection : Initial connection between the client and server(Socket connection is used here)
- Sending message : sending messages to the server
- Receiving message: receiving messages from the server
To connect to another machine, we need a Socket connection. A Socket (java.net.Socket class) is an object that represents a network connection between two machines.
(Wait.. What’s a connection….) A connection is a relationship between two machines. In order to make an relationship the two pieces of software have know about each other. Most importantly, these two software have to know how to communicate with each other. In other words, how to send bits to each other.
(What is a TCP port…..)It’s just a 16-bit number that identifies a specific program on the server. In server there can be more programs but how to identify them separately that’s where TCP Port comes in. For an example web (HTTP) server uses on port 80. If we’ve got a Telnet server, its running on port 23, FTP in port 20, POP3 mail server runs in port 110, SMTP runs in port 25.
A port number is like a unique identifiers. They represent a logical connection to a particular piece of software running on the server. Without port numbers, the server have no way to know which application a client want to connect. And since each application might have its own unique protocol, think of the trouble we’ll have without these identifiers.
Let’s read data from a Socket using a BufferedReader.
This is How it’s looks like,
To write data to a Socket, we use a PrintWriter
Get farook fazni’s stories in your inbox
Join Medium for free to get updates from this writer.
When we write one String at a time, PrintWriter is the standard choice, and we’ll recognize the two key methods in PrintWriter, print() and println() Just like good old System.out.
First we make a Socket connection to the server.(This is same as before)
Socket chatSocket = new Socket(“127.0.0.1”, 5000);
And then we Make a PrintWriter chained to the Socket’s low-level (connection) output stream
PrintWriter writer = new PrintWriter(chatSocket.getOutputStream());
PrintWriter acts like its own bridge between character data and the bytes it gets from the Socket’s low-level output stream. By chaining a PrintWriter to the Socket’s output stream, we can write Strings to the Socket connection.
At Last we print it out
writer.println(“message to send”);
writer.print(“another message”);
Multithreading in Java
As We know normally programs run step by step which means when one task is completed next task is processed. But in java, there is something called Multithreading. Which means Multiple threads run at once. However, java has multiple threads. but only one Thread class.
A thread is a separate call stack. Every java application starts with the main thread. And then that puts the main() method on the stack bottom. and JVM has responsible for starting the main thread.
(What does it mean to have more than one call stack……)With more than one call stack, we can have multiple things happen at the same time. In real scenario, only a true multiprocessor system can do more than one thing at a time, but with the Java threads, we can do several things simultaneously.
That’s all for this article I hope you guys got something. Let’s meet in another article soon until then bye bye. OH wait…. Most of the images in this article are taken from the Head First Java book which is written by Kathy Sierra, Bert Bates. Don’t forget to read the book as well.
Reference: Head First Java, 2nd Edition [Book] (oreilly.com)









