1 End-to-End Protocols User Datagram Protocol (UDP) Transmission Control Protocol(TCP)
JAVA Networking - WordPress.comNetworking Basics • Computers running on the Internet communicate...
Transcript of JAVA Networking - WordPress.comNetworking Basics • Computers running on the Internet communicate...
CSC 308 2.0
System Development with Java
Budditha Hettige
Department of Statistics and Computer Science
JAVA Networking
Networking Basics
• Computers running on the Internet communicate to
each other using either
– Transmission Control Protocol (TCP)
– User Datagram Protocol (UDP)
• Java programs that communicate over the network,
you are programming at the application layer
• use the classes in the java.net package
2 Budditha Hettige
TCP (Transmission Control
Protocol)
• is a connection-based protocol that provides a
reliable flow of data between two computers
• provides a point-to-point channel for applications that
require reliable communications
– Hypertext Transfer Protocol (HTTP)
– File Transfer Protocol (FTP)
– Telnet
• To communicate over the network (Through java.net)
– URL
– URLConnection
– Socket
– ServerSocket
3 Budditha Hettige
UDP (User Datagram Protocol)
• is a protocol that sends independent packets of data,
called datagrams, from one computer to another
• UDP is not connection-based
• Many firewalls and routers have been configured not
to allow UDP packets
• The TCP and UDP protocols use ports to map
incoming data to a particular process running on a
computer
• To communicate over the network (Through java.net)
– DatagramPacket
– DatagramSocket
– MulticastSocket
4 Budditha Hettige
URL- Universal Resource Locator • A string that describes how to find a resource on the Internet
• Is a specific character string that constitutes a reference to an
Internet resource.
• Syntax
– scheme://domain:port/path?query_string#fragment_id
– http://vnc.example.com:5800
• URL class
5 Budditha Hettige
Create a URL object
Using URL Class
URL myURL = new URL("http://example.com/");
Sample Code try {
// With components.
URL url = new URL("http", "hostname", 80, "index.html");
// With a single string.
url = new URL("http://hostname:80/index.html");
} catch (MalformedURLException e) {
}
6 Budditha Hettige
Parsing a URL
can use these getXXX methods to get information about the
URL
Sample Code
try {
URL url = new URL("http://hostname:80/index.html#_top_");
String protocol = url.getProtocol(); // http
String host = url.getHost(); // hostname
int port = url.getPort(); // 80
String file = url.getFile(); // index.html
String ref = url.getRef(); // _top_
} catch (MalformedURLException e) {
}
7 Budditha Hettige
Reading Directly from a URL
Sample Code
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.dscs.sjp.ac.lk/");
BufferedReader in = new BufferedReader(
new InputStreamReader(
oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
8 Budditha Hettige
9
Network
a client, a server, and network
Client Server
Client machine Server machine
Elements of Client-Server Computing
Budditha Hettige
Client-Server Paradigm
• Server waits for client to request
a connection.
• Client contacts server to
establish a connection.
• Client sends request.
• Server sends reply.
• Client and/or server terminate
connection.
Budditha Hettige 10
Sockets
• A socket is one end-point of a two-way
communication link between two programs
running on the network
• Represent the connection between a client
program and a server program
• The java.net package
– Socket
– ServerSocket
11 Budditha Hettige
13
Socket Connection
server
Client
Connection request po
rt
server
Client
Connection
po
rt
port po
rt
a client making a connection request to the server
A session established with temporary ports used for two way communication.
Budditha Hettige
More on Computer ports
• The port numbers in the range from 0 to 1023 are the well-known ports – Ports 20/21 File Transfer Protocol
– Port 23 Telnet
– Port 25 Simple Mail Transport Proto.
– Port 79 Finger
– Port 80 HTTP
– Port 110 POP3 (Post Office Protocol)
– All well known ports in the range 1..1023
• Registered ports are those from 1024 through 49151
• Dynamic, private or ephemeral ports
– The range 49152–65535 - above the registered ports
Budditha Hettige 14
Socket and ServerSocket
• Establish a connection with peer process.
• Socket provides an IO stream associated with the
connection.
– all Java IO methods/Objects can be used.
• New exceptions to deal with.
• Great it what you really need is to communicate
• using nothing more than a stream.
• Other endpoint could be written in any language!
17 Budditha Hettige
Example
Resolving a Hostname
try {
InetAddress addr = InetAddress.getByName(
"java.sun.com");
} catch (UnknownHostException e) {
}
18 Budditha Hettige
Example
Creating a Client Socket
try {
InetAddress addr = InetAddress.getByName(
"java.sun.com");
int port = 80;
Socket sock = new Socket(addr, port);
} catch (IOException e) {
}
19 Budditha Hettige
Example
Creating a Server Socket
try {
int port = 2000;
ServerSocket srv = new ServerSocket(port);
// Wait for connection from client.
Socket socket = srv.accept();
} catch (IOException e) {
}
20 Budditha Hettige
Example
Reading Text from a Socket
try {
BufferedReader rd = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String str;
while ((str = rd.readLine()) != null) {
System.out.println(str);
}
rd.close();
} catch (IOException e) {
}
21 Budditha Hettige
Example
Writing Text to a Socket
try {
BufferedWriter wr = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
wr.write("aString");
wr.flush();
} catch (IOException e) {
}
22 Budditha Hettige
What is datagram
• A datagram is an independent, self-
contained message sent over the
network whose arrival, arrival time, and
content are not guaranteed
24 Budditha Hettige
Budditha Hettige
UDP Sockets Programming
• Sending/Receiving data.
– java.net.DatagramPacket class
• Type of UDP classes
– DatagramSocket
– DatagramPacket
– MultiCastSocket
25
Sending a Datagram
public static void send(InetAddress dst,
int port, byte[] outbuf, int len)
{
try { DatagramPacket request =
new DatagramPacket(outbuf, len, dst, port);
DatagramSocket socket =
new DatagramSocket();
socket.send(request);
} catch (SocketException e) {
} catch (IOException e) {
}
}
28 Budditha Hettige
Receiving a Datagram
try
{ byte[] inbuf = new byte[256]; // default size
DatagramSocket socket = new DatagramSocket();
// Wait for packet
DatagramPacket packet = new DatagramPacket
(inbuf, inbuf.length);
socket.receive(packet);
// Data is now in inbuf
int numBytesReceived = packet.getLength();
} catch (SocketException e)
{ }
catch (IOException e)
{ }
29 Budditha Hettige
Building a Simple Web Server
• Handles only one HTTP request
• Accepts and parses the HTTP request
• Gets the required file from the server’s file system.
• Creates an HTTP response message consisting of the requested file preceded by header lines
• Sends the response directly to the client
• Example – WebBrowser.java
31 Budditha Hettige