Docsity

Socket Programming Part 1-Data Communication Network-Lab Mannual, Exercises of Data Communication Systems and Computer Networks

Partial preview of the text

Download Socket Programming Part 1-Data Communication Network-Lab Mannual and more Exercises Data Communication Systems and Computer Networks in PDF only on Docsity!

LAB 1LAB

OBJECTIVEOBJECTIVE ˆ

To get familiar with fundamental concepts

To get familiar with fundamental concepts^ of socket programming in linux environment

Common SOCKET APIs and their usage

mm

KE

g



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()

  • get outta my face

3

Close() and shutdown()

get outta my face



Getpeername() – who are you? 

Gethostname() – who am I?

docsity.com

Ass

ignment

Ass gnment ˆ

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) ˆ

R

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

Sockets ProgrammingSockets Programming

Recommended Texts:- Beej Guide

j

  • Unix Programming by Stevens

Netprog: Sockets API

What

is a socket?

What s a socket? ˆ

An interface between application and

f

pp

network^ 

The application creates a socketTh

k t

t^

di t t

th

t l

f



Th

e socket

type dictates the style of

communication

  • reliable vs. best effort• connection-oriented vs. connectionless

Once configured the application can



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

Two essential types of sockets

ˆ

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

D

t

App

D

socket

Dest.

socket

3

2

1

D2^9

D

Q: why have type

SOCK_DGRAM

A Socket-eye view of theI t

t

Internet

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)

Each host machine has an IP address

When a packet arrives at a host

11

When a packet arrives at a host

PortsPorts

Port 0

Each host has 65,

t

Port 0Port 1

ports

Some ports arer s rv d f r sp cific

Port 65535

reserved for specificapps^ 

20 21: FTP



20,21: FTP



23: Telnet 

80: HTTP

ˆ

A socket provides an interfaceto send data to/from thenetwork through a port



see RFC 1700 (about2000 ports are

d)

g^

p

12

reserved)

The

bind

function

The

bind

function

ˆ

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

  • chooses a

local address) 

size

: the size (in bytes) of the

addrport

structure

y

ˆ

bind

can be skipped for both types of sockets.

When and why?

Skipping the

bind

Skipping the

bind

SOCK DGRAM

SOCK

_DGRAM



if only sending, no need to bind.

The OS finds a

port each time the socket sends a pkt 

if receiving, need to bind

SOCK_STREAM



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)

Connect

ion setup cont’d

Connect on setup cont d ˆ

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

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

Connection setup

:^

listen

accept

Connection setup

listen

accept

ˆ

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

Send

ing / Receiving Data

Send ng / Rece v ng Data ˆ

W

ith a connection (

SOCK STREAM

W

(^

_



int count = send(sock, &buf, len, flags);

  • count

: # bytes transmitted (-1 if error)

-^

buf

:^ char[]

buffer to be transmitted

-^

buf

:^ char[]

,^ buffer to be transmitted

  • len

: integer, length of buffer (in bytes) to transmit

  • flags

: integer, special options, usually just 0

i t

t^

(^

k &b f

l^

fl^



int count = recv(sock, &buf, len, flags);

  • count

: # bytes received (-1 if error)

  • buf

:^ void[],

stores received bytes

  • len

: # bytes received

  • flags

: integer, special options, usually just 0



Calls are

blocking

[

returns only after data is sent

20

g

[

y

(to socket buf) / received]

Send

ing / Receiving Data

(cont’d)

Send ng / Rece v ng Data

(cont d)

Without a connection (

SOCK_DGRAM

i t

t^

dt (

k &b f l

fl^

& dd

dd l



int count = sendto(sock, &buf, len, flags, &addr, addrlen);

  • count, sock, buf, len, flags

: same as

send

  • addr

:^ struct sockaddr

, address of the destination

  • addrlen

:^ sizeof(addr)



int count = recvfrom(sock, &buf, len, flags, &addr,

&addrlen);&addrlen);

  • count, sock, buf, len, flags:

same as recv

  • name

:^ struct sockaddr

, address of the source

  • namelen

:^ sizeof(name):

value/result parameter

ˆ

Calls are

blocking

[returns only after data is sent (to

socket buf) / received]

21

socket buf) / received]

docsity.com