Docsity

Socket Programming: TCP Client-Server Interaction and I/O Multiplexing, Exercises of Data Communication Systems and Computer Networks

Partial preview of the text

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