1 ch 3. network programming. 2 network programming (1) network allows arbitrary applications to...

21
1 Ch 3. Network Programming

Upload: barnaby-kelley

Post on 16-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

1

Ch 3. Network Programming

Page 2: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

2

Network Programming (1)Network Programming (1)Network allows arbitrary applications to

communicateE.g., client-server computing such as WEB

browsingGUIFunctions supporteddata transmission via underlying network

Page 3: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

3

Network Programming (2)Network Programming (2)A programmer can create Internet application

software without understanding the underlying network technology

Network facilities are accessed through an Application Program Interface (API)

Page 4: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

4

Page 5: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

5

Page 6: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

6

Echo serverEcho serverUseful for network testingServer returns exact copy of data sentServer: Bind port numberClient: Assign server IP (host) and port number (AP)

LAN

ServerClient

> echoserver 22000

> echoclient X 22000

Page 7: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

7

Page 8: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

8

A Intuitive Look at the APIA Intuitive Look at the API

Server

await_contact()

recv()

send()

send_eof()

Client

make_contact()

send()

recv()

send_eof()

Page 9: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

9

Exercise: Files neededExercise: Files needed

CNAI API code readln.csend_eof.cmake_contact.ccname_to_comp.ccnaiapi_init.cawait_contact.cappname_to_appnum.c

Header filescnaiapi.hcnaiapi_win32.h

C source codeechoserver.cechoclient.c

Readme.txt

Page 10: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

10

Build net programs under Windows (1)

Build net programs under Windows (1)

Have cnaiapi.lib available[File][New][Win32 Static Library]Replace #include <xxx.h> with #include “xxx.h“

in .c and .h files[Project][Add to project][Fies]

Seven .c files[Build][Build cnaiapi.lib]

Page 11: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

11

Build net programs under Windows (2)

Build net programs under Windows (2)

Include “cnaiapi.h” in each of the C program files that use CNAI API library calls

Link cnaiapi.lib into the program [project] [settings] [Link tag] [general category] including 'cnaiapi.lib‘ in the Object/Library Modulessetting the Additional Library Path in the Input Cate.

link to the WinSock 2 library (ws2_32.lib)

Page 12: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

12

Appendix

Page 13: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

13

Page 14: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

14

A Intuitive Look at the APIA Intuitive Look at the APIServer Client

socket() socket()// open a TCP socket

bind()

listen()

accept()

read()

write()

connect()

// bind port number assign server IP and port number

// connect to the specific // server

write()

read()

// blocking mode

closes () close ()

Page 15: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

15

Page 16: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

16

A Intuitive Look at the APIA Intuitive Look at the API

Server

await_contact()

recv()

send()

send_eof()

Client

make_contact()

send()

recv()

send_eof()

Page 17: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

17

Echo clientEcho client /* echoclient.c */ #include <stdlib.h> #include <stdio.h> #include <cnaiapi.h> #define BUFFSIZE 256 #define INPUT_PROMPT "Input > " #define RECEIVED_PROMPT "Received> " int readln(char *, int); /*----------------------------------------------------------------------- * * Program: echoclient * Purpose: contact echoserver, send user input and print server response * Usage: echoclient <compname> [appnum] * Note: Appnum is optional. If not specified the standard echo appnum * (7) is used. * *----------------------------------------------------------------------- */ int main(int argc, char *argv[]) { computer comp; appnum app; connection conn; char buff[BUFFSIZE]; int expect, received, len; if (argc < 2 || argc > 3) { (void) fprintf(stderr, "usage: %s <compname> [appnum]\n", argv[0]); exit(1); }

Page 18: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

18

/* 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 */ conn = make_contact(comp, app); if (conn < 0) exit(1); (void) printf(INPUT_PROMPT); (void) fflush(stdout);

Page 19: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

19

/* iterate: read input from the user, send to the server, */ /* receive reply from the server, and display for user */ while((len = readln(buff, BUFFSIZE)) > 0) { /* send the input to the echoserver */ (void) send(conn, buff, len, 0); (void) printf(RECEIVED_PROMPT); (void) fflush(stdout); /* 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); } /* iteration ends when EOF found on stdin */ (void) send_eof(conn); (void) printf("\n"); return 0; }

Page 20: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

20

Page 21: 1 Ch 3. Network Programming. 2 Network Programming (1)  Network allows arbitrary applications to communicate E.g., client-server computing such as WEB

21

A Intuitive Look at the APIA Intuitive Look at the API

Server

await_contact()

recv()

send()

send_eof()

Client

make_contact()

send()

recv()

send_eof()