Title: Review
1Review
- In the last lecture, we discussed
- What is ISP? What is tire-1 ISP? How do tire-1
ISPs exchange data? - The layers of Internet protocols? What are each
layers for? - Packet switching vs. circuit switching
2Topics in this lecture
- Client server model
- Basic socket programming
- Project 1
3Client-server model
- Typical network app has two pieces client and
server
Client initiates contact with server (speaks
first) typically requests service from
server Server provides requested service to
client
4Client server model
- The client should know how to contact the server
means that the servers contact info is
publicly known - The server does not have to know the client
before being contacted. - After connection has been established, both sides
can send and receive
5The Web The HTTP Protocol
- hypertext transfer protocol
- Webs application layer protocol
- client/server model
- client browser that requests, receives,
displays Web objects - server Web server sends objects in response to
requests
http request
PC running Explorer
http response
http request
Server running NCSA Web server
http response
Mac running Safari
6Port Number
- The problem is, IP address specifies how to reach
a machine. But there could be many processes
running on the machine. How to make sure that I
am talking to the process I want to talk to? - Is using PID a good idea? Why?
7Port Number
- PID are not used for a number of reasons, include
- A client does not know the PID of the server
process - The server may want to provide more than one kind
of service
8Port Number
- So the solution is to use the port number. Port
number IP address - Every IP packet contains the source IP,
destination IP, source port , destination port - No two process on the same machine can have the
same port number.
9Socket
- Client and the server both write to and read from
sockets. A socket is a file descriptor. - Reference
- Unix Network Programming by Richard Stevens
- A socket will be bound to an IP address port
number - Why introducing the socket? Cant we just
communicate with the IP address and port number?
10Sockets Conceptual View
11Server
- Creates a socket by calling the socket() system
call. Nothing is done here except allocating
spaces to store the data structure - Bind the socket to an address (IP address _ port
number) using the bind() system call. - Listen for connection requests on this socket
with the listen() system call - Accept a connection with the accept() system
call. accept() returns the ID of a new socket
bound to a newly assigned port number - Send and receive data through the new socket
12Client
- Creates a socket by calling the socket() system
call. - Connect the socket to the address of the
listening socket of the server using the
connect() system call. If connect is successful,
the socket will be bound to an port number
assigned by connect(). And, the socket will be
connected to the servers newly created socket. - Send and receive data. For example, to send data
to the server can be done by calling the send()
function and with the clients socket ID as
parameter.
13Creating a socket
- int socket(int family, int service, int
protocol) - family symbolic name for protocol family
- AF_INET, AF_UNIX
- type symbolic name for type of service
- SOCK_STREAM, SOCK_DGRAM, SOCK_RAW
- protocol further info in case of raw sockets
- typically set to 0
- Returns socket descriptor
14Binding Socket with an Address
- int bind(int sd, struct sockaddr addr, int len)
- sd socket descriptor returned by socket()
- addr pointer to sockaddr structure containing
address to be bound to socket - len length of address structure
- Returns 0 if success, -1 otherwise
15Specifying Socket Address
- struct sockaddr_in
- short sin_family / set to AF_INET /
- u_short sin_port / 16 bit port number /
- struct in_addr sin_addr / 32 bit host address
/ - char sin_zero8 / not used /
-
- struct in_addr
- u_long s_addr / 32 bit host address /
16Bind Example
int sdstruct sockaddr_in ma sd
socket(AF_INET, SOCK_STREAM, 0) ma.sin_family
AF_INETma.sin_port htons(5000)ma.sin_addr.s_
addr htonl(INADDR_ANY)if (bind(sd, (struct
sockaddr ) ma, sizeof(ma)) ! -1)
17Connecting to Server
- int connect(int sd, struct sockaddr addr, int
len) - sd socket descriptor returned by socket()
- addr pointer to sockaddr structure containing
servers address (IP address and port) - len length of address structure
- Returns 0 if success, -1 otherwise
18Connect Example
int sdstruct sockaddr_in sa sd
socket(AF_INET, SOCK_STREAM, 0) sa.sin_family
AF_INETsa.sin_port htons(5000)sa.sin_addr.s_
addr inet_addr(128.101.34.78)if
(connect(sd, (struct sockaddr ) sa, sizeof(sa))
! -1)
19Connection Acceptance by Server
- int accept(int sd, struct sockaddr from, int
len) - sd socket descriptor returned by socket()
- from pointer to sockaddr structure which gets
filled with clients address - len length of address structure
- Blocks until connection requested or error
- returns a new socket descriptor on success
20Connection-oriented Server
int sd, cd, calenstruct sockaddr_in ma,
ca sd socket(AF_INET, SOCK_STREAM,
0)ma.sin_family AF_INETma.sin_port
htons(5000)ma.sin_addr.s_addr
htonl(INADDR_ANY)bind(sd, (struct sockaddr )
ma, sizeof(ma)) listen(sd, 5) calen
sizeof(ca) cd accept(sd, (struct sockaddr )
ca, calen) read and write to client treating
cd as file descriptor
21More on Socket Descriptor
- A 5-tuple associated with a connected socket
- protocol, local IP address, local port, remote
IP address, remote port - socket() fills the protocol component
- local IP address/port filled by bind()
- remote IP address/port by accept() in case of
server - in case of client both local and remote by
connect() - Complete socket is like a file descriptor
- Both send() and recv() through same socket
- accept() returns a new complete socket
- Original one can be used to accept more
connections
22Streams and Datagrams
- Connection-oriented reliable byte stream
- SOCK_STREAM based on TCP
- No message boundaries
- Multiple write() may be consumed by one read()
- Connectionless unreliable datagram
- SOCK_DGRAM based on UDP
- Message boundaries are preserved
- Each sendto() corresponds to one recvfrom()
23Input/Output Multiplexing
- You will find yourself in the situation that you
want to receive the servers message and also
want to read the users command - Recommend to use select() system call
- Process sleeps till an event happens
24Select System Call
- int select(int maxfdp1, fd_set readfds,fd_set
writefds, fd_set exceptfds,struct timeval
timeout) - maxfdp1 largest numbered file descriptor 1
- readfds check if ready for reading
- writefds check if ready for writing
- exceptfds check for exceptional conditions
- timeout specifies how long to wait for events
25Timeout in Select
- Wait indefinitely till there is an event
- Pass NULL to the timeout argument
- Dont wait beyond a fixed amount of time
- Pass pointer to a timeval structure specifying
the number of seconds and microseconds. - Just poll without blocking
- Pass pointer to a timeval structure specifying
the number of seconds and microseconds as 0
26Working with File Descriptor Set
- Set is represented by a bit mask
- Keep a descriptor in/out the set, turn on/off
corresponding bit - Using FD_ZERO, FD_SET and FD_CLR
- Use FD_ISSET to check for membership
- Example
- Make descriptors 1 and 4 members of the readset
- fd_set readset
- FD_ZERO(readset)
- FD_SET(1, readset)
- FD_SET(4, readset)
- Check if 4 is a member of readset
- FD_ISSET(4, readset)
27Return Values from Select
- Arguments readfds etc are value-result
- Pass set of descriptors you are interested in
- Select modifies the descriptor set
- Keeps the bit on if an event on the descriptor
- Turns the bit off if no event on the descriptor
- On return, test the descriptor set
- Using FD_ISSET
28Select Example
fd_set readset FD_ZERO(readset) FD_SET(0,
readset) FD_SET(4, readset) select(5,
readset, NULL, NULL, NULL) if (FD_ISSET(0,
readset) / something to be read from 0
/ if (FD_ISSET(4, readset) / something
to be read from 4 /
29Send (from http//www.opengroup.org/onlinepubs/00
0095399/functions/send.html)
- ssize_t send(int socket, const void buffer,
size_t length, int flags) - DESCRIPTION
- The send() function shall initiate transmission
of a message from the specified socket to its
peer. The send() function shall send a message
only when the socket is connected - The send() function takes the following
arguments - socket Specifies the socket file descriptor.
- buffer Points to the buffer containing the
message to send. - length Specifies the length of the message in
bytes. - flags Specifies the type of message transmission.
30recv
- ssize_t recv(int socket, void buffer, size_t
length, int flags) - DESCRIPTION
- The recv() function shall receive a message from
a connection-mode or connectionless-mode socket.
It is normally used with connected sockets
because it does not permit the application to
retrieve the source address of received data. - recv() function takes the following arguments
- socket Specifies the socket file descriptor.
- buffer Points to a buffer where the message
should be stored. - length Specifies the length in bytes of the
buffer pointed to by the buffer argument. - flags Specifies the type of message reception.
31Servers and Services
- Mapping between names and addresses (DNS)
- Host name to address gethostbyname()
- Host address to name gethostbyaddr()
32Project 1
- Goal establish basic communications between
client and server - Suppose there are totally n files in the system
and in this project n64. Each client may have
some files represented by the file vector. - Client reports to the server what file he has.
Client can also ask server who has the file he
needs.
33Client side
- Initialization
- The client will be invoked with a command line
which contains two arguments. The first argument
could be the servers IP address in dotted
decimal notation or the servers domain name. The
client has to be able to figure out which of
these two he was given. If it is given the IP
address, it calls the gethostbyaddr() function to
get the domain name and prints out to the user.
If he is given the domain name, he calls
gethostbyname() to find the servers IP address
and prints it out. - The second argument is the name of a config file
which he reads to find his id, servers listening
port, his own listening port, and his initial
file vector, which he then prints out. The config
files are different for different clients and
will be supplied by me.
34Client Side
- Connect to the server.
- Client tries to connect to the server by first
creating a socket and then calling the connect()
function. If the connection is successful, the
client prints out a success message. Otherwise he
quits. In this assignment, all connection is TCP
(SOCKET_STREAM). - Report to the server.
- He then sends the server a message with
information of his id, his listening port, and
the file vector. He does this by calling the
send() function.
35Client Side
- Wait for commands.
- He enters an infinite loop. He accepts two
commands, f and q. If the user types f, he
will ask which file the user wants and the user
will input the file index. If the user types q,
he quits He sends the server a message saying
that he has quit, then wait until server closes
the connection with him, then exits. He will use
the select() function to get user inputs and to
read message from server. If the user says that
file, say, 10, is needed, the client first checks
if he has file 10. If yes, he prints out a
message like I already have 10. Otherwise, he
sends a message to the server saying that can
you tell me who has file 10?. The server will
reply with a list of clients with file 10. Then
he prints out the list, then wait for the next
command.
36Client Side
- Auxiliary functions.
- The client also reads commands from the server.
For this project there is only one command quit.
If the server sends the client this command, the
client will close the connection by calling the
close() function and exit.
37Server side
- Initialization.
- Server creates a socket, bind it to port 5000
and all IP addresses (5000 is the so-called
well-know server listening port for our
project), and call the listen() function to wait
for connection requests. - Accept requests.
- If there is a connection request, server calls
accept() to accept the request. He then reads
the message the client is supposed to send him
which contains the clients ID, listening port,
and file vector
38Server side
- Answer client queries.
- If a client sends the server a query for a file,
server first prints out the clients ID, clients
IP address (the clients IP address can be found
when calling the accept() function and can be
later stored), and the file index the client is
asking for. Then he prints out all clients who
have the file and who have reported to him, and
send this list to the client. - Respond to user command.
- Server receives only one command from the user,
q. If the user types in this command, server
sends to all clients a quit message and waits
until all clients have closed their connections
with him, after which he will exit. - Respond to client quit message.
- If a client sends a quit message to server,
server prints out a message like (client id) at
(IP address) has quit, then close the
connection with the client. -
39Stuffs to pay attention to
- Design a good message format
- Make sure that you have read the entire message
- For debugging, use the loop back address
127.0.0.1