Top Banner
4330/6310 SECOND ASSIGNMENT Summer 2015
36

4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Jan 03, 2016

Download

Documents

Debra Clark
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

4330/6310SECOND ASSIGNMENT

Summer 2015

Page 2: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Capulets and Montagues

From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other

Mayhem on Verona's plaza

Page 3: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

The answer from the authorities

Prevent Capulets and Montagues to be at the same time

Post guards at two entrances

Page 4: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

The scenario

The plazaG G

C

Page 5: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

The scenario

The plazaG G

C

Page 6: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

The scenario

The plazaG G

CC

Page 7: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

The scenario

The plazaG G

C

C

Page 8: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

The scenario

The plazaG G

C

C

M

cannot enter

Page 9: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

The scenario

The plazaG G

C

C

M

cannot enter

Page 10: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

The scenario

The plazaG G

C

C

M

can now enter

Page 11: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

YOUR PROGRAM

Two parts

Clientprogram

IPC Serverprogram

Page 12: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Overview

Will have to control access to a simulated shared resource (Verona's main plaza)

Will have to writeA 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

Page 13: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

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

Page 14: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

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

Page 15: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Server side

Server will:1. Create a socket2. Bind an address to that socket3. Set up a buffer size for that socket4. Wait for incoming calls5. Accept incoming calls (and get a new socket)6. Execute the requested function call7. Repeat steps 4 to 6

Page 16: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Phone analogy

Server will1. Get a phone2. Get a phone number3. Wait for incoming calls4. Accept incoming calls (and transfer them to a new

line)5. Listen to what client says6. Execute the requested function7. Give the answer to the client8. Wait for new incoming calls

Page 17: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

The server side

Page 18: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

High-level view (I)

When the server receives a request from a Montague who want to enter the plaza, it willAllow the request and update the Montague count if no

Capulets are presentDelay its answer otherwise

When the server receives a request from a Capulet who want to enter the plaza, it willAllow the request and update the Capulet count if no

Montagues are presentDelay its answer otherwise

Page 19: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

High-level view (II)

When the server is notified that a Montague has left the plaza, it willUpdate 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 willUpdate the Capulet count If the last Capulet has left, it will allow it all the

Montagues that were waiting

Page 20: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

The server queues

FIFO by default Two queues

Waiting CapuletsWating 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

Page 21: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

How I would do it

Have a small array in the server

Count Queue

A Capulet roams the plaza looking for Montagues

A Montague is waiting: Its return socket address is 9

1 NIL0 9 NILMontagues

Capulets

Page 22: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Communicating through sockets

Page 23: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

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)

Page 24: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

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)

Page 25: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Accept "magic" (I)

accept () was designed to implement multithreaded serversEach 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

Page 26: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Accept "magic" (II)

Could let a childprocess do the work

Page 27: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

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)

Page 28: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

TCP socket calls (IV)

gethostbyname()returns host address structure associated with a given host name

Your client and your server will both beon the same host and you will do:

gethostname(myname, MAXLEN);hp = get hostbyname(myname);

Page 29: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

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)

Page 30: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

The connect/accept handshake

For the connect/accept handshake to work,the user stub must specify thehost address (sa.sin_family)port number (sa.sin_port)

of the server in its connect( ) call

Page 31: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Bad news and good news

The bad news is that socket calls are somewhat esotericMight feel you are not fully understanding

what you are writing

The good news is most of these mysterious options are fairly standard

Page 32: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Some examples (I)

// create socket if ((s= socket(AF_INET, SOCK_STREAM, 0)) < 0) return(-1);

With datagram sockets (SOCK_DGRAM), everything would be differentNo listen( ), no accept( ), no connect( )Only sendto( ) and recvfrom( )Message boundaries would be preserved

Page 33: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Some examples (II)

gethostname(myname, MAXHOSTNAME);// get host address structurehp= gethostbyname(myname); sa.sin_family= hp->h_addrtype; // host address sa.sin_port= htons(portnum); // set port number//bind addresss to an existing socketif (bind(s,&sa,sizeof(struct sockaddr_in)) < 0) {

close(s);return(-1);

}

Page 34: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Picking a port number

Your port number should beUnique

Should not interfere with other students' programs

Greater than or equal to 1024 Lower numbers are reserved for privileged

applications

Page 35: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Some examples (III)

// set buffer size for a bound socketlisten(s, 3);

// request a connection// sa must contain address of serverif (connect(s,&sa,sizeof sa) < 0) {

close(s);return(-1);

}

Page 36: 4330/6310 SECOND ASSIGNMENT Summer 2015. Capulets and Montagues From Shakespeare's Romeo and Juliet Two rival families always trying to kill each other.

Some examples (IV)

// accept a connectionint new_s;if ((new_s = accept(s,NULL,NULL)) < 0)

return(-1)

// send a messagewrite(s, buffer, nbytes);

// read a messageread(s, buffer, nbytes)

A fixed number of bytes

The reply socket