using tcp through so c k ets da vid mazi eres dm
play

Using TCP Through So c k ets Da vid Mazi eres - PDF document

Using TCP Through So c k ets Da vid Mazi eres dm@amsterdam.lcs.mit.edu 1 File descriptors 1 Most I/O on Unix systems tak es place through the and system calls . Before read write discussing net w ork I/O,


  1. Using TCP Through So c k ets Da vid Mazi � eres dm@amsterdam.lcs.mit.edu 1 File descriptors 1 Most I/O on Unix systems tak es place through the and system calls . Before read write discussing net w ork I/O, it helps to understand ho w these functions w ork ev en on simple �les. If y ou are already familiar with �le descriptors and the and system calls, y ou read write can skip to the next section. Section 1.1 sho ws a v ery simple program that prin ts the con ten ts of �les to the standard output|just lik e the UNIX command. The function uses four system calls to cat typefile cop y the con ten ts of a �le to the standard output. int open(char *path, int flags, ...); � The op en system call requests access to a particular �le. path sp eci�es the name of the �le to access; determines the t yp e of access b eing requested|in this case flags read-only access. op en ensures that the named �le exists (or can b e created, dep ending on flags ) and c hec ks that the in v oking user has su�cien t p ermission for the mo de of access. If successful, op en returns a non-negativ e in teger kno wn as a �le descriptor. All read and op erations m ust b e p erformed on �le descriptors. File descriptors remain write b ound to �les ev en when �les are renamed or deleted or undergo p ermission c hanges 2 that rev ok e access . By con v en tion, �le descriptors n um b ers 0, 1, and 2 corresp ond to standard input, standard output, and standard error resp ectiv ely . Th us a call to p rintf will result in a to �le descriptor 1. write If unsuccessful, returns � 1 and sets the global v ariable to indicate the nature op en errno of the error. The routine p erro r will prin t \�lename: error message" to the standard error based on errno . int read (int fd, void *buf, int nbytes); � will read up to b ytes of data in to memory starting at buf . It returns the read nbytes n um b er of b ytes actually read, whic h ma y v ery w ell b e less than nbytes . If it returns 0, this indicates an end of �le. If it returns � 1, this indicates an error. 1 High-lev el I/O functions suc h as fread and fp rintf are implemen ted in terms of read and write . 2 Note that not all net w ork �le systems prop erly implemen t these seman tics. 1

  2. int write (int fd, void *buf, int nbytes); � will write up to b ytes of data at to �le descriptor fd . It returns write nbytes buf the n um b er of b ytes actually written, whic h unfortunately ma y b e less than in nbytes some circumstances. W rite returns 0 to indicate an end of �le, and � 1 to indicate an error. int close (int fd); � deallo cates a �le descriptor. Systems t ypically limit eac h pro cess to 64 �le de- close scriptors b y default (though the limit can sometimes b e raised substan tially with the system call). Th us, it is a go o d idea to �le descriptors after their last setrlimit close use so as to prev en t \to o man y op en �les" errors. 1.1 type.c : Cop y �le to standard output #include <stdio.h> #include <unistd.h> #include <fcntl.h> void typefile (char *filename) { int fd, nread; char buf[1024]; fd = open (filename, O_RDONLY); if (fd == -1) { perror (filename); return; } while ((nread = read (fd, buf, sizeof (buf))) > 0) write (1, buf, nread); close (fd); } int main (int argc, char **argv) { int argno; for (argno = 1; argno < argc; argno++) typefile (argv[argno]); exit (0); } 2

  3. 2 TCP/IP Connections 2.1 In tro duction TCP is the reliable proto col man y applications use to comm unicate o v er the In ternet. TCP pro vides a stream abstraction: Tw o pro cesses, p ossibly on di�eren t mac hines, eac h ha v e a �le descriptor. Data written to either descriptor will b e returned b y a read from the other. Suc h net w ork �le descriptors are called so c k ets in Unix. Ev ery mac hine on the In ternet has a unique, 32-bit IP (In ternet proto col) address. An IP address is su�cien t to route net w ork pac k ets to a mac hine from an ywhere on the In ter- net. Ho w ev er, since m ultiple applications can use TCP sim ultaneously on the same mac hine, another lev el of addressing is needed to disam biguate whic h pro cess and �le descriptor in- coming TCP pac k ets corresp ond to. F or this reason, eac h end of a TCP connection is named b y 16-bit p ort n um b er in addition to its 32-bit IP address. So ho w do es a TCP connection get set up? T ypically , a serv er will listen for connections on an IP address and p ort n um b er. Clien ts can then allo cate their o wn p orts and connect to that serv er. Serv ers usually listen on w ell-kno wn p orts. F or instance, �nger serv ers listen on p ort 79, w eb serv ers on p ort 80 and mail serv ers on p ort 25. A list of w ell-kno wn p ort n um b ers can b e found in the �le /etc/services on an y Unix mac hine. The Unix utilit y will allo w to y ou connect to TCP serv ers and in teract with telnet them. By default, telnet connects to p ort 23 and sp eaks to a telnet daemon that runs login. Ho w ev er, y ou can sp ecify a di�eren t p ort n um b er. F or instance, p ort 7 on man y mac hines runs a TCP ec ho serv er: athena% telnet athena.dialup.mit.edu 7 ...including Athena's default telnet options: "-ax" Trying 18.184.0.39... Connected to ten-thousand-dollar-bil l.di alup .mi t.ed u. Escape character is '^]'. repeat after me... repeat after me... The echo server works! The echo server works! quit quit ^] telnet> q Connection closed. athena% Note that in order to quit telnet, y ou m ust t yp e Con trol-] follo w ed b y q and return. The ec ho serv er will happily ec ho an ything y ou t yp e lik e quit. As another example, let's lo ok at the �nger proto col, one of the simplest widely used TCP proto cols. The Unix finger command tak es a single argumen t of the form user@host . It then connects to p ort 79 of host , writes the string and a carriage-return line-feed user 3

Recommend


More recommend