IMPLEMENTING A SERVER
Submitted To:-
Ms. Annu Dhankar
Submitted By:-
Chuni Lal Kukreja
Connect With The Outside
World
Java program can reach out & touch a
program on another machine.
All low level networking details are
taken care of by classes in the java.net
package.
Big benefit of java is sending & receiving
data over a n/w is just I/O.
What We Have To Do ?
We have to make
Client Socket
Server Socket
Client & Server.
And make them communicate with
each other.
Overview
Client has to know about the
server.
Server has to know about it’s
client's.
Client A
Server
Client B
3 Things To Learn
How to establish the initial
connection b/w client & server.
How to send message b/w client &
server.
How to receive message from & to
client & server.
Make a Network Socket
Connection for Client
2 things are must to know about the server
-> Who it is (URL=“127.0.0.1”)
-> What port it’s running on (Port=5000)
To connect to another m/c, we need a socket
connection.
Socket sock = new Socket(“127.0.0.1”,5000);
To Read Data From A Socket
We use streams once socket connection is
made from client to server.
Client Server
I/O streams to & from the socket connection
We use BufferedReader same as we did in case
of file , it doesn’t make difference in that
underlying connection stream.
contd….
Make Socket connection to the server.
Make an InputStreamReader chained to the socket’s
low level input stream.
InputStreamReader stream= new
InputStreamReader(sock.getInputStream());
This input stream works like a bridge.
Make a BufferedReader and read !
BufferedReader reader= new
BufferedReader(stream);
Chain the BufferedReader to the InputStreamReader
which was chained to the low level connection we get from the socket.
String s=reader.readLine();
Close the open stream
reader.close();
Converted to bytes
characters from server
chained to chained to
Buffered
Client characters 011011011
Characters
destination InputStreamReader
Data on the Server
source
Program to Receive Message
From Server
import java.io.*;
import java.net.*;
public class chat {
public void go(){
try{
Socket sock=new Socket(“190.10.10.20”,5000);
InputStreamReader stream=new InputStreamReader(sock.getInputStream());
BufferedReader reader=new BufferedReader(stream);
String s=reader.readLine();
reader.close();
}}
catch(IOException ex){ ex.printStackTrace();}
}
public static void main(String[] clk)
{chat c=new chat();
c.go();
}}
To Write Data To A Socket
Make Socket Connection to the server.
Make a PrintWriter chained to the
Socket’s low level output stream.
PrintWriter writer=new
PrintWriter(sock.getOutputStream());
Acts as a bridge, it chains with Here socket gives us the low level
socket o/p stream, we can write conn stream & we chain it to the
Strings to socket connection PrintWriter, by giving it to its
constructor
contd….
Write (print) something
writer.println(“msg to send”);
It adds a new line at the end of what it sends
Close the open Stream
writer.close();
source
characters bytes to server
Chained
to
Client “msg to send” 101101110 Server
PrintWriter destination
Making Server To Interact
With Client
Server application makes a
ServerSocket , on a specific port.
ServerSocket serversock= new
ServerSocket(5000);
This starts the server app listening for client requests coming
in for port 4242
4242
Client Server
Socket ServerSocket contd….
Client makes a Socket Connection
Socket s= new Socket(“190.10.10.20”,5000);
Client Server
5000
Server makes a new socket to
communicate with the client.
Socket sock= serversock.accept();
Accept() method blocks while its waiting for client Socket Connection.
When a client tries to connect the method returns a Socket (on
different port). So by this ServerSocket wait for other client request.
Client & Server Interaction
5000Server socket
Socket (waiting for next client)
Client Server
2589
Program to Send Message to
Client
import java.io.*;
import java.net.*;
public class chat {
public void go(){
try{
ServerSocket serversock=new ServerSocket(5000);
while(true){
Socket sock=serversock.accept();
PrintWriter writer=new PrintWriter(sock.getOutputStream());
writer.println(“hello”);
writer.close();
}}
catch(IOException ex){ ex.printStackTrace();}}
public static void main(String[] clk)
{chat c=new chat();
c.go();
}}