
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

















1
Client^ and
Server^ on
the^ Same
Ethernet
5
ApplicationPresentationSessionTransportNetworkData^ LinkPhysical
ApplicationTCP UDPIPv4,^ IPv6Device^ drive^ and^ Hardware
ApplicationUser^ Details ProcessSocketsKernelCommunications^ Details
7 6 5 4 3 2 1 7
STCP
-^ Sockets^ programming
-^ How^ to^ write
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
-^ Therefore,^ interface
between^ layers^4
and^5 is^ natural^ place
to^ build^ API
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
-^ e.g.,^ “Connection
E^ and11
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]);
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
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.
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