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

Download Report

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

EECS340 Recitation 1:
Very helpful to your project
Hongyu Gao
1
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: [email protected]
2
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
3
If you have not…
Turned in homework1,
……
4
Roadmap
 How to do socket programming?
 How to use hints from project 1?
5
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 streamoriented
socket
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
6
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 streamoriented
socket
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
7
source
message
M
application
Ht
M
datagram Hn Ht
M
transport
segment
frame Hl Hn Ht
M
network
link
physical
link
physical
switch
destination
M
application
Ht
M
Hn Ht
Hl Hn Ht
M
transport
M
network
link
Hn Ht
Hl Hn Ht
M
network
M
link
Hn Ht
M
physical
router
physical
8
source
application
sockets
Something
(that I con’t care)
in between
destination
application
sockets
9
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
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)
10
Server – high level view
Corresponding functions
(parameters omitted):
Create a socket
socket(SOCK_STREAM)
Bind the socket with port
bind()
Listen for connections
listen()
Accept new client connections
//block
accept()
Read/write to client connections recv(), send()
Shutdown connection
close()
Client – high level view
Corresponding functions
(parameters omitted):
Create a socket
Connect to server
socket(SOCK_STREAM)
connect()
Send HTTP request
send()
Receive HTTP response
recv()
Shutdown connection
close()
A piece of real code
(client or server?)
int connect_ socket( char *hostname, int port) {
int sock;
Ipv4 socket address structure
struct
socketaddr_in{
struct sockaddr_in sin;
Hostent
structure
uint8_t
struct hostent{ sin_len; /*length of the structure (16)*/
struct hostent *host;
sa_falimily_t
sin_family /* AF_INT*/
char * h_name
name of host*/
sock = socket( AF_ INET, SOCK_in_port_t
STREAM,
0);sin_port /*/*official
bit TCP
UDPof\
port number*/
char ** h_aliases;
/*16
pointer
ot or
array
if (sock == -1)
struct in_addr sin_addr /* 32
bit
Ipv4
address
*/
pointers to alias name*/
char
sin_zero(8)/*
unused*/
return sock;
int
h_addrtype
/* host
address type*/
} int
h_length
/* length
of address */
family , int type,
int protocol);
host = gethostbyname( hostname); Socket(int
char
** h_addr_list
/*prt
array
of ptrs
return
nonnegative value
fortoOK,
-1 for
errorwith \
if (host == NULL) {
IPv4 or IPv6 address*/
close( sock);
}
return -1;
struct
hostent
*gethostbyname(
const char *hostname);
unit16_t
htons(unit16_t
host16bitvaule)
}
/*Return
nonnull
pointer
if
OK,
NULL
error
*/ to
/*Change the port number from host on
byte
order
memset (& sin, 0, sizeof( sin));
network byte order */
connect(int socketfd, const struct sockaddr * servaddr,
sin. sin_ family = AF_ INET;
socket_t addrlen)
/*Perform the TCP three way handshaking*/
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;
}
Make the socket
Resolve the host
Setup up the struct
Connect
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)
Make the socket
return -1;
memset(& sin, 0, sizeof( sin));
sin. sin_ family = AF_ INET;
Setup up the struct
sin. sin_ addr. s_ addr = htonl( INADDR_ ANY);
sin. sin_ port = htons( port);
if (bind( sock, (struct sockaddr *) &sin, sizeof( sin)) < 0)
Bind
return -1;
return sock;
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*/
Client-server interaction
socket()
bind()
TCP Client
Socket()
connect() Connection establishment
write()
TCP Server
Well-known port
listen()
accept()
blocks until connection from client
read()
process request
read()
close()
write()
read()
close()
Standard 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!
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

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 ()
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
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
Roadmap
 How to do socket programming?
 How to use hints from project 1?
21
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 */
22
See the behavior of correct
implementations
 /home/hga202/EECS340/netclass-
execs/http_client
 /home/hga202/EECS340/netclassexecs/http_server1
 /home/hga202/EECS340/netclassexecs/http_server2
 /home/hga202/EECS340/netclassexecs/http_server3
23