Top Banner
1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC
33

1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

Jan 02, 2016

Download

Documents

Gary Caldwell
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

1

A TCP/IP Application Programming Perspective

Chris Greenhalgh

G53ACC

Page 2: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

2

Contents

• Overview

• IP layer, IP addresses and API

• Transport layer– TCP service, API and examples– UDP service, API and examples

Books: Comer ch.17, 18, 24 & 25 (part), 28-30 (part); Farley ch. 2

Page 3: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

3

Overview

• The TCP/IP Internet Protocols– Internet =

Inter-Network

• Universal service: the illusion of a single network

Internal details

Concept (transparency:-)

Comer Fig. 17.3

Page 4: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

4

TCP/IP reference model

IEEE802Ethernet,WiFi, …

IP

TCP, UDP

Comer Fig. 17.4

Page 5: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

5

IP: Internet layer

• Deals with end-to-end communication between hosts on the Internet

• Identifies hosts using IP addresses– One per host network interface– Globally unique

• But only on the Internet proper• See also NAT & private IP addresses

(later notes)

Page 6: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

6

IP v.4 addresses: 32 bits

• Normally written in dotted decimal notation:

Comer Fig. 18.3

Page 7: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

7

One IP address per network interface (e.g.)

Host (computer)

Comer Fig. 18.9

Page 8: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

8

Special IP v.4 addresses

Network =class bit(s) and prefix

Comer Fig. 18.1, 18.8

Page 9: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

9

Address ranges, masks and CIDR addresses (e.g.)

CIDR notation

NB first 28 bits are 1s

NB bit-wise AND withMask value = prefix

Comer Fig. 18.7

Page 10: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

10

Java API: java.net.InetAddress

• Instance = single IP address and/or DNS hostname – (see later notes for more detail of DNS)

• API Excerpts: public class InetAddress { // host may be dotted IP or DNS name

public static InetAddress getByName(String host) throws UnknownHostException;

public static InetAddress getLocalHost()

throws UnknownHostException;public String getHostAddress();public String getHostName();public boolean isMulticastAddress();

}

Page 11: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

11

Transport Layer

• Allows multiple end-points per host

• Supports different qualities of service (QOS)– Connection oriented (stream) service

• Transport Control Protocol (TCP)

– Connectionless (datagram) service • User Datagram Protocol (UDP)

Page 12: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

12

Transport Control Protocol (TCP)

• Specified in RFC793

• Service:– Connection between two processes– Reliable delivery– Bi-directional– With flow and congestion control– e.g. used for remote login, email, HTTP

• Acts as a “pipe” between two endpoints.

Page 13: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

13

Connection-Oriented Services

• C.f. the telephone network– Pick up the phone = initiate a connection– Dial number = specify address of other party– Recipient answers phone = connection is

made– Speak = data is transferred in both directions– One or both parties hang up = break

connection

Page 14: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

14

Ports

• Each transport protocol has a set of ‘ports’ on the network– TCP & UDP have 16 bit port numbers (0-65535)

• Transport level protocols identify source & destination port numbers– Network level protocol (IP) identifies source and

destination host

• Application can be bound to one particular IP and one particular port, or to any local IP and one particular port

Page 15: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

15

Ports example

...1 2 3 4

Process A

Process B

Process C

Host=128.243.22.10

Network

PacketHost 128.243.22.10Port# 4

Page 16: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

16

Special port numbers

• Fixed port numbers allocated by Internet Assigned Numbers Authority (IANA)– E.g. HTTP, tcp port 80

• “Low” port-numbers typically reserved for standard services– E.g. <1024

• Or application may obtain “next unused” local port number from OS

Page 17: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

17

Sockets

• A socket– is an endpoint of communication

• Created and used within an application

– is bound to a particular port– has a type which specifies the type of

communications it supports • E.g. TCP (stream), UDP (datagram)

– communicates with sockets of the same type• May be linked to one specific socket, e.g. TCP, or may be

able to communicate with multiple other sockets, e.g. TCP server socket or UDP socket.

Page 18: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

18

Java API: java.net.ServerSocket

• Used by server• API Excerpts: public class ServerSocket { // port 0 => next unused local port

public ServerSocket(int port) throws java.io.IOException;

public InetAddress getInetAddress();public int getLocalPort();public Socket accept()

throws java.io.IOException;public void close()

throws java.io.IOException;}

Page 19: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

19

Java server skeleton

import java.net.Socket;import java.net.ServerSocket;import java.io.IOException;public class ReverseServerTCP { public static void main(String [] args) { try { int port = Integer.parseInt(args[0]); ServerSocket s = new ServerSocket(port); while (true) {

Socket c = s.accept(); // handle client...

} } catch (IOException e) { /* ... */ }}

Page 20: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

20

Java API: java.net.Socket

• Used by client and server (once connected)• API Excerpts: public class Socket { // constructors only used by client public Socket(String host, int port) throws UnknownHostException, IOException; public Socket(InetAddress host, int port) throws IOException; public InetAddress getInetAddress(); public int getPort(); public java.io.InputStream getInputStream() throws IOException; public java.io.OutputStream getOutputStream()

throws IOException; public void close() throws IOException;}

Page 21: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

21

Sample client (and full server)

• See ACCExamples– tcp/ReverseClientTCP.java– tcp/ReverseServerTCP.java

• Note: relies on java.io framework - TCP just moves a sequence of bytes– Use raw streams and/or BufferedInput/OutputStreams

if you want to exchange particular byte sequences– Use Reader/Writer if you want to exchange text– Use DataInput/DataOutput if you want to marshall

java primitive types– Use ObjectInputStream/ObjectOutputStream if you

want to marshall Java Serializable objects

Page 22: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

22

TCP Client and server structure

• Server– new ServerSocket(port)– serverSkt.accept()

• returns new Socket

– skt.getInputStream• read, etc.

– skt.getOutputStream• write, etc.

– skt.close()

• Client– new Socket(host, port)

– skt.getOutputStream• write, etc.

– skt.getInputStream• read, etc.

– skt.close()

connect

request

response

Page 23: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

23

Multi-threaded server execution

Main thread

accept()

Client-specific thread(s)

ServerSocket

client Socket(s)

new Thread().start() [UNIX: fork()]

Page 24: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

24

User Datagram Protocol (UDP)

• Specified in RFC 768

• Connectionless

• Datagrams can be transmitted to– A single machine– A group of machines (multicast or broadcast)

• Bidirectional

• Unreliable

• Maximum payload 64Kbytes (65535 bytes)

Page 25: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

25

Connectionless Services analogy

• Can be compared to the postal service– A letter is put into an envelope (data into a

datagram)– The envelope is addressed to the recipient– The envelope is put in the post box and

enters the delivery system– The envelope arrives (hopefully) at the

destination

Page 26: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

26

Connectionless Services

• Unreliable– May never arrive– No indication if the delivery is successful

• Unordered– May arrive in a different order

(a variety of possible routes)

• Rarely (but occasionally) duplicated• Error-checked (corrupt packets usually

detected and discarded)

Page 27: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

27

UDP and TCP

• Uses sockets API for both UDP and TCP– stream type for TCP– datagram type for UDP

• Uses port numbers for both– but UDP port 53 is NOT same as TCP port 53

Page 28: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

28

UDP uses

• Very simple request/response protocols– small maximum size, e.g. DNS

• No 3-way handshake delay (cf. TCP)• No reliability overhead/retransmit delay

– E.g. for streamed audio, video

• But:– no flow control, congestion control

(can be added, but quite tricky and more overhead again)

Page 29: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

29

Java API: java.net.DatagramSocket

• Used by client and server• API Excerpts: public class DatagramSocket {

public DatagramSocket() throws SocketException;

public DatagramSocket(int port) throws SocketException;

public int getLocalPort();public void setSoTimeout(int timeout)

throws SocketException;public void send(DatagramPacket p)

throws IOException;public void receive(DatagramPacket p)

throws IOException;public void close();

}

Page 30: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

30

Java API: java.net.DatagramPacket

• Instance combines:– Application data - byte array– Actual size used (number of bytes)– Source/destination InetAddress– Source/destination port

• Source if received, destination if sending

• API Excerpts: see over

Page 31: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

31

• public class DatagramPacket { public DatagramPacket(byte[] buf, int offset,

int length); public DatagramPacket(byte[] buf, int length); public DatagramPacket(byte[] buf, int offset,

int length, InetAddress address, int port); public DatagramPacket(byte[] buf, int length, InetAddress address, int port); public InetAddress getAddress(); public int getPort(); public byte[] getData(); public int getLength(); public void setAddress(InetAddress iaddr); public void setPort(int iport); public void setData(byte[] buf); public void setData(byte[] buf, int offset, int length); public void setLength(int length);}

Page 32: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

32

Sample UDP client and server

• See ACCExamples– unicast/ReverseClientUnicast.java– unicast/ReverseServerUnicast.java

• Note: can still use java.io framework - UDP just moves an array of bytes (<=65535)– Can get/set bytes directly– Can use ByteArrayInput/OutputStream to read/write

bytes as streams• Can combine with DataInput/Output or

ObjectInput/OutputStream as used with TCP

Page 33: 1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.

33

UDP Client and server structure

• Server– new DatagramSocket()– loop:

• new DatagramPacket()

• skt.receive(pkt)

• new DatagramPacket()

• skt.send(pkt)

– GC DatagramSocket

• Client– new DatagramSocket()– work:

• new DatagramPacket()

• skt.send(pkt)

• new DatagramPacket()

• skt.receive(pkt)

– GC DatagramSocket

request

response