Top Banner
Network Programming Eddie Aronovich mail: [email protected]
55

Network Programming

Jan 05, 2016

Download

Documents

Network Programming. Eddie Aronovich mail: [email protected]. How did it started ?. How can we write communication ?. Application Program Interface (API) Sockets TLI (Transport Layer Interface) System calls Library functions. What is it for ?. Communication systems provides 3 services: - PowerPoint PPT Presentation
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: Network Programming

Network Programming

Eddie Aronovich

mail: [email protected]

Page 2: Network Programming

How did it started ?1983 First wide

availiable Tcp/IP(BSD 4.2)

1988 (BSD 4.3)slow start,congestion

avoidence, fastretransmit

1990 (BSD 4.3)fast recovery, TCPheader prediction,

routing tables changes

1993(4.4BSD)multicast,

long fat pipemodification

1989 - Net/1

1991 - Net/2

1994 Net/3

Page 3: Network Programming

How can we write communication ?

Application Program Interface (API)

– Sockets

– TLI (Transport Layer Interface)

System calls Library functions

Page 4: Network Programming

What is it for ?

Communication systems provides 3 services:

– Information & resource Sharing

– Distance gapping

– Backup abilities

Page 5: Network Programming

Fundamentals

Server - An entity which gives services

Client - An entity which requests services

Transport layer - To transfer the data

Page 6: Network Programming

The 7 layers model

Layer 1Physical

Interface Layer(Ethernet, SLIP, loopback, etc)

Socket Layer

Protocol Layer(TCP/IP, OSI, Unix)

Layer 7Application

Layer 6Presentation

Layer 5Session

Layer 4Transport

Layer 3Network

Layer 2Data Link

Media

Process

Page 7: Network Programming

The 7 Layer model in real life

Process

Layer 7Application

Layer 6Presentation

Layer 5Session

Layer 1Physical

Media

Socket LayerLayer 4

Transport

Protocol Layer(TCP/IP, OSI, Unix)

Layer 3Network

Interface Layer(Ethernet, SLIP, loopback, etc)

Layer 2Data Link

Function Call

Function CallSocket Queue Socket Queue

Interfacequeue Protocol queue

Interfacequeue

Page 8: Network Programming

The message is built in one side...Message from

application

Message fromapplication

4th layerheader

Message fromapplication

4th layerheader

3rdlayer

header

Message fromapplication

4th layerheader

3rdlayer

header

2ndlayer

header

2ndlayertrailer

Page 9: Network Programming

And striped in the dest. side…

Ethernet

Driver

3rd layer3rd layer3rd layer

4th layer 4th layer

ApplicationApplication

Page 10: Network Programming

From Lynx to Netscapeor chat client

Presentation

Page 11: Network Programming

InitiatesConnection

Requestsservice

ProcessResponse

Closeconenction

InitiatesConnection

AcceptsRequest

Responseto Request

Closeconenction

Waits forconnection

Client Server

Page 12: Network Programming

Descriptors

Everything in Unix is a file Descriptor is an index into an array

fd1fd2fd3

.

.

.fdifdj..

proc {}

i-node

file{}

V-node

file{}

i-node

file{}

V-node

socket{}

Page 13: Network Programming

Memory Buffers

Contains Socket Address Structure

Headers

Data

Page 14: Network Programming

The Basics of communication

LectureII

Page 15: Network Programming

Processing Techniques

Page 16: Network Programming

The Client Server Model

The server roles:– Give service as asked– Wait the client to appeal to him

The client roles:– Start the communication process– Asks the wanted service

Page 17: Network Programming

Design considerations

Serve single or multiple users

Use reliable or unreliable protocol

Software updates

Page 18: Network Programming

Client and server roles in UDP

Server roles:– Bind a port– Wait for a message to come– Send reply

Client roles:– Send a message– Get the reply

Page 19: Network Programming

User Datagram Protocol*

Simple protocol

Connectionless

Unreliable

*{RFC 768}

Page 20: Network Programming

Socket:={ip_addr, port number}

API, an interface for the program to contact with communication.

Enable usage of regular file commands as read, write and so on.

The sockets are structures passed from kernel to process and vice versa.

Page 21: Network Programming

What the socket struct contains ?

Socket type {stream, dgram, raw,…} Socket options {broadcast, OOB...} Time to linger wait before close the socket Socket state flags Protocol Control Block Protocol Handle

Page 22: Network Programming

The socket & address structs

Generalstruct sockaddr {

uint8_t sa_len; /*Len of socket struct */sa_family_t sa_family; /*Addr family as AF_INET */

char sa_data[20]; /*Protocol Address */

IP V4 address socketstruct sockaddr_in {

uint8_t sin_len; /*The socket length*/sa_family_t sin_family; /*AF_INET for IP addresses*/unit16_t sin_port; /*The port id 16-bit

port num */struct in_addr sin_addr; /*The IP address 32-bit */char sin_zero [8]; /* FFU - Must be zero */

Page 23: Network Programming

How the socket is created

The system call passes identifiers for address family(e.g.AF_UNIX, AF_INET,etc.), socket type and protocol.

Socket data structure is allocated.

Pointer from the fd table to other i-node struct which points to the socket.

Page 24: Network Programming

User Datagram Protocol (rfc 768)

Data transport layer protocol(Fragment packets to fit local MTU)

Used to make available datagram packet switched mode

Connectionless protocol

Used when RTT is important or no connection needed

Page 25: Network Programming

UDP header

Source port number

(16 bit)

Destination port number (16 bit)

Length

(16 bit)

UDP Checksum

(16 bit)

Data

Page 26: Network Programming

UDP by network monitor

Page 27: Network Programming

Lets do it in UDP - client

C:\TEMP\udp-cli-c.htmC:\TEMP\udp-cli-c.htm

Page 28: Network Programming

Lets do it in UDP - server

C:\TEMP\udp-srv-c.html

Page 29: Network Programming

TCP

LectureIII

Page 30: Network Programming

Transmission Control Protocolrfc 793

Reliability

Sequenced data transfer

Flow control

Full duplex

Page 31: Network Programming

TCP HeaderSource port number

(16 bit)

Dest port number

(16 bit)

Sequence number (32 bit)

Acknowledgement number (32 bit)

Header length

(4 bit)

F.F.U

(6 bit)

UR

G

AC

K

PS

H

RS

T

SY

N

FIN

Window size

(16 bit)

TCP Checksum

(16 bit)

Urgent Pointer

(16 bit)

Options

Data

Page 32: Network Programming
Page 33: Network Programming
Page 34: Network Programming

Ack & Flow control

Acknowledgements are sent by byte.

The acks can be sent for group of packets (but should arrive before time out of first packet).

Window size is used for flow control and is controlled by receiver.

Page 35: Network Programming

How connection starts ?

connect(); listen();

connect returns…. accept();

accept returns….

SYN j

SYN k, ACK j+1

Connection Established

ACK k+1

Page 36: Network Programming

How we do it in C ?

Server Clientsocket(); socket();

bind();

connect();listen();

accept();

{New file descriptor is given}

Page 37: Network Programming

Ending Sessions

Passive close (FIN arrives) Vs. Active close

FIN_WAIT_1CLOSE_WAIT

FIN_WAIT_2LAST_ACK

TIME_WAIT(2MSL) CLOSED

FIN M

ACK M+1

FIN N

ACK N+1

Page 38: Network Programming

Internet Protocol

LectureIV

Page 39: Network Programming

Network Layer

Passing packet from source host to destination host (cross networks if needed)

Independent of the datalink layer

QoS

Flow control

Page 40: Network Programming

Internet Protocol

Connectionless

Unreliable

Best effort

Page 41: Network Programming

Address

IP address consist 32bit and is divided into {network id.,host id}

Part of host id can be used for subnet mask

Some special addresses are defined (as: net-addr, broadcast-addr, multicast-addr)

Page 42: Network Programming

Addresses and Classes

Class A

Addresses: 0-127.X.X.X

Class B

Addresses: 128-191.0-255.X.X

Class C

Addresses: 192-223.0-255.0-255.X

01XXXXXX

10XXXXXX

110XXXXX

Page 43: Network Programming

Masks

Mask is used to refine network division.

‘1’b in mask symbolize bit belongs to network address.

‘0’b in mask symbolize bit belongs to host address.

Page 44: Network Programming

Address conventions

Network address filled with ‘0’ in host address.

Broadcast address filled with ‘1’ in host address.

First, last subnet and address aren’t used

Page 45: Network Programming

How routing is done ?

The router compares destination IP with each Network & subnet address.

Unknown destination is routed to default.

Routers update each other with appropriate algorithms.

Page 46: Network Programming
Page 47: Network Programming
Page 48: Network Programming
Page 49: Network Programming
Page 50: Network Programming
Page 51: Network Programming

VIשיעור I/O Multiplexing

Page 52: Network Programming

I/O Models

Blocking I/O

Nonblocking I/O

I/O Multiplexing

Signal driven

Asynchronous I/O

Page 53: Network Programming

Non-Blocking I/O

No blocking is done (Error is returned)

Reading is done in loop (until data arrives)

Polling – costs CPU time

Implementation in real time systems only

Page 54: Network Programming

Signal Driven I/O

Signal handler should be started

Page 55: Network Programming