CS 640: Comput er Net working Ashut osh Shukla Lect ure 3 Net work - - PDF document

cs 640 comput er net working
SMART_READER_LITE
LIVE PREVIEW

CS 640: Comput er Net working Ashut osh Shukla Lect ure 3 Net work - - PDF document

CS 640: Comput er Net working Ashut osh Shukla Lect ure 3 Net work Programming Topics Client -server model Socket s int erf ace Socket primit ives Example code f or echoclient and echoserver Debugging Wit h GDB


slide-1
SLIDE 1

Page 1

Ashut osh Shukla Lect ure 3 Net work Programming

CS 640: Comput er Net working

  • Client -server model
  • Socket s int erf ace
  • Socket primit ives
  • Example code f or echoclient and

echoserver

  • Debugging Wit h GDB
  • Programming Assignment 1 (MNS)

Topics Client / sever model

  • Client asks (request ) –server provides (response)
  • Typically: single server - mult iple client s
  • The server does not need t o know anyt hing about t he

client – even t hat it exist s

  • The client should always know somet hing about t he

server – at least where it is locat ed

Client process Server process

  • 1. Client sends request
  • 2. Server

handles request

  • 3. Server sends response
  • 4. Client

handles response Resource Note: clients and servers are processes running on hosts (can be the same or dif f erent hosts).

slide-2
SLIDE 2

Page 2

The int erf ace t hat t he OS provides t o it s net working subsyst em

application layer transport layer (TCP/UDP) network layer (IP) link layer (e.g. ethernet) physical layer application layer transport layer (TCP/UDP) network layer (IP) link layer (e.g. ethernet) physical layer

OS network stack

Socket s as means f or int er-process communicat ion (I PC)

Client Process Server Process Socket OS network stack Socket Internet Internet Internet

I nt ernet Connect ions (TCP/ I P)

Connection socket pair (128. 2. 194. 242:3479, 208. 216. 181. 15:80) Server (port 80) Client Client socket address

  • 128. 2. 194. 242:3479

Server socket address

  • 208. 216. 181. 15:80

Client host address

  • 128. 2. 194. 242

Server host address

  • 208. 216. 181. 15
  • Address t he machine on t he net work

– By I P address

  • Address t he process

– By t he “port ”-number

  • The pair of I P-address + port – makes up a “socket -address”

Note: 3479 is an ephemeral port allocated by the kernel Note: 80 is a well- known port associated with Web servers

Client s

  • Examples of client programs

– Web browsers, ftp, telnet, ssh

  • How does a client f ind t he server?

– The I P address in t he server socket address ident if ies t he host – The (well-known) port in t he server socket address ident if ies t he service, and t hus implicit ly ident if ies t he server process t hat perf orms t hat service. – Examples of well known port s

  • Port 7: Echo server
  • Port 23: Telnet server
  • Port 25: Mail server
  • Port 80: Web server
slide-3
SLIDE 3

Page 3

Using Port s t o I dent if y Services

Web server (port 80) Client host Server host 128. 2. 194. 242 Echo server (port 7) Service request f or

  • 128. 2. 194. 242:80

(i. e. , the Web server) Web server (port 80) Echo server (port 7) Service request f or

  • 128. 2. 194. 242:7

(i. e. , the echo server) Kernel Kernel Client Client

Servers

  • Servers are long-running processes (daemons).

– Creat ed at boot -t ime (t ypically) by t he init process (process 1) – Run cont inuously unt il t he machine is t urned of f .

  • Each server wait s f or request s t o arrive on a

well-known port associat ed wit h a part icular service.

– Port 7: echo server – Port 23: t elnet server – Port 25: mail server – Port 80: HTTP server

  • Ot her applicat ions should choose bet ween 1024 and

65535

See / etc/ services f or a comprehensive list of the services available on a Linux machine.

Socket s

  • What is a socket ?

– To t he kernel, a socket is an endpoint of communicat ion. – To an applicat ion, a socket is a f ile descript or t hat let s t he applicat ion read/ writ e f rom/ t o t he net work.

  • Remember: All Unix I / O devices, including net works, are

modeled as f iles.

  • Client s and servers communicat e wit h each by reading

f rom and writ ing t o socket descript ors.

  • The main dist inct ion bet ween regular f ile I / O and

socket I / O is how t he applicat ion “opens” t he socket descript ors.

slide-4
SLIDE 4

Page 4

Socket Programming Cliches

  • Net work Byt e Ordering

– Net work is big-endian, host may be big- or lit t le-endian – Funct ions work on 16-bit (short ) and 32-bit (long) values – ht ons() / ht onl() : convert host byt e order t o net work byt e order – nt ohs() / nt ohl(): convert net work byt e order t o host byt e order – Use t hese t o convert net work addresses, port s, …

  • St ruct ure Cast s

– You will see a lot of ‘st ruct ure cast s’

struct sockaddr_in serveraddr; / * f ill in serveraddr with an address */ … / * Connect takes (struct sockaddr * ) as its second argument * / connect(clientf d, (struct sockaddr * ) &serveraddr, sizeof (serveraddr)); …

Socket primit ives

  • SOCKET: int socket(int domain, int type, int

protocol);

– domain := AF_I NET (I Pv4 prot ocol) – t ype := (SOCK_DGRAM or SOCK_STREAM ) – prot ocol := 0 (I PPROTO_UDP or I PPROTO_TCP) – ret urned: socket descript or (sockf d), -1 is an error

  • BI ND: int bind(int sockf d, struct sockaddr

*my_addr, int addrlen);

– sockf d - socket descript or (ret urned f rom socket ()) – my_addr: socket address, st ruct sockaddr_in is used – addrlen := sizeof (st ruct sockaddr)

struct sockaddr_in { unsigned short sin_family; /* address family (always AF_INET) */ unsigned short sin_port; /* port num in network byte order */ struct in_addr sin_addr; /* IP addr in network byte order */ unsigned char sin_zero[8]; /* pad to sizeof(struct sockaddr) */ };

  • LI STEN: int listen(int sockf d, int backlog);

– backlog: how many connect ions we want t o queue

  • ACCEPT: int accept(int sockf d, void *addr, int * addrlen);

– addr: here t he socket -address of t he caller will be writ t en – ret urned: a new socket descript or (f or t he t emporal socket )

  • CONNECT: int connect(int sockf d, struct sockaddr

*serv_addr, int addrlen); / / used by TCP client

– paramet ers are same as f or bind()

  • SEND: int send(int sockf d, const void *msg, int len, int

f lags);

– msg: message you want t o send – len: lengt h of t he message – f lags := 0 – ret urned: t he number of byt es act ually sent

  • RECEI VE: int recv(int sockf d, void *buf , int len, unsigned int

f lags);

– buf : buf f er t o receive t he message – len: lengt h of t he buf f er (“don’t give me more!”) – f lags := 0 – ret urned: t he number of byt es received

slide-5
SLIDE 5

Page 5

  • SEND (DGRAM-st yle): int sendto(int sockf d, const void *msg,

int len, int f lags, const struct sockaddr * to, int tolen);

– msg: message you want t o send – len: lengt h of t he message – f lags := 0 – t o: socket address of t he remot e process – t olen: = sizeof (st ruct sockaddr) – ret urned: t he number of byt es act ually sent

  • RECEI VE (DGRAM-st yle): int recvfrom(int sockf d, void *buf ,

int len, unsigned int f lags, struct sockaddr *from, int *f romlen);

– buf : buf f er t o receive t he message – len: lengt h of t he buf f er (“don’t give me more!”) – f rom: socket address of t he process t hat sent t he dat a – f romlen:= sizeof (st ruct sockaddr) – f lags := 0 – ret urned: t he number of byt es received

  • CLOSE: close (socketf d);

Client +server: connect ionless

CREATE BIND SEND SEND CLOSE RECEIVE

Client +server: connect ion-orient ed

Concurrent server

SOCKET BIND LISTEN CONNECT ACCEPT RECEIVE RECEIVE SEND SEND CLOSE TCP three-way handshake

slide-6
SLIDE 6

Page 6 EchoClient .c – # include’s

# include < st dio.h> / * f or print f () and f print f () */ # include < sys/ socket .h> / * f or socket (), connect (), sendt o(), and recvf rom() */ # include < arpa/ inet .h> / * f or sockaddr_in and inet _addr() */ # include < st dlib.h> / * f or at oi() and exit () */ # include < st ring.h> / * f or memset () */ # include < unist d.h> / * f or close() */ # def ine ECHOMAX 255 / * Longest st ring t o echo */

EchoClient .c -variable declarat ions

int main(int argc, char * argv[]) { int sock; / * Socket descript or * / st ruct sockaddr_in echoServAddr; / * Echo server address */ st ruct sockaddr_in f romAddr; / * Source address of echo */ unsigned short echoServPort =7; / * Echo server port */ unsigned int f romSize; / * address size f or recvf rom() */ char * servI P=“172.24.23.4”; / * I P address of server */ char * echoSt ring=“I hope t his works”; / * St ring t o send t o echo server */ char echoBuf f er[ECHOMAX+1]; / * Buf f er f or receiving echoed st ring * / int echoSt ringLen; / * Lengt h of st ring t o echo */ int respSt ringLen; / * Lengt h of received response */

EchoClient .c - creat ing t he socket and sending

/ * Creat e a dat agram/ UDP socket */ sock = socket (AF_I NET, SOCK_DGRAM, 0); / * Const ruct t he server address st ruct ure */ memset (&echoServAddr, 0, sizeof (echoServAddr)); / * Zero out st ruct ure */ echoServAddr.sin_f amily = AF_I NET; / * I nt ernet addr f amily * / echoServAddr.sin_addr.s_addr = ht onl(servI P); / * Server I P address * / echoServAddr.sin_port = ht ons(echoServPort ); / * Server port */ / * Send t he st ring t o t he server * / sendt o(sock, echoSt ring, echoSt ringLen, 0, (st ruct sockaddr *) &echoServAddr, sizeof (echoServAddr); / * Recv a response */

slide-7
SLIDE 7

Page 7

EchoClient .c – receiving and print ing

f romSize = sizeof (f romAddr); recvf rom(sock, echoBuf f er, ECHOMAX, 0, (st ruct sockaddr *) &f romAddr, &f romSize); / * Error checks like packet is received f rom t he same server*/ / * null-t erminat e t he received dat a * / echoBuf f er[echoSt ringLen] = ' \ 0' ; print f ("Received: %s\ n", echoBuf f er); / * Print t he echoed arg * / close(sock); exit (0); } / * end of main () * /

EchoServer.c

int main(int argc, char * argv[]) { int sock; / * Socket * / st ruct sockaddr_in echoServAddr; / * Local address * / st ruct sockaddr_in echoClnt Addr; / * Client address * / unsigned int cliAddrLen; / * Lengt h of incoming message * / char echoBuf f er[ECHOMAX]; / * Buf f er f or echo st ring */ unsigned short echoServPort =7; / * Server port */ int recvMsgSize; / * Size of received message * / / * Creat e socket f or sending/ receiving dat agrams * / sock = socket (AF_I NET, SOCK_DGRAM, 0); / * Const ruct local address st ruct ure */ memset (&echoServAddr, 0, sizeof (echoServAddr)); / * Zero out st ruct ure */ echoServAddr.sin_f amily = AF_I NET; / * I nt ernet address f amily * / echoServAddr.sin_addr.s_addr = ht onl(“172.24.23.4”); echoServAddr.sin_port = ht ons(echoServPort ); / * Local port */ / * Bind t o t he local address * / bind(sock, (st ruct sockaddr *) &echoServAddr, sizeof (echoServAddr);

f or (;;) / * Run f orever * / { cliAddrLen = sizeof (echoClnt Addr); / * Block unt il receive message f rom a client * / recvMsgSize = recvf rom(sock, echoBuf f er, ECHOMAX, 0, (st ruct sockaddr *) &echoClnt Addr, &cliAddrLen); print f ("Handling client %s\ n", inet _nt oa(echoClnt Addr.sin_addr)); / * Send received dat agram back t o t he client * / sendt o(sock, echoBuf f er, recvMsgSize, 0, (st ruct sockaddr *) &echoClnt Addr, sizeof (echoClnt Addr); } } / * end of main () * / Error handling is must

slide-8
SLIDE 8

Page 8

Socket Programming Help

  • man is your f riend

– man accept – man sendt o – Et c.

  • The manual page will t ell you:

– What # include< > direct ives you need at t he t op of your source code – The t ype of each argument – The possible ret urn values – The possible errors (in errno)

Debugging wit h gdb

  • Prepare program f or debugging

– Compile wit h “-g” (keep f ull symbol t able) – Don’t use compiler opt imizat ion (“-O”, “–O2”, … )

  • Two main ways t o run gdb

– On program direct ly

  • gdb progname
  • Once gdb is execut ing we can execut e t he program wit h:

– run args

– On a core (post -mort em)

  • gdb progname core
  • Usef ul f or examining program st at e at t he point of crash
  • Ext ensive in-program document at ion exist s

– help (or help <topic> or help <command> )

More inf ormat ion…

  • Socket programming

– W. Richard St evens, UNI X Net work Programming – I nf init e number of online resources – ht t p:/ / www.cs.rpi.edu/ courses/ sysprog/ socket s/ sock.ht ml

  • GDB

– Of f icial GDB homepage: ht t p:/ / www.gnu.org/ sof t ware/ gdb/ gdb.ht ml – GDB primer: ht t p:/ / www.cs.pit t .edu/ ~mosse/ gdb-not e.ht ml