1 / 23

EECS340 Recitation 1: Very helpful to your project

EECS340 Recitation 1: Very helpful to your project. Hongyu Gao. If you’re not…. Burke Fetscher, Jedidiah McClurg, Beibei Lin, Xitao Wen, Gopi Vajravelu, Taiyo Sogawa, Galiya Ibrgimova, Andrew Lee, Cary Lee, Brad Weinberger, Jonathan Chan, Daniel Lieberman, Xin Zhao, or Weixian Wen

emilioj
Download Presentation

EECS340 Recitation 1: Very helpful to your project

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EECS340 Recitation 1: Very helpful to your project Hongyu Gao

  2. If you’re not… Burke Fetscher, Jedidiah McClurg, Beibei Lin, Xitao Wen, Gopi Vajravelu, Taiyo Sogawa, Galiya Ibrgimova, Andrew Lee, Cary Lee, Brad Weinberger, Jonathan Chan, Daniel Lieberman, Xin Zhao, or Weixian Wen • Email to: networkingta@gmail.com

  3. If you have not… Joined the newsgroup, • Use your favorite email client (thunderbird, outlook, etc. ) • Add a newsgroup account • Server name: news.cs.northwestern.edu • Select newsgroup: cs.340

  4. If you have not… Turned in homework1, ……

  5. Roadmap • How to do socket programming? • How to use hints from project 1?

  6. a host-local, application-created, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another application process socket Socket programming Goal: learn how to build client/server application that communicate using sockets Socket API • introduced in BSD4.1 UNIX, 1981 • explicitly created, used, released by apps • client/server paradigm • two types of transport service via socket API: • unreliable datagram • reliable, byte stream-oriented

  7. a host-local, application-created, OS-controlledinterface (a “door”) into which application process can both send and receive messages to/from another application process socket Socket programming Goal: learn how to build client/server application that communicate using sockets Socket API • introduced in BSD4.1 UNIX, 1981 • explicitly created, used, released by apps • client/server paradigm • two types of transport service via socket API: • unreliable datagram • reliable, byte stream-oriented

  8. network link physical link physical M M M Ht M Hn Hn Hn Hn Ht Ht Ht Ht M M M M Ht Ht Hn Hl Hl Hl Hn Hn Hn Ht Ht Ht M M M source message application transport network link physical segment datagram frame switch destination application transport network link physical router

  9. source application sockets Something (that I con’t care) in between destination application sockets

  10. Client side: Create client-local TCP socket specify IP address, port number of server process Establish connection to server Server side (When contacted by client): Accept the connection (automatically create a new socket) Communicate with client through the new socket (different than the socket that it’s listening on) Socket programming • Server always listens (waits) • To some socket (port) that welcomes client’s contact • Usually 80, 8000, 8080 for HTTP server • It’s always a client that initiates contact • Contact the socket (port) that the server is listening on

  11. Server – high level view Corresponding functions (parameters omitted): socket(SOCK_STREAM) bind() listen() //block accept() recv(), send() close() Create a socket Bind the socket with port Listen for connections Accept new client connections Read/write to client connections Shutdown connection

  12. Client – high level view Corresponding functions (parameters omitted): socket(SOCK_STREAM) connect() send() recv() close() Create a socket Connect to server Send HTTP request Receive HTTP response Shutdown connection

  13. Ipv4 socket address structure struct socketaddr_in{ uint8_t sin_len; /*length of the structure (16)*/ sa_falimily_t sin_family /* AF_INT*/ in_port_t sin_port /* 16 bit TCP or UDP port number*/ struct in_addr sin_addr /* 32 bit Ipv4 address */ char sin_zero(8)/* unused*/ } Hostent structure struct hostent{ char * h_name /*official name of host*/ char ** h_aliases; /* pointer ot array of\ pointers to alias name*/ int h_addrtype /* host address type*/ int h_length /* length of address */ char ** h_addr_list /*prt to array of ptrs with \ IPv4 or IPv6 address*/ } Make the socket Socket(int family , int type, int protocol); return nonnegative value for OK, -1 for error Resolve the host struct hostent *gethostbyname( const char *hostname); /*Return nonnull pointer if OK, NULL on error */ unit16_t htons(unit16_t host16bitvaule) /*Change the port number from host byte order to network byte order */ connect(int socketfd, const struct sockaddr * servaddr, socket_t addrlen) /*Perform the TCP three way handshaking*/ Setup up the struct Connect A piece of real code (client or server?) int connect_ socket( char *hostname, int port) { int sock; struct sockaddr_in sin; struct hostent *host; sock = socket( AF_ INET, SOCK_ STREAM, 0); if (sock == -1) return sock; host = gethostbyname( hostname); if (host == NULL) { close( sock); return -1; } memset (& sin, 0, sizeof( sin)); sin. sin_ family = AF_ INET; sin. sin_ port = htons( port); sin. sin_ addr. s_ addr = *( unsigned long *) host-> h_ addr_ list[ 0]; if (connect( sock, (struct sockaddr *) &sin, sizeof( sin)) != 0) { close (sock); return -1; } return sock; }

  14. Make the socket Setup up the struct Bind bind(int sockfd, const struct sockaddr * myaddr, socklen_t addrlen); /* return 0 if OK, -1 on error assigns a local protocol adress to a socket*/ Another piece of real code int make_ listen_ socket( int port) { struct sockaddr_ in sin; int sock; sock = socket( AF_ INET, SOCK_ STREAM, 0); if (sock < 0) return -1; memset(& sin, 0, sizeof( sin)); sin. sin_ family = AF_ INET; sin. sin_ addr. s_ addr = htonl( INADDR_ ANY); sin. sin_ port = htons( port); if (bind( sock, (struct sockaddr *) &sin, sizeof( sin)) < 0) return -1; return sock; }

  15. Client-server interaction TCP Server socket() bind() Well-known port TCP Client listen() Socket() accept() blocks until connection from client connect() Connection establishment Data(request) write() read() process request Data(reply) write() read() close() End-of-file notification read() close()

  16. Standard socket vs Minet “man” every function to get help on how to use it exactly!

  17. Dealing with blocking calls • Many functions block • accept(), connect(), recvfrom() • Consider the following case: • The server accepts a client, and blocks on receiving the HTTP request • Another client tries to initiate a connection

  18. How to handle multiple connections • Create multi-process or multi-threaded code • Not covered • I/O multiplexing using polling • Not covered • I/O multiplexing using select ()

  19. I/O Multiplexing: Select (1) • select() • Wait on multiple file descriptors/sockets and timeout • Return when any file descriptor • is ready to be read or written, or • Indicate an error, or • timeout exceeded • How to solve the blocking problem? • select() among the listening socket and all the opened connection

  20. I/O Multiplexing: Select (2) • int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); • Use FD_CLR(), FD_ISSET(), FD_SET(), and FD_ZERO() to manipulate the file descriptor lists • “man select” and get all the help that is needed

  21. Roadmap • How to do socket programming? • How to use hints from project 1?

  22. Read comments in the file • In client.cc, from line 53 /* create socket */ // Do DNS lookup /* Hint: use gethostbyname() */ /* set address */ /* connect socket */ /* send request */

  23. See the behavior of correct implementations • /home/hga202/EECS340/netclass-execs/http_client • /home/hga202/EECS340/netclass-execs/http_server1 • /home/hga202/EECS340/netclass-execs/http_server2 • /home/hga202/EECS340/netclass-execs/http_server3

More Related