Download presentation
Presentation is loading. Please wait.
Published byDebra Clark Modified over 9 years ago
1
4330/6310 SECOND ASSIGNMENT Summer 2015
2
Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other Mayhem on Verona's plaza
3
The answer from the authorities Prevent Capulets and Montagues to be at the same time Post guards at two entrances
4
The scenario The plaza GG C
5
The scenario The plaza GG C
6
The scenario The plaza GG CC
7
The scenario The plaza GG C C
8
The scenario The plaza GG C C M cannot enter
9
The scenario The plaza GG C C M cannot enter
10
The scenario The plaza GG C C M can now enter
11
YOUR PROGRAM Two parts Client program IPC Server program
12
Overview Will have to control access to a simulated shared resource (Verona's main plaza) Will have to write A client program Will fork a child for each entity trying to access the shared resource Will connect to the server to ask permission to enter and to notify it's time to leave A single-threaded server program
13
Client side To make a request to the server, a client will: 1.Create a socket 2.Connect it to the server 3.Send a request to the server 4.Wait for a reply from the server 5.Close the socket
14
Phone analogy To connect to the server, a client will: 1.Get a phone 2.Call the server using that number 3.Transmit the request in a single call Montague Aldo wants to enter the plaza Montague Aldo leaves the plaza 4.Wait for a reply 5.Hang up and go away
15
Server side Server will: 1.Create a socket 2.Bind an address to that socket 3.Set up a buffer size for that socket 4.Wait for incoming calls 5.Accept incoming calls (and get a new socket) 6.Execute the requested function call 7.Repeat steps 4 to 6
16
Phone analogy Server will 1.Get a phone 2.Get a phone number 3.Wait for incoming calls 4.Accept incoming calls (and transfer them to a new line) 5.Listen to what client says 6.Execute the requested function 7.Give the answer to the client 8.Wait for new incoming calls
17
The server side
18
High-level view (I) When the server receives a request from a Montague who want to enter the plaza, it will Allow the request and update the Montague count if no Capulets are present Delay its answer otherwise When the server receives a request from a Capulet who want to enter the plaza, it will Allow the request and update the Capulet count if no Montagues are present Delay its answer otherwise
19
High-level view (II) When the server is notified that a Montague has left the plaza, it will Update the Montague count If the last Montague has left, it will allow in all the Capulets that were waiting When the server is notified that a Capulet whas left the plaza, it will Update the Capulet count If the last Capulet has left, it will allow it all the Montagues that were waiting
20
The server queues FIFO by default Two queues Waiting Capulets Wating Montagues Each entry contains the socket address that was retuned by accept( ) when the server received the request Only clients that have to wait to enter thr plaza are in the queue
21
How I would do it Have a small array in the server CountQueue A Capulet roams the plaza looking for Montagues A Montague is waiting: Its return socket address is 9 1NIL 0 9 Montagues Capulets
22
Communicating through sockets
23
TCP socket calls (I) socket(…) creates a new socket of a given socket type (both client and server sides) bind(…) binds a socket to a socket address structure (server side) listen(…) puts a bound TCP socket into listening state (server side)
24
TCP socket calls (II) connect(…) requests a new TCP connection from the server (client side) accept(…) accepts an incoming connect request and creates a new socket associated with the socket address pair of this connection (server side)
25
Accept "magic" (I) accept () was designed to implement multithreaded servers Each time it accepts a connect request it creates a new socket to be used for the duration of that connection Can, if we want, fork a child to handle that connection Would be a bad idea this time
26
Accept "magic" (II) Could let a child process do the work
27
TCP socket calls (III) write() sends data to a remote socket (both client and server sides) read() receives data from a remote socket (both client and server sides) close() terminates a TCP connection (both client and server sides)
28
TCP socket calls (IV) gethostbyname() returns host address structure associated with a given host name Your client and your server will both be on the same host and you will do: gethostname(myname, MAXLEN); hp = get hostbyname(myname);
29
Summary Client side: csd = socket(…) connect(csd, …) write(csd, …) read(csd, …) close(csd) Server side: ssd = socket(…) bind(…) listen(…) newsd = accept(…) read(newsd, …) write(newsd, …) close(newsd)
30
The connect/accept handshake For the connect/accept handshake to work, the user stub must specify the host address (sa.sin_family) port number (sa.sin_port) of the server in its connect( ) call
31
Bad news and good news The bad news is that socket calls are somewhat esoteric Might feel you are not fully understanding what you are writing The good news is most of these mysterious options are fairly standard
32
Some examples (I) // create socket if ((s= socket(AF_INET, SOCK_STREAM, 0)) < 0) return(-1); With datagram sockets (SOCK_DGRAM), everything would be different No listen( ), no accept( ), no connect( ) Only sendto( ) and recvfrom( ) Message boundaries would be preserved
33
Some examples (II) gethostname(myname, MAXHOSTNAME); // get host address structure hp= gethostbyname(myname); sa.sin_family= hp->h_addrtype; // host address sa.sin_port= htons(portnum); // set port number //bind addresss to an existing socket if (bind(s,&sa,sizeof(struct sockaddr_in)) < 0) { close(s); return(-1); }
34
Picking a port number Your port number should be Unique Should not interfere with other students' programs Greater than or equal to 1024 Lower numbers are reserved for privileged applications
35
Some examples (III) // set buffer size for a bound socket listen(s, 3); // request a connection // sa must contain address of server if (connect(s,&sa,sizeof sa) < 0) { close(s); return(-1); }
36
Some examples (IV) // accept a connection int new_s; if ((new_s = accept(s,NULL,NULL)) < 0) return(-1) // send a message write(s, buffer, nbytes); // read a message read(s, buffer, nbytes) A fixed number of bytes The reply socket
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.