Title: EECS340 Recitation 1: Very helpful to your project
1EECS340 Recitation 1 Very helpful to your
project
2If youre 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_at_gmail.com
3If 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
4If you have not
5Roadmap
- How to do socket programming?
- How to use hints from project 1?
6Socket 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
7Socket 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
8source
message
application transport network link physical
segment
datagram
frame
switch
destination
application transport network link physical
router
9source
application
sockets
Something (that I cont care) in between
destination
application
sockets
10Socket programming
- Server always listens (waits)
- To some socket (port) that welcomes clients
contact - Usually 80, 8000, 8080 for HTTP server
- Its always a client that initiates contact
- Contact the socket (port) that the server is
listening on
- 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 its listening on)
- Client side
- Create client-local TCP socket
- specify IP address, port number of server process
- Establish connection to server
11Server 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
12Client 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
13A 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-gt h_ addr_ list 0 - if (connect( sock, (struct sockaddr ) sin,
sizeof( sin)) ! 0) - close (sock)
- return -1
14Another 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 lt 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)) lt 0) return -1 return sock
15Client-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()
16Standard socket vs Minet
Standard function call Minet 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!
17Dealing 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
18How 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 ()
19I/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
20I/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
21Roadmap
- How to do socket programming?
- How to use hints from project 1?
22Read comments in the file
- In client.cc, from line 53
- / create socket /
- // Do DNS lookup
- / Hint use gethostbyname() /
- / set address /
- / connect socket /
- / send request /
23See 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