cs 640 comput er net working
play

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


  1. 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 • Programming Assignment 1 (MNS) 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 1. Client sends request Client Server Resource process process 4. Client 2. Server 3. Server sends response handles handles response request Note: clients and servers are processes running on hosts (can be the same or dif f erent hosts). Page 1

  2. Socket s as means f or int er-process communicat ion (I PC) application layer application layer Internet Client Process Server Process Socket Socket transport layer (TCP/UDP) transport layer (TCP/UDP) OS network OS network Internet network layer (IP) network layer (IP) stack stack link layer (e.g. ethernet) link layer (e.g. ethernet) Internet physical layer physical layer The int erf ace t hat t he OS provides t o it s net working subsyst em I nt ernet Connect ions (TCP/ I P) • 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” Client socket address Server socket address 128. 2. 194. 242:3479 208. 216. 181. 15:80 Server Client (port 80) Connection socket pair (128. 2. 194. 242:3479, 208. 216. 181. 15:80) Client host address Server host address 128. 2. 194. 242 208. 216. 181. 15 Note: 3479 is an Note: 80 is a well- known port associated with Web servers ephemeral port allocated by the kernel 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 Page 2

  3. Using Port s t o I dent if y Services Server host 128. 2. 194. 242 Client host Service request f or Web server 128. 2. 194. 242:80 (port 80) (i. e. , the Web server) Kernel Client Echo server (port 7) Service request f or Web server 128. 2. 194. 242:7 (port 80) (i. e. , the echo server) Kernel Client Echo server (port 7) 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 See / etc/ services f or a comprehensive list of the – Port 23: t elnet server services available on a – Port 25: mail server Linux machine. – Port 80: HTTP server • Ot her applicat ions should choose bet ween 1024 and 65535 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. Page 3

  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, … 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)); … • St ruct ure Cast s – You will see a lot of ‘st ruct ure cast s’ 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 Page 4

  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 RECEIVE SEND CLOSE Client +server: connect ion-orient ed BIND SOCKET LISTEN CONNECT ACCEPT TCP three-way handshake SEND RECEIVE SEND RECEIVE CLOSE Concurrent server Page 5

  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 */ Page 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend