Presentation is loading. Please wait.

Presentation is loading. Please wait.

Network Server Programming Expression Evaluation Tutorial #10 CPSC 261.

Similar presentations


Presentation on theme: "Network Server Programming Expression Evaluation Tutorial #10 CPSC 261."— Presentation transcript:

1 Network Server Programming Expression Evaluation Tutorial #10 CPSC 261

2 Socket calls in sequence (TCP) Server side Socket Bind Listen Accept Recv Send Close

3 listen() Indicates that the socket is to be used for accepting incoming connections Done on the “server” side only Returns 0 on success Returns -1 on error – errno is set to indicate why

4 listen() listen(socket, backlog) socket: a file descriptor returned from socket() backlog: the number of incoming connection requests to allow to queue

5 accept() Accepts an incoming connection – waits for an incoming connection request Done on the “server” side only Returns a new socket file descriptor on success Returns -1 on error – errno is set to indicate why

6 accept() accept(socket, addr, addrlen) socket: a file descriptor returned from socket() addr: a pointer to a struct sockaddr addrlen: a pointer to the length of the addr, changed by the call

7 The general pattern s = socket(AF_INET, SOCK_STREAM, 0); bind(s,...); listen(s, 5); while ((news = accept(s, addr, &len)) >= 0) { // communicate with client via news close(news); }

8 Multiple clients at once? How do you modify the general pattern if you want to be able to talk to multiple clients at the same time? Create a thread to deal with each one Or, if isolation matters, create a process to deal with each one

9 Expression evaluation The lab requires you to evaluate strings like: – “4+20” – “34/2”

10 Useful library functions isdigit(int c) strtol(char *p, char **end, 10)

11 If you want the bonus Arithmetic expressions are defined as: – Expr ::= Term { [+|-] Term }* – Term ::= Factor { [*|/] Factor }* – Factor ::= Literal | ( Expr ) Examples: – 2+5 – 4*6 – 3+5*7 – 2+5-4*6/(7+1)

12 Parsing Parsing is the process of determining the structure of a string It can often be done using a collection of mutually recursive functions; one for each non-terminal in the grammar

13 Parsing expressions Write functions named: – Expr() – Term() – Factor() – Literal() They will share the input string – In C, this pretty much has to be in a global variable

14 Literal() long Literal() { long answer = 0; if (isdigit(*input)) { answer = strtol(input, &input, 10); } else { Error("Literal", "Expected digit"); } Trace("Literal", answer); return answer; }

15 Term() pseudocode long Term() { call Factor(); while looking at ‘*’ or ‘/’ { remember the operator and skip it call Factor(); calculate the answer } return answer; }

16 Expr() Is pretty much like Term()

17 Factor() Has to choose between Literal() and Expr() in parentheses You can decide which to call based on the first character of the input – digit -> Literal – ( -> Expr After skipping over the (


Download ppt "Network Server Programming Expression Evaluation Tutorial #10 CPSC 261."

Similar presentations


Ads by Google