JAVA Beginners part 18

Client and Server Model
– how to establish connection to the server
– how to send messages to the server
– how to receive messages from the server

Socket chatSocket = new Socket(“”,5000); address of the sever
5000 is the port number
server / TCP port numbers

a server can take up-to 65536 different server apps running one per port
TCP port numbers from 0 to 1023 are reserved for well known services.
Dont use them for your own programs.

if you are writing services (server programs) to run on a company network, you should check with the sys admins to find out which ports are already taken

How do you know the port number of the server program you want to talk to ?

That depends on whether the program is one of the well-known services.If you’re trying to connect to a well-known service, like the ones

someone writes a network service and wants others to write clients for it , they’ll publish the lP address.

can two applications on the same server have the same port number?
No – If you try to bind a program to a port that is already in use,you’ll get a BindException.

Socket chatsocket = new Socket(“”,5000);
InputStreamReader stream = new InputStreamReader(chatsocket.getInputStream());
BufferedReader reader = new BufferedReader(stream);
String message = reader.readLine();

InputStreamReader is a bridge between a low level byte stream between socket and high level stream like buffered reader


Using printer writer

Socket chatsocket = new Socket(“”,5000);
PrintWriter writer = new PrintWriter(chatsocket.getOutputStream());

//using buffered reader is a two step approach but print writer is a single.

writer.println(“message to send”);
writer.print(“another message”):


ServerSocket serversock = new ServerSocket(4242);
//creates socket on the server which then starts listening to the client

Socket sock = new Socket(“”,4242);
//make connection the socket created

Socket sock = serversock.accept();
//accept method waits for a client socket connection. when client tries to connect
the method returns a plain old socket.

the socket is on different port than the server socket.such that the server port can
go back to waiting for other clients

A Socket represents a connection between two applications which may (or may not) be running on two different physical machines.

Multi threading In Java

A thread is a separate call stack. Thread is a class , and when we say thread means a thread of execution like a separate call stack.By Default JVM starts with main thread.

What is the use of multiple call stack ? we can have multiple things happening at the same time.Execution can move back and forth between multiple stacks so rapidly that you will feel as though all the stacks are executing at the same time.


How to launch a new thread. Implement the Runnable Interface. In general which ever class you want to create a thread , let that implement the Interface. In that class you will define what the thread will do.

Runnable threadjob = new MyRunnable();

Thread mythread = new Thread(threadjob);
//pass the Runnable object to the thread object. what this will do is that it tells the thread
object which method to put on the bottom of the new stack. The Runnables run() method.


A thread object needs a job. The job is actually the first method that runs when the thread
is started, which is run() method.

public void run()


3 states for a thread

a thread may go to blocked state or sleep state because it tried to call an object that is locked

Thread Scheduler moves the jobs from various states. You cannot control the scheduler
One of the techniques to control the flow of threads is by using sleep, when you say sleep to a thread it cannot be running.

Using Thread Sleep is a way to control the threads.When a thread wakes up it is once again at the mercy of the thread scheduler.

You can give names to the thread. let see how.


thread clash


Use the synchronized keyword to modify a method so that only one thread at a time can access it.You lock the method that does the banking transaction .

In code, though, it’s quite simple-just add the synchronized modifier to your method declaration.

The locks are not per method, they are per object.There’s no API in java for accessing object locks, it is handled by the JVM. Every Java object has a lock. A lock has only one key.

Synchronizing the methods will help to resolve the lost update problem.When code hits a synchronized method, there’s going to be a performance hit. A good rule of thumb is to synchronize only the bare minimum that should be synchronized.



next page 539

More in next part.


Head First Java 2nd Edition

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s