Network
Network
Network
Message 1: Access
to resource
Processing
s
f acces
su l t o
ge 2: R e
Messa
Internet Reference Model
Telnet HTTP DNS
Application
FTP Layer
SMTP NFS SNMP
ICMP
OSPF/RIP Internet Control Message
Routing IP Network Layer
Information Internet Protocol
IGMP
Internet Group Management
Client Server
WWW browser WWW server
(Internet Explorer) (Apache)
HTTP HTTP
Port 80
TCP TCP
}
}
Networking
• Java’s fundamental networking capabilities are declared
by classes and interfaces of the java.net package,
through which Java offers stream-based communications.
• The classes and interfaces of java.net also offer
packet-based communications for transmitting individual
packets of information. This is most commonly used to
transmit audio and video over the Internet.
• We will focus on both sides of the client-server
relationship.
• The client requests that some action be performed, and
the server performs the action and responds to the client.
Networking (cont.)
• A common implementation of the request-response
model is between Web browsers and Web servers.
– When a user selects a Web site to browse through a
browser (a client application), a request is sent to the
appropriate Web server (the server application). The
server normally responds to the client by sending the
appropriate HTML Web page.
java.net
• “High-level” APIs
– Implement commonly used protocols such as HTTP, FTP, etc.
• “Low-level” APIs
– Socket-based communications
• Applications view networking as streams of data
• Connection-based protocol
• Uses TCP (Transmission Control Protocol)
– Packet-based communications
• Individual packets transmitted
• Connectionless service
• Used UDP (User Datagram Protocol)
Internet Reference Model
Application Layer
(HTTP, FTP, DNS, etc.)
Transport Layer
(TCP, UDP)
Network Layer
(IP)
class UDPServer {
public static void main(String args[]) throws Exception
{
//Create datagram socket on port 9876
DatagramSocket serverSocket = new DatagramSocket(9876);
while (true)
{
//create space for the received datagram
DatagramPacket receivePacket = new
DatagramPacket(receiveData,
receiveData.length);
//receive the datagram
serverSocket.receive(receivePacket);
class UDPClient {
public static void main(String args[]) throws Exception
{
//Create input stream
BufferedReader inFromUser = new BufferedReader(new
InputStreamReader(System.in));
//Create client socket
DatagramSocket clientSocket = new DatagramSocket();
//Translate hostname to IP address using DNS
InetAddress IPAddress = InetAddress.getByName("localhost");
clientSocket.receive(receivePacket);
Server responds by
returning the datagram
to the client in all capital
letters
Socket Programming with TCP
• Server process must first be running (must have created a
socket). Recall that TCP is not connectionless.
• Client contacts the server by creating client-local socket
specifying IP address and port number of server process.
Client TCP establishes connection to server TCP.
• When contacted by client, server TCP creates a new
socket for server process to communicate with client.
– Allows server to talk with multiple clients
– Source port numbers used to distinguish clients
close clientSocket
Example: Java server using TCP
//simple server application using TCP
import java.io.*;
import java.net.*;
class TCPServer {
public static void main (String args[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
while (true) {
//block on welcoming socket for contact by a client
Socket connectionSocket = welcomeSocket.accept();
//process
capitalizedSentence = clientSentence.toUpperCase() + '\n';
DataOutputStream(connectionSocket.getOutputStream());
//write out line to socket
outToClient.writeBytes(capitalizedSentence);
}
}
}
Example: Java client using TCP
//simple client application using TCP
import java.io.*;
import java.net.*;
class TCPClient {
public static void main (String args[]) throws Exception
{
String sentence;
String modifiedSentence;
sentence = inFromUser.readLine();
clientSocket.close();
}
}
Start TCP Server executing