Download Socket Programming: TCP Client-Server Interaction and I/O Multiplexing and more Exercises Data Communication Systems and Computer Networks in PDF only on Docsity!
LAB 2LAB
OBJECTIVEOBJECTIVE
To Learn the basic concepts of TCP
To Learn the basic concepts of TCP^ Client/Server model in networkprogramming
Ass
ignment
Ass gnment ^
Define a structure like the one given below.
f^
g
struct student{
char
IP[20];
int portno;
M dif
h^
TCP
li^
/^
d^
i^
i^
‘b^
j’
^
M
odify the TCP client/server code given in ‘beej’
guide such that:^
Instead of Passing a welcome message to client Instead of Passing a welcome message to client,child process passes the IP and port number ofthe client as struct of type ‘clientifo’ back toth
cli nt p
m^
hich
ill displ
this inf
5
th
e client program which will display this info.
on the screen
docsity.com
TCP Cl
ient/Server Interaction
TCP Cl ent/Server Interact on
Client
-Server Paradigm
Client Server ParadigmTypical network app has two pieces:
client and
server
Cli
t
applicationtransportnetworkdata link
Client:•^
Initiates contact with server(“speaks first”)
dataphysical
-^
Typically requests service fromserver,
-^
For Web, client is implemented in
request
,^
p
browser; for e-mail, in mailreader
Server:
replyapplicationtransportnetworkdata linkh
i^
l
Server:•^
Provides requested service toclient
-^
e g
Web server sends
p y physical
-^
e.g., Web server sendsrequested Web page, mail serverdelivers e-mail
What Transport ServiceD
A
li
ti
N
d?
Does an Application Need?Data loss
Timing
^
Some apps (e.g., audio) cantolerate some loss
^
Other apps (e g
file
g
^
Some apps (e.g., Internettelephony, interactivegames) require low delay to
^
Other apps (e.g., filetransfer, telnet) require100% reliable data transfer
g^
)^
q^
y
be “effective”
Bandwidth•^
Some apps (e.g., multimedia) require minimum amount ofb^
d^
idth t
b
“ ff
ti^
”
bandwidth to be “effective”
-^
Other apps (“elastic apps”) make use of whatever bandwidth theyget
Transmission ControlP
t^
l (TCP)
A
A
l
Protocol (TCP): An Analogy
TCP
Telephone Call
^
Reliable – guaranteedelivery
^
Byte stream
in order
p
•^
Guaranteed delivery
•^
In-order delivery
^
Byte stream
– in-order
delivery
^
Connection-oriented –
l^
k
•^
Connection-oriented
•^
Setup connectionfollowed by conversation
single socket perconnection
^
Setup connection
y
p
followed by datatransfer
Example TCP applications
11
Web, Email, Telnet
Network Address
ing Analogy
Network Address ng Analogy
Network Programming
Telephone Call
412-268-8000ext.
Applications/ServersWeb
Mail
412-268-8000ext.
Professors at CMU
g^
g
p
ext.
Port 80
Port 25
ext.
Port No.
Extension Central Number
Exchange
IP AddressNetwork No.
Telephone NoExchangeArea Code
Host Number
12
15-441 Students
Clients
Names and AddressesNames and Addresses
Each attachment point on Internet is given
Each attachment point on Internet is given^ unique address^
Based on location within network – like phone
p
numbers
Humans prefer to deal with names not
ddaddresses ^ DNS provides mapping of name to address ^ Name based on administrative ownership of^ Name based on administrative ownership ofhost
TCP Server
For example: web server
Web Server
For example: web server ^
What does a
web server
TCP
Port 80
need to do so that a
web
client
can connect to it?
IP
Ethernet Adapter
Socket I/O: bind() ^
A
socket
can be bound to a
port
()
int
fd;
socket
descriptor
struct
sockaddr_in
srv;
/* used
by
bind()
/* create
the
socket
srv.sin_family
=^
AF_INET
;^ /*
use
the
Internet
addr
family
srv.sin_port
htons
(^80
bind
socket
‘fd’
to
port
/*^
bind:
a^
client
may
connect
to
any
of
my
addresses
srv.sin_addr.s_addr
=^
htonl
( INADDR_ANY
_
if(
bind
(fd,
(struct
sockaddr*)
&srv,
sizeof(srv))
<^
0)^
perror("bind");
exit(1);
17
-^
Still not quite ready to communicate with a client...
Socket I/O
: listen()
Socket I/O l sten() ^ listen
indicates that the server will accept a connection
int fd;
/* socket descriptor */
struct sockaddr_in srv;
/* used by bind() */
/* 1) create the socket // 2) bind the socket to a port */if(
li t
(fd
if(
li
sten
(fd
perror(“listen”);exit(1);
-^
Still not quite ready to communicate with a client...
Socket I/O: accept()
ti
d
continued...^ struct sockaddr_in cli;
/* used by accept() */
int newfd;
/* returned by accept() */
int newfd;
/^
returned by accept()
int cli_len = sizeof(cli);
/* used by accept() */
newfd =
accept
(fd, (struct sockaddr*) &cli, &cli_len);
if(newfd < 0) {
perror("accept");exit(1);
}} • How does the server know which client it is?
-^
cli.sin_addr.s_addr
contains the client’s
IP address
-^
cli.sin_port
contains the client’s
port number
•^
Now the server can exchange data with the client byusing
read
and
write
on the descriptor
newfd
20
using
read
and
write
on the descriptor
newfd
•^
Why does
accept
need to return a new descriptor?
TCP Cl
ient
TCP Cl ent ^
For example: web client
2 Web Clients
p
^
How does a
web client
connect to a
web server
TCP IPIP
Ethernet Adapter
21
p