Docsity

Network Programming Lecture 4: Sockets and TCP/IP Interaction, Slides of Network Programming

Partial preview of the text

Download Network Programming Lecture 4: Sockets and TCP/IP Interaction and more Slides Network Programming in PDF only on Docsity!

Network^ ProgrammingLec#

1

Ethernet

Gateways Two connected networks with a gatewayMultiple networks, multiple gateways

Client^ and

Server^ on

the^ Same

Ethernet

5

OSI^ Model

&^ TCP/IP

Suite^ Comparison

ApplicationPresentationSessionTransportNetworkData^ LinkPhysical

ApplicationTCP UDPIPv4,^ IPv6Device^ drive^ and^ Hardware

ApplicationUser^ Details ProcessSocketsKernelCommunications^ Details

7 6 5 4 3 2 1 7

STCP

-^ Sockets^ programming

interfaces^ are

interfaces^ from

upper

three^ layers^

into^ transport

layer

-^ How^ to^ write

applications

using^ sockets

that^ use^ either

TCP^ or

UDP^ or^ SCTP?

Why^ do

sockets

provide

the^ interface

from^ the

upper^ three

layers?

-^ First^ reason^ –^ Upper^ three

layers^ handle^ all

the^ details^ of^ the

application^ (FTP,

Telnet,

or^ HTTP,^ etc)^ and

know^ little^ about

the^ communication

details

-^ Lower^ four^ layers

know^ little^ about

the^ application,

but^ handle^ all^

the

communication

details • Sending data, waiting^ for^ acknowledgments,

sequencing^ data^

that^ arrives^ out

of^ order,^ calculating

and^ verifying^ checksums,

and^ so^ on

-^ Second^

reason – Upper three^ layers^ often

form^ what^ is^ called

a^ user^ process

while^ the

lower^ four^ layers

are^ normally^ provided

as^ part^ of^ the^ (OS)

kernel

-^ Unix^ provides

this^ separation

between^ the^ user

process^ and^ the

kernel,^ as^ do^ many

other^ contemporary

OS

-^ Therefore,^ interface

between^ layers^4

and^5 is^ natural^ place

to^ build^ API

TCP^ Client/Server

Interaction

1.^ Create^ a^ TCP

socket

2.^ Establish^ connection3.^ Communicate4.^ Close^ the^

connection

1.^ Create^ a^ TCP

socket

2.^ Specify

server^ IP

address and^ port 3. Establish^ connection

with^ server

4.^ Read^ and^

display^ server^ reply

5.^ Terminate

program

Unix^ errno

Value

•^ When^ an

error^ occurs

in^ a^ Unix^ function,

global

variable^ errno

is^ set^ to^ a^ positive

value^ indicating

the

type^ of^ error

and^ the^ function

normally^ returns

‐^1

•^ err_sys^ function

looks^ at^ the

value^ of^ errno

and

prints^ the^ corresponding

error^ message

-^ e.g.,^ “Connection

timed^ out”^ if

errno^ equals

ETIMEDOUT

•^ Value^ of^

errno^ is^ set

by^ a^ function

only^ if^ an^ error

occurs,^ otherwise,

it^ is^ undefined

•^ All^ of^ the

positive^ error

values^ are^

constants^ with

all

uppercase^ name

beginning^

with^ E^ and

are^ normally

defined^ in^ the

<sys/errno.h>

header

11

Create^

TCP^ socket

The^ socket

function^

creates^ an

internet(AF_INET)stream

(SOCK_STREAM)socket.

The^ function

returns^ a

small^ integer

descriptor

that^ we^ can

use^ to^ identify

the^ socket

in^ all^ future

function^ calls(call

to^ connect

and^ read).

If^ call^ to

socket^ fails,

we^ abort

the^ program

by

calling^ our

own^ err_sys

function.

TCP^ Client/Server

Interaction

1.^ Create^ a^ TCP

socket

2.^ Specify

server^ IP

addr^ and Port 3. Establish^ connection

with^ server

4.^ Read^ and^

display^ server^ reply

5.^ Terminate

program bzero(&servaddr, sizeof(servaddr));servaddr.sin_family = AF_INET;servaddr.sin_port

= htons(13);^

/* daytime server */ if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)err_quit("inet_pton error for %s", argv[1]);

TCP^ Client/Server

Interaction

1.^ Create^ a^ TCP

socket

2.^ Specify

server^ IP

address and^ port 3. Establish^ connection

with^ server

4.^ Read^ and^

display^ server^ reply

5.^ Terminate

program

if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)

err_sys("connect error");

Establish

connection

with^ server

The^ connect^

function,^ when

applied^ to^ TCP

socket,^ established

a^ TCP

connection^ with

the^ server^ specified

by^ the^ socket^ address

pointer^ to^ by^ the

second^ argument.Must^ specify

the^ length^ as^ third

argument^ to^ connect,

and^ for^ internet

socket^ address

structure.In^ the^ unp.h^

header,^ we^ #define

SA^ to^ be^ struct

sockaddr,^ that

is,^ a^ generic

socket^ address

structure.^ Everytime

one^ of^ the^ socket

functions^ requires

a

pointer^ to^ a^ socket

address^ structure,

that^ pointer^ must

be^ cast^ to^ a^ pointer

to

a^ generic^ socket

address^ structure.

This^ is^ because

the^ socket^ functions

predate^ the^ ANSI

C^ standard,^ so

the^ void^ *****^ pointer

type^ was^ not^ available

in

the^ early^ 1980s

when^ these^ functions

were^ developed.

The^ problem^ is

that

"struct^ sockaddr"

is^15 characters

and^ often^ causes

the^ source^ code

line^ to

extend^ past^ the

right^ edge^ of^ the

screen^ (or^ page,

in^ the^ case^ of^ a

book),^ so^ we

shorten^ it^ to^ SA.

Read^ and

display

server’s

reply

Read^ the

server’s^ reply

and^ display

using^ standard

I/O

fputs^ function. The^ server

reply^ is^ normally

a^26 ‐byte^

string.

With^ a^ byte

‐stream^ protocol,

these^26 bytes

can^ be

returned^ in

numerous

ways^ :^ a^ single

TCP^ segment

containing^

all^ of^ the^ data

,^ in^26 TCP

segements

each

containing^

1 byte^ of^ data.

With^ larger

data^ sizes^

,^ we^ cannot

assume^ that

the

server^ reply

will^ be^ with

single^ read.

Therefore

,^ for^ reading

TCP^ socket

we^ always

code^ them

in^ a^ loop^ and

either^ read

return^0 to

terminate.

TCP^ Client/Server

Interaction

1.^ Create^ a^ TCP

socket

2.^ Specify

server^ IP

address and^ port 3. Establish^ connection

with^ server

4.^ Read^ and^

display^ server^ reply

5.^ Terminate

program

exit(0);