



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




-! an endpoint of a connection! -! similar to UNIX file I/O API (provides a file descriptor)! -! associated with each end-point (end-host) of a connection! -! identified by the IP address and port number of both the sender and receiver!
-! runs on Linux, FreeBSD, OS X, Windows! -! fed/fed off the popularity of TCP/IP! -! can build higher-level interfaces on top of sockets! -! e.g., Remote Procedure Call (RPC)!
-! does not require multiple threads!
-! http://www.kohala.com/start/unpv12e.html!
Stevens TCP/IP Illustrated v. 2 p. 446! sd!
-! Stream v.s. datagram!
-! connection-oriented! -! reliable, in order delivery! -! at-most-once delivery, no duplicates! -! used by e.g., ssh, http!
-! connectionless (just data-transfer)! -! ābest-effortā delivery, possibly lower variance in delay! -! used by e.g., IP telephony, streaming audio, streaming video, Internet gaming, etc.!
-! translates cs.usc.edu to its IP address (128.125.1.45)! -! decides to use TCP as the transport protocol (Why?)! -! creates a socket! -! connects to 128.125.1.45 at the well-known SMTP port # (25)! -! parcels out your email into packets! -! sends the packets out!
-! transmitted! -! routed! -! buffered! -! forwarded, or! -! dropped!
known port #!
requests!
socket () bind () listen () accept () recv () close () socket () connect () send () recv () send () time^ close () initialize establish data xfer terminate Client Server
!! When contacted by client, server TCP creates new socket for server process to communicate with client
-! allows server to talk with multiple clients -! source port numbers used to distinguish clients
int sd; if ((sd = socket (AF_INET, SOCK_STREAM , IPPROTO_TCP)) < 0) { perror("socket"); printf("Failed to create socket\n"); abort(); }
struct sockaddr_in sin; struct hostent *host = gethostbyname(argv[1]); unsigned int server_addr = *(unsigned long *) host->h_addr_list[0]; unsigned short server_port = atoi(argv[2]); memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = server_addr; sin.sin_port = htons( server_port ); if ( connect (sd, (struct sockaddr *) &sin, sizeof (sin)) < 0) { perror("connect"); printf("Cannot connect to server\n"); abort(); } connect() initiates connection (for TCP)!
int addr_len = sizeof(addr); int td; td = accept( sd , (struct sockaddr *) &addr, &addr_len); if ( td < 0) { perror("error accepting connection"); abort(); }
Stevens TCP/IP Illustrated v. 2 pp. 441, 461! sd! sd! td! td!
int receive_packets(char *buffer, int buffer_len, int *bytes_read) { int left = buffer_len - *bytes_read; received = recv( td , buffer + *bytes_read, left, 0);
.... return 0; }
-! SO_RCVBUF and SO_SNDBUF used to set buffer sizes! -! SO_KEEPALIVE tells server to ping client periodically!
-! stdin (typically keyboard/mouse input)! -! sockets!
multithreading: each thread handles one I/O stream (482)! I/O multiplexing: a single thread handles multiple I/O streams" Flavors:! a.! blocking I/O (default):!
-! put process to sleep until I/O is ready! -! blocking for: device availability and I/O completion! -! by polling or use of select() b.! non-blocking I/O:! -! only checks for device availability! -! by polling or signal driven (not covered)! c.! asynchronous I/O:! -! process is notified when I/O is completed (not covered)!
int opts = fcntl(sock, F_GETFL); if (opts < 0) { perror("fcntl(F_GETFL)"); abort(); } if (fcntl(sock, F_SETFL, opts | O_NONBLOCK) < 0) { perror("fcntl(F_SETFL)"); abort(); } while (1) { if (receive_packets(buffer, buffer_len, &bytes_read) != 0) { break; } if (read_user(user_buffer, user_buffer_len, &user_bytes_read) != 0) { break; } } get data from socket get user input get current socket option settings set non-blocking I/O socket option
-! waits on multiple file descriptors/sockets and timeout! -! application does not consume CPU cycles while waiting! -! maxfd is the maximum file descriptor number + 1! -! if you have only one descriptor, number 5, maxfd is 6! -! descriptor sets provided as bit mask! -! use FD_ZERO, FD_SET, FD_ISSET, and FD_CLR " to work with the descriptor sets! -! returns as soon as one of the specified sockets are ready "
-! returns # of ready sockets, -1 on error, 0 if timed out and no device is ready (what for?)!
fd_set read_set; struct timeval time_out; while (1) { FD_ZERO(read_set); FD_SET(stdin, read_set); /* stdin is typically 0 / FD_SET(sd, read_set); time_out.tv_usec = 100000; time_out.tv_sec = 0; err = select(MAX(stdin, sd) + 1, &read_set, NULL, NULL, &time_out); if (err < 0) { perror ("select"); abort (); } if (err > 0) { if (FD_ISSET(sd, read_set)) if (receive_packets(buffer, buffer_len, &bytes_read) != 0) break; if (FD_ISSET(stdin, read_set)) if (read_user(user_buffer, user_buffer_len, &user_bytes_read) != 0) break; } else {... / timed out */ } } set up parameters for select() run select() interpret result
Naming and Addressing!
Name and Address Manipulation!
-! file system that doesnāt support folders/sub-directories!
-! Duncan McLeod, William Wallace!
-! 5 Wilberforce Rd., Cambridge, Cambridgeshire, England, UK! -! Japan, Tokyo-to, Minato-ku, Shirokanedai 4-chome 6-41! -! +1 734 763 1583!