Title: CS 241 Section Week
1- CS 241 Section Week 15(12/04/08)
2Announcements
- TA Final review
- -Tuesday December 16th 2008
- -Wednesday December 17th 2008
- (200pm - 400pm) (630pm - 830pm) (700pm
- 900pm) - (we will announce it on the newsgroup about time
and location) - No Discussion Section next week
3Outline
- MP7
- Networking Basics
- HW3
4MP7
5MP7
- Goal Build a simple HTTP web server
- Setting up the TCP sockets
- Reading the data from the server
- Sending the response to the server
- Handle multiple requests
6MP7 Tasks
- Create a socket to listen for incoming TCP
connections on a specific port. - Upon accepting a connection, launch a thread for
the incoming TCP connection - In the handler for each connection, you need to
recv() data from the socket.
7MP7 Tasks
- We give you in myhttp.h a struct called
HTTPResponse - getResponseString()
- getFileNotFoundResponseString() Notify browser
that the file is not found. - getNotImplementedResponseString() Notify browser
that you are unable to handle its request - Understand our special MP7 language file
8MP7 Tasks
- Using HTTPResponse you must now use send() to
send the contents of HTTPResponse.vptrResponse
back to the web browser - You will need to continue to recv() requests on
this socket until the web browser closes its TCP
connection with your web server
9Networking Basics
10Generic TCP Client Server Script
- Client
- socket()
- connect()
- while ()
- send()/recv()
-
- close()
- Server
- socket()
- bind()
- listen()
- while ()
- accept()
- send()/recv()
-
- close()
Lets take it one step at a time, starting with
the client
11Pseudo code
- Example 1 Simple chat TCP
12Example 1 Simple chat TCP
- Allows two users to send messages back and forth,
on the same host or different hosts
include ltstdlib.hgtinclude ltstdio.hgtinclude
ltstring.hgtinclude lterrno.hgtinclude
ltsys/socket.hgtinclude ltsys/types.hgtinclude
ltnetinet/in.hgtinclude ltunistd.hgtinclude
ltnetdb.hgt define MY_ADDR "localhost"define
MY_PORT 10000define BUFSIZE 100int main(int
argc, char argv) int sock, client_sock
struct sockaddr_in sa, client_addr char
bufferBUFSIZE int client_len sizeof
(client_addr) int msg_len 1 struct
hostent h // create socket // set
the SO_REUSEADDR option // fill in the
struct sockaddr_in data structure // Use DNS
to get IP address // bind the socket to the
port // listen on the socket while(1)
// accept client connections
// receive data // and print it
// send data // close the socket
return EXIT_SUCCESS
include ltstdlib.hgtinclude ltstdio.hgtinclude
ltstring.hgtinclude lterrno.hgtinclude
ltsys/socket.hgtinclude ltsys/types.hgtinclude
ltnetinet/in.hgtinclude ltunistd.hgtinclude
ltnetdb.hgtdefine DEST_ADDR "localhost"define
DEST_PORT 10000define BUFSIZE 100int main(int
argc, char argv) int sock struct
sockaddr_in sa struct hostent h //
create socket // fill in the struct sockaddr_in
data structure // Use DNS to get IP address //
establish a connection to the server while(1)
// send message // receive data // and
print it // close the socket return
EXIT_SUCCESS
Client
Server
13Pseudo code
- Example 1 Simple chat TCP
- Example 2 Simple chat UDP
14Example 2 Simple chat UDP
include ltstdlib.hgtinclude ltstdio.hgtinclude
ltstring.hgtinclude lterrno.hgtinclude
ltsys/socket.hgtinclude ltsys/types.hgtinclude
ltnetinet/in.hgtinclude ltunistd.hgtinclude
ltnetdb.hgt define MY_ADDR "localhost"define
MY_PORT 10000define BUFSIZE 100int main(int
argc, char argv) int sock, client_sock
struct sockaddr_in sa, client_addr char
bufferBUFSIZE int client_len sizeof
(client_addr) int msg_len 1 struct
hostent h // create socket // set
the SO_REUSEADDR option // fill in the
struct sockaddr_in data structure // Use DNS
to get IP address // bind the socket to the
port while(1) // receive data
// and print it // send data
// close the socket return
EXIT_SUCCESS
include ltstdlib.hgtinclude ltstdio.hgtinclude
ltstring.hgtinclude lterrno.hgtinclude
ltsys/socket.hgtinclude ltsys/types.hgtinclude
ltnetinet/in.hgtinclude ltunistd.hgtinclude
ltnetdb.hgtdefine DEST_ADDR "localhost"define
DEST_PORT 10000define BUFSIZE 100int main(int
argc, char argv) int sock struct
sockaddr_in sa struct hostent h
// create socket // fill in the struct
sockaddr_in data structure // Use DNS to get
IP address while(1) // send
message // receive data //
and print it // close the socket
return EXIT_SUCCESS
Client
Server
15TCP vs. UDP at a glance
- TCP UDP
- Socket type SOCK_STREAM SOCK_DGRAM
- Form of data transmitted Stream Packets
- Calls for sending and receiving send,
recv sendto, recvfrom - Uses sessions? Yes No
- Overhead for ordering packets Substantial Minima
l - Example Services FTP, HTTP DNS, SNMP
16send and sendto
- int send(int socket, const void msg, int len,
int flags) - int sendto(int socket, const void msg, int len,
int flags, const struct sockaddr to, socklen_t
tolen) - send sends along an established connection (TCP),
while sendto sends to an address (UDP). - The extra two parameters specify the destination.
17recv and recvfrom
- int recv(int socket, const void msg, int len,
int flags) - int recvfrom(int socket, const void msg, int
len, int flags, const struct sockaddr from,
socklen_t fromlen) - recv receives from an established connection
(TCP), while recvfrom receives from anywhere
(UDP), and saves the address. - The extra two parameters specify the source.
18Pseudo code
- Example 1 Simple chat TCP
- Example 2 Simple chat UDP
- Example 3 Simple chat with threads
19Example 3 Simple chat with threads
- The chat programs assume alternating send/receive
communication. Thus the output is counter
intuitive if the users deviate from this.
include ltstdlib.hgtinclude ltstdio.hgtinclude
ltstring.hgtinclude lterrno.hgtinclude
ltsys/socket.hgtinclude ltsys/types.hgtinclude
ltnetinet/in.hgtinclude ltunistd.hgtinclude
ltnetdb.hgtinclude ltpthread.hgtdefine MY_ADDR
"localhost"define MY_PORT 10000define BUFSIZE
100// Thread for sendingvoid send_thr(void
data) while(1) // Send data //
Thread for receivingvoid recv_thr(void data)
while(1) // receive data int
main(int argc, char argv) int sock,
client_sock struct sockaddr_in sa,
client_addr int true 1 struct
hostent h int client_len sizeof
(client_addr) pthread_t t1, t2 //
create a socket, same as with client // set
the SO_REUSEADDR option // fill in the
struct sockaddr_in data structure // Use DNS
to get IP address // bind the socket to the
port // listen on the socket // accept
client connections // Create threads
// Wait for threads return EXIT_SUCCESS
include ltstdlib.hgtinclude ltstdio.hgtinclude
ltstring.hgtinclude lterrno.hgtinclude
ltsys/socket.hgtinclude ltsys/types.hgtinclude
ltnetinet/in.hgtinclude ltunistd.hgtinclude
ltnetdb.hgtinclude ltpthread.hgtdefine DEST_ADDR
"localhost"define DEST_PORT 10000define
BUFSIZE 100// Thread for sendingvoid
send_thr(void data) while(1) // Send
data // Thread for receivingvoid
recv_thr(void data) while(1) //
receive data int main(int argc, char
argv) int sock struct sockaddr_in
sa int len struct hostent h
pthread_t t1, t2 // create a socket
// fill in the struct sockaddr_in data structure
// establish a connection to the server
// Create threads // Wait for threads
return EXIT_SUCCESS
Client
Server
20Homework 3
21Question 1 (Signals and Timers)
22(No Transcript)
23Question 2 (I/O Devices)
24(No Transcript)
25(No Transcript)
26(No Transcript)
27(No Transcript)
28(No Transcript)
29(No Transcript)
30(No Transcript)
31Question 3 (Memory Management)
32(No Transcript)
33(No Transcript)
34(No Transcript)
35(No Transcript)
36(No Transcript)
37(No Transcript)
38(No Transcript)
39Question 4 (File Systems)
40(No Transcript)
41(No Transcript)
42(No Transcript)
43(No Transcript)
44Question 5 (Networking)
45(No Transcript)
46(No Transcript)
47(No Transcript)