Download Basic Networking in Java OSI and TCP/IP Layers TCP

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
OSI and TCP/IP Layers
Basic Networking in Java
OSI Layer
Description
TCP/IP
Application
Presentation
Session
Transport
Application level protocol
Encoding, Formatting
Manages end-user comm.
Transparent user data, reliability,
manages packets
Packets between source &
destination, routing
Data between nodes
Bits on the medium (cable)
HTTP, FTP
Network
Data Link
Physical
Calin Curescu
TCP, UDP
IP
Comments
Source and destination
port numbers
Source & destination
IP numbers
Network
frames
• ISO (International Standards Organization) has created a
layered model, called the OSI (Open Systems Interconnect)
• TCP/IP (Transmission Control Protocol / Internet Protocol)
already developed
Basic Networking in Java
Calin Curescu
12 pages
TDDC32 lecture 2, 2006
Basic Networking in Java
Calin Curescu
2 of 12
TDDC32 lecture 2, 2006
TCP & UDP
• TCP
– Connection based protocol
• Data flows
• i.e. IP Packets are ordered, FIFO
– Reliable
• IP Packets are retransmitted
– Congestion and flow control
• UDP
– Connectionless protocol
• Independent datagrams
java.net package
• Sockets
– One endpoint of a two-way communication link between two
programs running on the network
– ServerSocket, Socket classes in java.net
• URL (Uniform Resource Locator)
– http://ida.liu.se:80/figure.jpg (protocol:resource)
– Connecting to resource
– Reading & writing (using input and output streams)
• Datagrams
– Basically adding source and destination port to IP packet
• May arrive in any order
– Unreliable
• Datagrams can be lost
– Needed for UDP communication
– TCP provides reliable flows, which are automatically
transformed to InputStream and OutputStream
Basic Networking in Java
Calin Curescu
3 of 12
TDDC32 lecture 2, 2006
Basic Networking in Java
Calin Curescu
Sockets for TCP
1. On server side, a socket (ServerSocket) is bound to a port and
listens for incoming client connections
2. On client side, the server address and port is known; the client
creates a new socket (Socket) and tries to connect to server
3. If everything ok
•
On server side the server accepts the connection and creates a
new socket (Socket) bound on a different port
•
On client side the connection attempt returns successfully
•
Server needs the initial socket to continue to listen
4. Client and server can communicate by using the InputStream and
OutputStream provided by Socket
•
Think about a pipe between server and client
•
A “pipe” is uniquely identified by [server address, server port,
client address, client port]
Basic Networking in Java
Calin Curescu
5 of 12
TDDC32 lecture 2, 2006
4 of 12
TDDC32 lecture 2, 2006
Server Example
import java.net.*;
import java.io.*;
public class EchoServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
//ports under 1024 are usually reserved by OS
try {
serverSocket = new ServerSocket(7);
} catch (IOException e) {
System.err.println("Could not listen on port: 7");
System.exit(1);
}
Socket clientSocket = null;
//thread blocks until a client connects
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.println(inputLine);
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
Examples from “Java Tutorials”:
http://java.sun.com/docs/books/tutorial/networking/
Basic Networking in Java
Calin Curescu
6 of 12
TDDC32 lecture 2, 2006
Client Example
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
//server’s address and port
//takes an available free local port
BufferedReader in = null;
try {
echoSocket = new Socket("dalix.ida.liu.se", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
//automatically tries to resolve
System.err.println("Unknown host: dalix.ida.liu.se ");
hostname to IP address (using a DNS)
System.exit(1);
} catch (IOException e) {
System.err.println("IOException connecting to: dalix.ida.liu.se ");
System.exit(1);
//automatically tries connect to server
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
Basic Networking in Java
Calin Curescu
7 of 12
TDDC32 lecture 2, 2006
Multiple clients
• On the server side:
while (true) {
accept a connection ;
//returning a new socket
create a thread to deal with the client ;
//i.e. that takes as an argument the new created socket
}
Basic Networking in Java
Calin Curescu
Datagram Server
import java.io.*;
import java.net.*;
import java.util.*;
Datagram Client
import java.io.*;
import java.net.*;
public class EchoDatagramClient {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.out.println("Usage: java DatagramClient <hostname>");
System.exit(1);
}
//get a datagram socket
DatagramSocket socket = new DatagramSocket();
String userInput;
while ((userInput = stdIn.readLine()) != null) {
//send request
byte[] buf = userInput.getBytes();
InetAddress address = InetAddress.getByName(args[0]);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 7777);
socket.send(packet);
//get response
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
//display response
String received = new String(packet.getData());
System.out.println("echo: " + received);
}
socket.close();
}
}
public class EchoDatagramServer {
public static void main(String[] args) throws IOException {
protected DatagramSocket socket = new DatagramSocket(7777);
while (true) {
try {
byte[] buf = new byte[256];
//receive request
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
//send the response to the client at "address" and "port"
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);
}
catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
Basic Networking in Java
Calin Curescu
9 of 12
TDDC32 lecture 2, 2006
Basic Networking in Java
Calin Curescu
Pipes
• Inter-thread communication
– Shared files
Thread A
10 of 12
TDDC32 lecture 2, 2006
The End
• RMI
– Remote method invocation
– Advanced networking technique
– Call methods on objects that are not on the same machine
– Shared variables
– Method calls
8 of 12
TDDC32 lecture 2, 2006
pipeOut
pipeIn
Thread B
– Pipe Streams
• Additional reading
– The Java Tutorial
• Trail: Custom Networking
• http://java.sun.com/docs/books/tutorial/networking/index.html
PipedWriter pipeOut = new PipedWriter();
PipedReader pipeIn = new PipedReader(pipeOut);
• or use PipedInputStream and PipedOutputStream
Basic Networking in Java
Calin Curescu
11 of 12
TDDC32 lecture 2, 2006
Basic Networking in Java
Calin Curescu
12 of 12
TDDC32 lecture 2, 2006