Beruflich Dokumente
Kultur Dokumente
Chapter 4
Network Programming
• Introduction
• Java Networking
• Manipulating URLs
• Java Socket Programming
• InetAddress
• Datagram Sockets and
Datagram Packet
Java provides:-
Stream-based communications
a process establishes a connection to another process. While the
connection is in place, data flows between the processes
Connection-based protocol - Uses TCP
Packet-based communications – use UDP
Individual packets transmitted
Client-server relationship
Client requests some action to be performed
Server performs the action and responds to client
Request-response model
Common implementation: Web browsers and Web servers
2
Java Networking is a concept of connecting two or more computing devices
computing devices.
3
Table 1:Java networking classes and interfaces
4
• The HTTP protocol uses URIs to locate data on the Internet.
• A URI that represents a document is called a URL
• An object of the java.net.URL class represents a URL.
• The URL class
• Has constructors that constructs a URL object:
• By taking an absolute URL in string form
• By taking the URL’s component parts:- protocol, host name …
• By taking a base and relative URL
• constructors throw a MalformedURLException if
• The protocol is an unsupported protocol or
• The URL is syntactically incorrect.
• Has methods to
• Parse the different parts of a URL
• Gets an input stream from a URL, so you can read data from server
• Get content from a server as a java object.
Example
5
The openStream( ) method
connects to the resource referenced by the URL,
and returns an InputStream from which data can be read.
The data you get from this InputStream is the raw contents of the file the URL
references:
The openConnection( ) method
opens a socket to the specified URL
6
A socket, in network terminology, is an end-point for communication
between two processes on the network.
Java Socket programming is used for communication between the
The Socket class allows you to create a socket that performs the following
operations:
Connect to a remote machine
Send data
Receive data
Close a connection
It’s constructors
specify the host and the port number you want to connect to.
Hosts may be specified as an InetAddress or a String.
returns an input stream that can read data from the socket into a program.
returns a raw OutputStream for writing data from your application to the other
10
Listens on well-known port for incoming connections
Creates a dynamically allocated port for each newly established connection
Provides a Socket connected to the new port
Maintains a queue to ensure that prospective clients are not lost
Construction:
ServerSocket(int port, int backlog)
Allows up to backlog requests to queue waiting for the server to deal
with them
Some useful methods:
Socket accept()
Blocks waiting for a client to attempt to establish a connection
void close()
Called by the server when it is shutting down to ensure that any
11 resources are deallocated
Five steps to create a simple server in Java
Step 1: Create ServerSocket object
ServerSocket server = new ServerSocket( port, queueLen);
Register an available port
Specify a maximum number of clients
Handshake point
Binding the server to the port
Step 2: Server listens for client connection
Socket connection = server.accept();
Server blocks until client connects
accept( ) returns a Socket object that performs the actual
communication with the client.
Step 3: get the input and output streams that communicate with the
client.
Use connection.getInputStream(); to get the input stream.
Use connection.getOutputStream(); to get the output stream.
12
Step 4: Process phase
Server and Client communicate via streams
ExampleServer
13
Four steps to create a simple client in Java
Step 1: Create a Socket to connect to server
Socket connection = new Socket(serverAddress, port);
Step 2: Obtain Socket’s InputStream and Outputstream
Step 3: Process information communicated
Step 4: Close streams and connection
ExampleClient
14
Connectionless transmission with datagrams
No connection maintained with other computer
Break message into separate pieces and send as packets
Datagram packet
used as short lived envelopes for datagram messages
Used to assemble messages before they are dispatched onto the network,
or dismantle messages after they have been received
The DatagramPacket class
represents a datagram packet.
Has the following attributes:
Destination/source address
Destination/source port number
Data bytes constituting the message
15 Length of message data bytes
Java DatagramSocket and DatagramPacket classes are used for
connection-less socket programming.
DatagramSocket class
16
Java DatagramPacket class
17
ExampleSender ExampleReceiver