JDBC - Vutube.edu.pk

advertisement
Web
Design & Development
Lec - 21
Umair Javed
1
Web
Design & Development
Socket Programming
2
Request – Response Model
• animation
3
JDBC
Umair Javed©2005
Socket
• A socket is one endpoint of a two-way
communication link between two programs
running on the network.
• A socket is a bi-directional communication
channel between hosts (a computer on a
network can be termed as a host).
• A socket is bound to a port number
4
JDBC
Umair Javed©2005
Socket’s Dynamics
• A Socket is an abstraction of the network similar
to the way a file is an abstraction of your hard
drive.
 You store and retrieve data through files from
hard drive without knowing the actual dynamics
of the hard drive.
 Similarly you send and receive data to and from
network through socket without actually going
into underlying mechanics.
5
JDBC
Umair Javed©2005
Sending/Receving Messages
using Socket
• You read from or write data to a file, using
streams.
• Similarly to read from or write data to a socket,
you use streams.
6
JDBC
Umair Javed©2005
What is a Port?
• Transport address to which processes can
listen for connection requests
• Local to host: 64K TCP & 64K UDP ports
• Well-known ports
– Below 1024
– Standard services
– Only supervisor privileged
enough to access
7
JDBC
Examples
 FTP: 21
 HTTP: 80
 TELNET: 23
Umair Javed©2005
Request – Response Model
host:port
host:port
host:port
Clients make “calls”
to that port #
8
JDBC
Server listens
on a port #
host:port
Umair Javed©2005
How Client & Server communicate
• animation
9
JDBC
Umair Javed©2005
Steps – To Make a Simple Client
1. Import required Package
2. Connect / Open a Socket with Server
3. Get I/O Streams of Socket
4. Send / Receive Message
5. Close Socket
10
JDBC
Umair Javed©2005
Steps – To Make a Simple Client
1. Import required Package
import java.net.*;
import java.io.*;
2. Connect / Open a Socket with Server
Socket s = new Socket(“sAdd”, sPort);
11
JDBC
Umair Javed©2005
Steps – To Make a Simple Client
Show code here
3. Get I/O Streams of Socket
FileReader fr = new
BufferedReader br =
InputStream is = s.getInputStream();
InputStreamReader isr= new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
12
JDBC
Umair Javed©2005
animation
13
JDBC
Umair Javed©2005
Steps – To Make a Simple Client
4. Send / Receive Message
pw.println(“hello world”);
String recMsg = br.readLine();
5. Close Socket
s.close();
14
JDBC
Umair Javed©2005
Steps – To Make a Simple Server
1. Import required Package
2. Create a Server Socket
3. Wait for Incoming Connections
4. Get I/O Streams of communication Socket
5. Send / Receive Message
6. Close Socket
15
JDBC
Umair Javed©2005
Steps – To Make a Simple Server
1. Import required Package
import java.net.*;
import java.io.*;
2. Create a Server Socket
ServerSocket ss = new ServerSocket(portNo);
16
JDBC
Umair Javed©2005
Steps – To Make a Simple Server
3. Wait for Incoming Connection
–
When connection established, communication socket is returned
Socket s = ss.accept();
17
JDBC
Umair Javed©2005
Steps – To Make a Simple Server
4. Get I/O Streams of Socket
InputStream is = s.getInputStream();
InputStreamReader isr= new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
18
JDBC
Umair Javed©2005
Steps – To Make a Simple Server
5. Send / Receive Message
pw.println(“hello world”);
String recMsg = br.readLine();
6. Close Socket
s.close();
19
JDBC
Umair Javed©2005
aniamtion
20
JDBC
Umair Javed©2005
Web
Design & Development
Example Code
Echo Server
21
Web
Design & Development
Example Code
Echo Client
22
Download