IMPLEMENTING A SERVER-new

Views:
 
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

IMPLEMENTING A SERVER : 

IMPLEMENTING A SERVER Submitted To:- Ms. Annu Dhankar Submitted By:- Chuni Lal Kukreja

Connect With The Outside World : 

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 ? : 

What We Have To Do ? We have to make Client Socket Server Socket Client & Server. And make them communicate with each other.

Overview : 

Overview Client has to know about the server. Server has to know about it’s client's. Client A Client B Server

3 Things To Learn : 

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 : 

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 : 

To Read Data From A Socket We use streams once socket connection is made from client to server. We use BufferedReader same as we did in case of file , it doesn’t make difference in that underlying connection stream. contd…. Client Server I/O streams to & from the socket connection

Slide 8: 

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.

Slide 9: 

Make a BufferedReader and read ! BufferedReader reader= new BufferedReader(stream); String s=reader.readLine(); Close the open stream reader.close(); Chain the BufferedReader to the InputStreamReader which was chained to the low level connection we get from the socket.

Slide 10: 

Client Buffered Characters characters 011011011 InputStreamReader chained to Converted to characters bytes from server chained to Data on the Server source destination

Program to Receive Message From Server : 

Program to Receive Message From Server

Slide 12: 

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 : 

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 socket o/p stream, we can write Strings to socket connection Here socket gives us the low level conn stream & we chain it to the PrintWriter, by giving it to its constructor contd….

Slide 14: 

Write (print) something writer.println(“msg to send”); Close the open Stream writer.close(); It adds a new line at the end of what it sends

Slide 15: 

Client “msg to send” characters PrintWriter 101101110 bytes to server Server Chained to source destination

Making Server To Interact With Client : 

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 5000 Client Server Socket ServerSocket 5000 contd….

Slide 17: 

Client makes a Socket Connection Socket s= new Socket(“190.10.10.20”,5000); Server makes a new socket to communicate with the client. Socket sock= serversock.accept(); Client Server 5000

Client & Server Interaction : 

Client & Server Interaction Client Server Socket 5000 2589 Server socket (waiting for next client)

Program to Send Message to Client : 

Program to Send Message to Client

Slide 20: 

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(); }}

Slide 21: 

THANK YOU

Slide Show Tips : 

Slide Show Tips To present in true widescreen, you’ll need a computer and, optionally, a projector or flat panel that can output widescreen resolutions. Common computer widescreen resolutions are 1280 x 800 and 1440 x 900. (These are 16:10 aspect ratio, but will work well with 16:9 projectors and screens.) Standard high definition televisions resolutions are1280 x 720 and 1920 x 1080. Use the Test Pattern on the next slide to verify your slide show settings.

Slide 23: 

Widescreen Test Pattern (16:9) Aspect Ratio Test (Should appear circular) 16x9 4x3