OSI and TCP/IP Layers in Java Networking: Understanding Sockets, UDP, TCP, and Datagrams, Slides of Java Programming

An overview of the osi and tcp/ip layers in java networking, focusing on sockets, udp, tcp, and datagrams. It covers the structure and functions of each layer, as well as practical examples of creating servers and clients using java code.

Typology: Slides

2011/2012

Uploaded on 03/11/2012

holmger
holmger 🇸🇪

1

(1)

12 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Basic Networking in Java
CalinCur escu
Basic Networking in Java
Calin Curescu
12 pages
TDDC32 lecture 2, 2006
Basic Networking in Java
CalinCur escu
2of 12
TDDC32 lecture 2, 2006
OSI and TCP/IP Layers
ISO (International Standards Organization) has created a
layered model, called the OSI (Open Systems Interconnect)
TCP/IP (Transmission Control Protocol / Internet Protocol)
already developed
OSI Layer Description TCP/IP Comments
Application Application level protocol
Presentation Encoding, Formatting
Session Manages end-user comm.
HTTP, FTP
Transport Transparent user data, rel iability,
manages packets
TCP, UDP Source and destination
port numbers
Network Packets between source &
destination, routing
IP Source & destination
IP numbers
Data Link Data between nodes
Physical Bits on the medi um (cable)
Network
frames
Basic Networking in Java
CalinCur escu
3of 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
Basically adding source and destination port to IP packet
May arrive in any order
Unreliable
Datagrams can be lost
Basic Networking in Java
CalinCur escu
4of 12
TDDC32 lecture 2, 2006
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
Needed for UDP communication
TCP provides reliable flows, which are automatically
transformed to InputStream and OutputStream
Basic Networking in Java
CalinCur escu
5of 12
TDDC32 lecture 2, 2006
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
Server needs the initial socket to continue to listen
On client side the connection attempt returns successfully
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
CalinCur escu
6of 12
TDDC32 lecture 2, 2006
Server Example
import java.net.*;
import java.io.*;
public class EchoServer {
public static void main(String[] args) throws IOE xception {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(7);
} catch (IOException e) {
System.err.println("Could not listen on port: 7");
System.exit(1);
}
Socket clientSocket = null;
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();
}
}
//thread blocks until a client connects
//ports under 1024 are usually reserved by OS
Examples from “Java Tutorials”:
http://java.sun.com/docs/books/tutorial/networking/
pf2

Partial preview of the text

Download OSI and TCP/IP Layers in Java Networking: Understanding Sockets, UDP, TCP, and Datagrams and more Slides Java Programming in PDF only on Docsity!

Basic Networking in Java Calin Curescu

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

OSI and TCP/IP Layers

• ISO (International Standards Organization) has created a

layered model, called the OSI (Open Systems Interconnect)

• TCP/IP (Transmission Control Protocol / Internet Protocol)

already developed

OSI Layer Description TCP/IP Comments

Application Application level protocol

Presentation Encoding, Formatting

Session Manages end-user comm.

HTTP, FTP

Transport Transparent user data, reliability,

manages packets

TCP, UDP Source and destination

port numbers

Network Packets between source &

destination, routing

IP Source & destination

IP numbers

Data Link Data between nodes

Physical Bits on the medium (cable)

Network

frames

Basic Networking in Java Calin Curescu

3 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

– Basically adding source and destination port to IP packet

• May arrive in any order

– Unreliable

• Datagrams can be lost

Basic Networking in Java Calin Curescu

4 of 12 TDDC32 lecture 2, 2006

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

– Needed for UDP communication

– TCP provides reliable flows, which are automatically

transformed to InputStream and OutputStream

Basic Networking in Java Calin Curescu

5 of 12 TDDC32 lecture 2, 2006

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

• Server needs the initial socket to continue to listen

• On client side the connection attempt returns successfully

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

6 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; try { serverSocket = new ServerSocket(7); } catch (IOException e) { System.err.println("Could not listen on port: 7"); System.exit(1); } Socket clientSocket = null; 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(); } }

//thread blocks until a client connects

//ports under 1024 are usually reserved by OS

Examples from “Java Tutorials”:

http://java.sun.com/docs/books/tutorial/networking/

Basic Networking in Java Calin Curescu

7 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;

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) {

System.err.println("Unknown host: dalix.ida.liu.se ");

System.exit(1);

} catch (IOException e) {

System.err.println("IOException connecting to: dalix.ida.liu.se ");

System.exit(1);

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();

//takes an available free local port //server’s address and port

//automatically tries to resolve

hostname to IP address (using a DNS)

//automatically tries connect to server

Basic Networking in Java Calin Curescu

8 of 12 TDDC32 lecture 2, 2006

Multiple clients

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

• On the server side:

Basic Networking in Java Calin Curescu

9 of 12 TDDC32 lecture 2, 2006

Datagram Server

import java.io.*;

import java.net.*;

import java.util.*;

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

10 of 12 TDDC32 lecture 2, 2006

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 "); 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(); } }

Basic Networking in Java Calin Curescu

11 of 12 TDDC32 lecture 2, 2006

Pipes

• Inter-thread communication

– Shared variables

– Method calls

– Shared files

– Pipe Streams

• or use PipedInputStream and PipedOutputStream

PipedWriter pipeOut = new PipedWriter();

PipedReader pipeIn = new PipedReader(pipeOut);

pipeOut

pipeIn

Thread A

Thread B

Basic Networking in Java Calin Curescu

12 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

• Additional reading

– The Java Tutorial

• Trail: Custom Networking

• http://java.sun.com/docs/books/tutorial/networking/index.html