Socket programming Scripting Review 15 441 Computer Networks Spring 2014 M Tarek Abdellatif Agenda n n General Info Proxy Lab Agenda n n General Info Proxy Lab About Recitation n n n Thursday 5 30 to 6 20 pm Office hours by appointment Extra help by n appointment over email n n n mabdella qatar cmu edu Piazza ALWAYS come prepared for the recitation Project 1 n Project 1 out today n n Next Recitation SVN n n n n START EARLY http svnbook red bean com 27th Jan Checkpoint 1 Due 3rd Feb Checkpoint 2 Due 12th Feb Project 1 Due Systems Software n n Low level projects in C Designed to run forever n n n Must be secure n n Handle every possible error condition Manage resources carefully The Internet is not a friendly place Based on documented protocols Quite different from 213 n n n n Project size 1000s of lines vs 100s Project duration 4 5 weeks vs 1 2 weeks Apart from first project Partners vs solo developer Result n n You can t keep the state for 441 projects in your head at all times It s too big Requires more care in development Our Philosophy n n Your time is valuable and limited Some things are fun n n Some things are less fun n n Design initial coding a working project Agonizing debugging bad project grades spending 10x longer than you thought Use techniques that minimize time and maximize fun vs less fun Make it Easy for Yourself n Techniques take a bit of time to learn n n n E g revision control software Makefile next recitation But they will pay off Some techniques take a bit more up front time n n n E g writing good log messages thinking about design good debugging capabilities But they make the rest of the project more predictable and reduce the uncertainty of failing in the last day And they save debugging time Your job n Ask yourself Could I be doing this in a more efficient way n n Typing gcc g Wall foo c bar c baz c vs typing make Debugging Have I seen this bug before What caused it How could I avoid it n Be reflective strive to learn improve Don t prematurely optimize n Keep your programs simple n n n Easier to write Easier to debug But make it easy to change implementation details n Modularity Later lecture Optimizing your time n Sorting 3 numbers Do it by hand n Sorting 3000 numbers Do it in ruby n Sorting 300 000 000 000 numbers Write some serious code Agenda n n General Info Proxy Lab Identify the Destination n n n n n Addressing IP address hostname resolve to IP address via DNS Multiplexing port Server socket address 208 216 181 15 80 Client socket address 128 2 194 242 3479 Client Client host address 128 2 194 242 Connection socket pair 128 2 194 242 3479 208 216 181 15 80 FTP Server port 21 HTTP Server port 80 Server host address 208 216 181 15 Sockets n How to use sockets n Setup socket n Where is the remote machine IP address hostname What service gets the data port n Send and Receive n n Designed just like any other I O in unix send write recv read n Close the socket n n Overview Client Server socket socket bind open clientfd listen connect Client Server Session Connection request accept write read read write close EOF read close open listenfd Step 1 Setup Socket n Both client and server need to setup the socket n int socket int domain int type int protocol n domain n AF INET IPv4 AF INET6 for IPv6 n type n SOCK STREAM TCP SOCK DGRAM UDP n protocol n 0 n For example n int sockfd socket AF INET SOCK STREAM 0 n Step 2 Server Binding n n Only server need to bind int bind int sockfd const struct sockaddr my addr socklen t addrlen n sockfd n file descriptor socket returned n my addr n n struct sockaddr in for IPv4 cast struct sockaddr in to struct sockaddr struct sockaddr in short sin family e g AF INET unsigned short sin port e g htons 3490 struct in addr sin addr see struct in addr below char sin zero 8 zero this if you want to struct in addr unsigned long s addr load with inet aton What is that Cast n bind takes in protocol independent struct sockaddr struct sockaddr unsigned short char n sa family address family sa data 14 protocol address There are structs for IPv6 etc Step 2 Server Binding contd n addrlen n size of the sockaddr in struct sockaddr in saddr int sockfd unsigned short port 80 if sockfd socket AF INET SOCK STREAM 0 0 printf Error creating socket n from back a couple slides memset saddr 0 sizeof saddr zero structure out saddr sin family AF INET match the socket call saddr sin addr s addr htonl INADDR ANY bind to any local address saddr sin port htons port specify port to listen on if bind sockfd struct sockaddr saddr sizeof saddr 0 bind printf Error binding n What is htonl htons n n n Byte ordering Network order is big endian Host order can be big or little endian n x86 is little endian SPARC is big endian n Conversion n n htons htonl host to network short long ntohs ntohl network order to host short long n What need to be converted n n n n Addresses Port etc Step 3 Server Listen n Now we can listen n int listen int sockfd int backlog n sockfd n again file descriptor socket returned n backlog n number of pending connections to queue n For example n listen sockfd 5 Step 4 Server Accept n n n n n n n n n n Server must explicitly accept incoming connections int accept int sockfd struct sockaddr addr socklen t addrlen sockfd again file descriptor socket returned addr pointer to store client address struct sockaddr in cast to struct sockaddr addrlen pointer to store the returned size of addr should be sizeof addr For example int isock accept sockfd struct sockaddr in caddr clen Put Server Together struct sockaddr in saddr caddr int sockfd clen isock unsigned short port 80 if sockfd socket AF INET SOCK STREAM 0 0 from back a couple slides printf Error creating socket n memset saddr 0 sizeof saddr saddr sin family AF INET saddr sin addr s addr htonl INADDR ANY saddr sin port htons port zero structure out match the socket call bind to any local address specify port to listen on if bind sockfd struct sockaddr saddr sizeof saddr 0 bind printf Error binding n if listen sockfd 5 0 printf Error listening n listen for incoming connections clen sizeof caddr if isock accept sockfd struct sockaddr caddr clen 0 printf Error accepting n accept one What about client n n n n n Client need not bind listen and accept All client need to do is to connect …
View Full Document