111CMSC 212 – S07 (lect 19)AnnouncementsProject #3– grades are available on grades.cs.umd.eduProject #4– Use gdb and valgrind– test cases given are only a subsetExam– Tuesday April 10 – 6:00-7:30pm – ARM 0126– No class on that day – extra study timeReading– socket handout - on web (Today)– notes (Thursday)2CMSC 212 – S07 (lect 19)Internet AbstractionsDomain Name System– character strings to represent names– Example name: mashie.cs.umd.eduIP Address (in IPv4)– 32 bit value that describes a network card– Example address: 128.8.129.30Port (TCP)– 16 bit endpoint for communication on a machine– Well known ports:• used to connect to a service (e.g. 80 is HTTP)– Machine assigned:• used for incoming connections223CMSC 212 – S07 (lect 19)– Unique identification of communication• 4 tuple of <[srcIp,srcPort], [destIp, destPort]>Networking Port: 25Port: 80Port: 666Port: 12344CMSC 212 – S07 (lect 19)Network API Typesstruct hostent– represents a host– struct hostent * gethostbyname(char *name);• returns the information about the passed host namestruct sockaddr– end point for communication– can be used for IP or other protocols toostruct sockaddr_in– defines an endpoint on an IP network– contains fields for src/dest IP addr and src/dest portfile descriptor– used to refer to an active socet335CMSC 212 – S07 (lect 19)Socket API Calls (Client)int socket(int domain, int type, int protocol);– create a new socket– typical usage:• fd = socket(AF_INET, SOCK_STREAM, 0);• creates a TCP socketint connect(int sockfd, struct sockadrr*server, int len);– connect to the remote host/port described by sockaddr– sockfd must be a previously created socket– len is the size of struct sockaddr6CMSC 212 – S07 (lect 19)Socket API Calls (Server)int bind(int sockfd, struct sockaddr *addr, int len);– bind a local port to the passed socket– port is specificed in the passed sockaddrint listen(int sockfd, int backlog);– inform OS to let connections happen on the passed sock– allow at most backlog pending connectionsint accept(int sockfd, struct sockaddr *addr, int len);– blocks until a connection arrives on passed sockfd– returns a new file descriptor for the active connection– fill out addr with information about established connection447CMSC 212 – S07 (lect 19)Network API - Using and Open SocketSockets can be read/written just like other fdFILE *ofp, ifp;int serverfd;serverfd = openserverfd("www.dyninst.org", 80) ;ofp = fdopen(serverfd, "w");if (!ofp) exit (-1);ifp = fdopen(serverfd, "r");if (!ifp) exit(-1);fprintf(ofp, "GET index.html\n");while (fgets(line, sizeof(line), ifp)) {printf(line);}8CMSC 212 – S07 (lect 19)Other DetailsFor example– see Client.Server/client.c– see Client.Server/server.cWhen done with a socket– use close to
View Full Document