Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "EECS340 Recitation 1: Very helpful to your project Hongyu Gao 1."— Presentation transcript:

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

2 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 r Email to: networkingta@gmail.com

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

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

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

6 6 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of transport service via socket API:  unreliable datagram  reliable, byte stream- oriented 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 Goal: learn how to build client/server application that communicate using sockets

7 7 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of transport service via socket API:  unreliable datagram  reliable, byte stream- oriented 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 Goal: learn how to build client/server application that communicate using sockets

8 8 source application transport network link physical HtHt HnHn M segment HtHt datagram destination application transport network link physical HtHt HnHn HlHl M HtHt HnHn M HtHt M M network link physical link physical HtHt HnHn HlHl M HtHt HnHn M HtHt HnHn M HtHt HnHn HlHl M router switch message M HtHt M HnHn frame

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

10 10 Socket programming Client side: r Create client-local TCP socket r specify IP address, port number of server process r Establish connection to server Server side (When contacted by client): r Accept the connection (automatically create a new socket) r Communicate with client through the new socket (different than the socket that it’s listening on) 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 Create a socket Bind the socket with port Listen for connections Accept new client connections Read/write to client connections Shutdown connection Corresponding functions (parameters omitted): socket(SOCK_STREAM) bind() listen() //block accept() recv(), send() close()

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

13 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; } Resolve the host struct hostent *gethostbyname( const char *hostname); /*Return nonnull pointer if OK, NULL on error */ Setup up the struct unit16_t htons(unit16_t host16bitvaule) /*Change the port number from host byte order to network byte order */ Connect connect(int socketfd, const struct sockaddr * servaddr, socket_t addrlen) /*Perform the TCP three way handshaking*/ 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*/ } 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*/ } Make the socket Socket(int family, int type, int protocol); return nonnegative value for OK, -1 for error A piece of real code (client or server?)

14 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; } 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

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

16 Standard socket vs Minet Standard function callMinet function call socket()minet_socket() bind()minet_bind() listen()minet_listen() accept()minet_accept() recv()minet_read() send()minet_write() close()minet_close() connect()minet_connect() select()minet_select() “man” every function to get help on how to use it exactly!

17 Dealing with blocking calls r Many functions block  accept(), connect(), recvfrom() r 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 r Create multi-process or multi-threaded code  Not covered r I/O multiplexing using polling  Not covered r I/O multiplexing using select ()

19 I/O Multiplexing: Select (1) r 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 r How to solve the blocking problem?  select() among the listening socket and all the opened connection

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

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

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

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


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

Similar presentations


Ads by Google