






































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lab manual was designed by Prof. Prajin Ahuja at Birla Institute of Technology and Science for Data Communication Network lab. Its main points are: Fundamental, Environment, Descriptor, Comprehend, Sequence, Socket, Stream, Gethostname, Associated, Socket, Sock, Reliable, Delivery
Typology: Exercises
1 / 46
This page cannot be seen from the preview
Don't miss anything!







































Socket()—Get the file Descriptor
Bind()—What port am I on?
Connect()
Hey you!
Connect()—Hey, you!
Listen—will somebody please call me
Accept()—Thank you for calling
Send() and recv() – talk to me, friend
Sendto() and recvfrom() – talk to me, DGRAM syle
Close() and shutdown()
3
Close() and shutdown()
get outta my face
Getpeername() – who are you?
Gethostname() – who am I?
docsity.com
Get
Familiar
with
cygwin
linux
environment
yg
(Learn basic commands e.g. ls, cd, pwd, mkdir etc.,director tree of linux, use of gcc compiler)
d th
fi st 25 30
s^
f B
j^
id
Read the first 25-30 pages of Beej-guide
Read man pages of:
^
Socket()—Get the file Descriptor ^
Bind()—What port am I on? ^
Connect()—Hey, you! ^
Listen—will somebody please call me ^
Accept()—Thank you for calling
p^
y^
g
^
Send() and recv() – talk to me, friend ^
Sendto() and recvfrom() – talk to me, DGRAM syle ^
Close() and shutdown() – get outta my face ^
Getpeername() – who are you?
5
^
Getpeername()
who are you?
^
Gethostname() – who am I?
docsity.com
Recommended Texts:- Beej Guide
j
Netprog: Sockets API
The application creates a socketTh
k t
di t t
th
t l
f
Th
e socket
communication
pass data to the socket for network
pass data to the socket for networktransmission
receive data from the socket (transmittedth
h th
t^
k b
th
h
t)
8
th
rough the network by some other host)
docsity.com
SOCK_STREAM
TCP
reliable delivery
SOCK_DGRAM
UDP
reliable delivery
in-order guaranteed
connection-oriented
unreliable delivery
no order guarantees
no notion of “connection” –
bidirectional
app indicates dest. for eachpacket
can send or receive
AppApp
socket
3
2
1
t
App
socket
Dest.
socket
3
2
1
Q: why have type
medellin cs columbia edumedellin.cs.columbia.edu(128.59.21.14)
newworld.cs.umass.edu
cluster.cs.columbia.edu(128.59.21.14, 128.59.16.7,
(128.119.245.93)
(^
,^
,
128.59.16.5, 128.59.16.4)
11
Port 0
Port 0Port 1
Port 65535
23: Telnet
A socket provides an interfaceto send data to/from thenetwork through a port
see RFC 1700 (about2000 ports are
d)
g^
p
12
reserved)
associates and (can exclusively) reserves a port
y)
p
for use by the socket
int status = bind(sockid, &addrport, size);
1 if bi d f il d
status
: error status, = -1 if bind failed
sockid
: integer, socket descriptor
addrport
:^ struct sockaddr
, the (IP) address and port of the
p
machine (address usually set to
INADDR_ANY
local address)
size
: the size (in bytes) of the
addrport
structure
y
bind
can be skipped for both types of sockets.
When and why?
if only sending, no need to bind.
The OS finds a
port each time the socket sends a pkt
if receiving, need to bind
destination determined during conn. setup
don’t need to know port sending from (duringconnection setup receiving end is informed ofconnection setup, receiving end is informed ofport)
Passive participant
Active participant
p
p
step 1:
listen
(for
incoming requests)
step 3:
accept
(a
)
p
p
step 2: request &establish
connect
ion
request)
step 4: data transfer
The accepted
establish
connect
ion
step 4: data transferP^
p
connection is on a newsocket
The old socket
Passive Participant
l-sock
a-sock-
a-sock-
The old socketcontinues to listen forother activeparticipants
socket
socket
17
participants
Why?
Active 1
socket
Active 2
socket
Called by passive participant
int status
= listen(sock queuelen);
int status
listen(sock, queuelen);
status
: 0 if listening, -1 if error
sock
: integer, socket descriptor
queuelen
: integer # of active participants that can
queuelen
: integer, # of active participants that can
“wait” for a connection
listen
is
non-blocking
: returns immediately
int s = accept(sock &name &namelen);
int
s = accept(sock, &name, &namelen);
s
: integer, the new socket (used for data-transfer)
sock
: integer, the orig. socket (being listened on):
t^
t^
k dd
dd
ss
f th
ti
ti i
t
name
:^ struct sockaddr
, address of the active participant
namelen
:^ sizeof(name):
value/result parameter
-^
must be set appropriately before call
-^
adjusted by OS upon return
18
-^
adjusted by OS upon return
accept
is
blocking
: waits for connection before returning
int count = send(sock, &buf, len, flags);
: # bytes transmitted (-1 if error)
-^
buf
:^ char[]
buffer to be transmitted
-^
buf
:^ char[]
,^ buffer to be transmitted
: integer, length of buffer (in bytes) to transmit
: integer, special options, usually just 0
i t
t^
k &b f
l^
fl^
int count = recv(sock, &buf, len, flags);
: # bytes received (-1 if error)
:^ void[],
stores received bytes
: # bytes received
: integer, special options, usually just 0
Calls are
blocking
returns only after data is sent
20
g
y
(to socket buf) / received]
i t
t^
dt (
k &b f l
fl^
& dd
dd l
int count = sendto(sock, &buf, len, flags, &addr, addrlen);
: same as
send
:^ struct sockaddr
, address of the destination
:^ sizeof(addr)
int count = recvfrom(sock, &buf, len, flags, &addr,
&addrlen);&addrlen);
same as recv
:^ struct sockaddr
, address of the source
:^ sizeof(name):
value/result parameter
Calls are
blocking
[returns only after data is sent (to
socket buf) / received]
21
socket buf) / received]
docsity.com