EECS340 Recitation 1: Very helpful to your project - PowerPoint PPT Presentation

About This Presentation
Title:

EECS340 Recitation 1: Very helpful to your project

Description:

Title: 3rd Edition: Chapter 2 Author: Jim Kurose and Keith Ross Last modified by: hongyu Created Date: 10/8/1999 7:08:27 PM Document presentation format – PowerPoint PPT presentation

Number of Views:111
Avg rating:3.0/5.0
Slides: 24
Provided by: JimKurosea221
Category:

less

Transcript and Presenter's Notes

Title: EECS340 Recitation 1: Very helpful to your project


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

2
If 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

3
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

4
If you have not
  • Turned in homework1,

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

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 stream-oriented

7
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 stream-oriented

8
source
message
application transport network link physical
segment
datagram
frame
switch
destination
application transport network link physical
router
9
source
application
sockets
Something (that I cont care) in between
destination
application
sockets
10
Socket 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

11
Server 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
12
Client 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
13
A 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

14
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 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
15
Client-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()
16
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!
17
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

18
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 ()

19
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

20
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

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

22
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 /

23
See 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
Write a Comment
User Comments (0)
About PowerShow.com