2. Introduction
Manipulating URLs
Reading the Files on
the Server
Establishing Simple
Server Using Stream
Sockets
Datagrams:
Connectionless
Client/Server
Interaction
Topics to be covered
4. •Built – in Networking Facilities – Easy to Program
•Classes and Interfaces declared in java.net package
•Packages for Stream Based Communications and Packet
based Communication.
•Focus on C/S relationship – client requests and server
responds
•Socket Based Communications – Read from a Socket and
Write to a socket.
Introduction
5. Introduction – Stream Sockets
•Data flows between process in continuous streams
•Connection Oriented Service
•Protocol used – TCP (Transmission Control Protocol)
6. Introduction – Datagram Sockets
•Individual Packets of Information are transmitted.
•Connection Less Service – does not guarantee that
packets arrive in any particular order (May be lost or
duplicated).
•Protocol used – UDP (User Datagram Protocol)
8. Manipulating URLs
•HTTP – Hyper Text Transfer Protocol – Forms the basis of
the web
•URI – Uniform Resource Identifiers – Identify data on the
internet
•URL – Uniform Resource Locator – Locations of websites
and webpages
10. Manipulating URLs - Example
•<html> <body>
• <applet>
• <param name = "title0" value = "Java Home Page">
• <param name = "location0" value = “www.oracle.com/technetwork/java/">
• <param name = "title1" value = "Deitel">
• <param name = "location1" value = "http://www.deitel.com/">
• <param name = "title2" value = "JGuru">
• <param name = "location2" value = "http://www.jGuru.com/">
• <param name = "title3" value = "JavaWorld">
• <param name = "location3" value = "http://www.javaworld.com/">
• </applet>
•</body></html>
11. Manipulating URLs - Example
public class SiteSelector extends Japplet
{
public void init()
{ title = getParameter( "title0“ );
location = getParameter( "location0“);
try
{ URL url = new URL( location ); // convert location to URL
AppletContext browser = getAppletContext();
browser.showDocument(url);
} // end try
catch ( MalformedURLException urlException )
{ urlException.printStackTrace();
} // end catch
13. Reading a File on a Server
Swing GUI component JEditorPane - to display the
contents of a file on a webserver.
HyperLinkEvent – Event Raised when a user clicks a
Hyperlink
14. Reading a File on a Server
public class ReadServerFile extends JFrame
{
JEditorPane contentsArea;
public ReadServerFile()
{
contentsArea = new (); // create
contentsArea
contentsArea.setEditable( false );
contentsArea. (
new HyperlinkListener()
{
15. Reading a File on a Server
public void ( HyperlinkEvent event )
{
if ( event.getEventType() ==
)
{
location = .toString();
contentsArea. ( location ); ;
}
} // end method hyperlinkUpdate
} // end inner class
); // end call to addHyperlinkListener
}
}
17. Establishing Simple Server Using
Stream Sockets
Step 1 : Create a ServerSocket
Step 2 : Wait for a Connection
Step 3 : Get the Socket’s I/O Streams
Step 4 : Perform the Processing
Step 5 : Close the Connection
18. STEP – 1 : Create a Server Socket
Call the ServerSocket constructor
ServerSocket server =
new ServerSocket(portnum,quelength)
Portnum Registers available TCP port number
Quelength Max. Number of Clients that can wait
This constructor establishes the port
where the server waits for connection
from clients
19. STEP – 2 : Wait for a Connection
Program calls the ServerSocket method accept which returns a
Socket object
Socket connection = server.accept();
Socket object
Connection with the client.
Allows the server to interact with the client.
20. STEP – 3 : Get the Sockets I/O Stream
Get the I/P and O/P stream for Communications.
Server sends information to client via OutputStream
calls the method getOutputStream to get the reference
Server receives information from the client via InputStream
calls the method getInputStream to get the reference
Methods write and read of Stream objects
send / receive individual /sequence of
bytes.
21. STEP – 3 : Get the Sockets I/O Stream
ObjectInputStream input =
new ObjectInputStream(connection.getInputStream());
ObjectOutputStream output =
new ObjectOutputStream(connection.getOutputStream());
22. STEP – 4 : Perform the Processing
Client and Server communicates via
InputStream and OutputStream
23. STEP – 5 : Close the Connection
Server closes the connection via close method
24. Establishing Simple Client Using
Stream Sockets
Step 1 : Create a Socket to connect to the Server
Step 2 : Get the Sockets I/O Streams
Step 3 : Perform the Processing
Step 4 : Close the Connection
25. STEP – 1 : Create a Server Socket
Create a Socket to connect to the server
IOException – raised, If the connection fails
UnknownHostException – raised, when the system unable
to resolve the server name
26. STEP – 2 : Get the Sockets I/O
Streams
Get the references of InputStream and OutputStream
Using Socket methods getInputStream and
getOutputStream
27. STEP – 3 : Perform the processing
Client and Server communicates via the InputStream and
OutputStream.
28. STEP – 4 : Close the Connection
Closes the connection when the transmission is complete
by invoking the close method.
29. Client / Server Interaction with Stream
socket connections -
java.io.*;
java.net.*;
javax.swing.*;
public class Server extends JFrame
{
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
public Server()
{
server = new ServerSocket( 12345, 100 );
connection = server.accept();
30. Client / Server Interaction with Stream
socket connections -
output = new ObjectOutputStream( connection.getOutputStream() );
input = new ObjectInputStream( connection.getInputStream() );
String message = "Connection successful";
output.writeObject( "SERVER>>> " + message );
output.flush();
output.close();
input.close();
connection.close();
}
}
31. Client / Server Interaction with Stream
socket connections -
public class Client extends Jframe
{
JTextField txtField;
ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private Socket client;
public Client( )
{
client = new Socket( InetAddress.getByName( “Node-23” ), 12345 );
client = new Socket( InetAddres.getLocalHost(), 12345 );
output = new ObjectOutputStream( client.getOutputStream() );
input = new ObjectInputStream( client.getInputStream() );
message = ( String ) input.readObject();
txtField.setText(message);
output.close(); // close output stream
input.close(); // close input stream
client.close();
34. Datagrams : Connectionless Client/Server
Interaction
Connectionless Transmission
- like a mail is carried via the postal service.
- Message break into separate pieces and
sequentially numbered.
- Letters may arrive in order or not.
-Receiver reassembles the pieces into sequential
order
Protocol - UDP
35. Datagrams : Connectionless Client/Server
Interaction – SERVER class
DatagramPackets : Packets used to send and receive
information.
DatagramSocket : Sends and Receives the packets.
SocketException : if the DatagramSocket constructor fails
to bind to the port
36. Datagrams : Connectionless Client/Server
Interaction – SERVER class
import java.net.*;
import java.io.*;
import javax.swing.*;
public class Server extends JFrame
{ JTextArea txtarea;
public Server()
{
Txtarea = new JTextArea(10,40);
socket = new DatagramSocket( 5000 );
byte[] data = new byte[ 100 ]; // set up packet
DatagramPacket receivePacket =
new DatagramPacket( data, data.length );