Download presentation
Presentation is loading. Please wait.
Published byPriscilla Howard Modified over 8 years ago
1
Web Design & Development 1 Lec - 21 Umair Javed
2
Web Design & Development 2 Socket Programming
3
Umair Javed©2005 JDBC3 Request – Response Model animation
4
Umair Javed©2005 JDBC4 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
5
Umair Javed©2005 JDBC5 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.
6
Umair Javed©2005 JDBC6 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.
7
Umair Javed©2005 JDBC7 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 What is a Port? Examples FTP: 21 HTTP: 80 TELNET: 23
8
Umair Javed©2005 JDBC8 Request – Response Model host:port Clients make “calls” to that port # Server listens on a port # host:port
9
Umair Javed©2005 JDBC9 How Client & Server communicate animation
10
Umair Javed©2005 JDBC10 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
11
Umair Javed©2005 JDBC11 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);
12
Umair Javed©2005 JDBC12 Steps – To Make a Simple Client 3.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); Show code here FileReader fr = new FileRes.. BufferedReader br = new Bu..
13
Umair Javed©2005 JDBC13 animation
14
Umair Javed©2005 JDBC14 Steps – To Make a Simple Client 4.Send / Receive Message pw.println(“hello world”); String recMsg = br.readLine(); 5.Close Socket s.close();
15
Umair Javed©2005 JDBC15 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
16
Umair Javed©2005 JDBC16 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);
17
Umair Javed©2005 JDBC17 Steps – To Make a Simple Server 3.Wait for Incoming Connection –When connection established, communication socket is returned Socket s = ss.accept();
18
Umair Javed©2005 JDBC18 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);
19
Umair Javed©2005 JDBC19 Steps – To Make a Simple Server 5.Send / Receive Message pw.println(“hello world”); String recMsg = br.readLine(); 6.Close Socket s.close();
20
Umair Javed©2005 JDBC20 aniamtion
21
Web Design & Development 21 Example Code Echo Server
22
Web Design & Development 22 Example Code Echo Client
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.