Docsity

Network Programming and Applications - Lecture Notes | CPSC 5157U, Study notes of Computer Systems Networking and Telecommunications

Partial preview of the text

Download Network Programming and Applications - Lecture Notes | CPSC 5157U and more Study notes Computer Systems Networking and Telecommunications in PDF only on Docsity!

Chapter 3: Network Programming and Applications

We begin with a very simplistic view of the Internet and network programming.

In this view, the network provides connectivity between applications.

In this view, we do not care how the Internet works, we just use it.

For many programmers, this is enough. For us, it is a starting point.

Client – Server and P2P

These examples use a Client – Server model.

Server: this application starts and then waits on communication

from the client. It responds as needed.

Client: this application starts, contacts the server, and then

requests services from that application.

P2P: Peer – to – Peer Networks are those in which two or more

application interact as “equals”.

Chat applications are really P2P. Our example code is

client – server in order to keep the example simple.

NOTE: Client–Server is a good way to write forms–based application.

The form code, as client, just validates the input & calls the server.

Basic Roles: Server and Client

The Client cannot access the Server until the Server is ready.

The Server

1. Sets up a connection and then waits on contact from some client.

2. Receives data from the client

3. Processes that data and prepares data for transmission

4. Sends data to the client.

5. Terminates the connection in response to a specific client request.

The Client

1. Establishes contact with the server.

2. Sends data to the server

3. Receives data from the server.

4. Terminates the connection when done.

The Basic Operations

Echo Server Code

/* wait for a connection from an echo client / conn = await_contact((appnum) atoi(argv[1])); if (conn < 0) exit(1); / iterate, echoing all data received until end of file */ while((len = recv(conn, buff, BUFFSIZE, 0)) > 0) (void) send(conn, buff, len, 0); send_eof(conn); return 0;

Note the “elegant” while loop. Basically, we have

len = recv(conn, buff, BUFFSIZE, 0)

while (len > 0)

Echo Client Code (Part 1)

// convert the arguments to binary format comp and appnum comp = cname_to_comp(argv[1]); if (comp == -1) exit(1); if (argc == 3) app = (appnum) atoi(argv[2]); else if ((app = appname_to_appnum("echo")) == -1) exit(1); /* form a connection with the echoserver */ Note that some C++ programmers prefer to test for equality using a construct such as if ( –1 = = comp) because (comp = – 1) always evaluates to TRUE.

Echo Client Code (Part 3)

/* read and print same no. of bytes from echo server */ expect = len; for (received = 0; received < expect;) { len = recv(conn, buff, (expect - received) < BUFFSIZE? (expect - received) : BUFFSIZE, 0); if (len < 0) { send_eof(conn); return 1; } (void) write(STDOUT_FILENO, buff, len); received += len; } (void) printf("\n"); (void) printf(INPUT_PROMPT); (void) fflush(stdout); } For the rest of the code, see the textbook.

Echo Client Code (Part 4)

Note the “elegant code”

len = recv(conn, buff, (expect - received) < BUFFSIZE? (expect - received) : BUFFSIZE, 0);

Question: Is the amount of data yet to be received enough to fill a buffer.

If so, request another buffer. If not, request what is needed.

This uses the C++ ternary expression

(expect - received) < BUFFSIZE? (expect - received) : BUFFSIZE

Somewhat equivalent to

If (expect – received) < BUFFSIZE

then length = ( expect – received) else length = BUFFSIZE

len = recv( conn, buff, length, 0)