Network Programming and Internet Applications - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

Network Programming and Internet Applications

Description:

HTTP/1.0 status status_string CRLF. Server: CNAI Demo Server CRLF. Content-Length: datasize CRLF. Content-Type: text/html CRLF. CRLF. 53. The end of Chapter 3 ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 56
Provided by: hpcus525
Category:

less

Transcript and Presenter's Notes

Title: Network Programming and Internet Applications


1
  • Chapter 3 4
  • Network Programming and Internet Applications

2
Topics
  • Network communication
  • client-server computing
  • communication paradigm
  • an example application program interface
  • an intuitive look at the API
  • definition of the API
  • code for an echo application
  • code for a chat application
  • code for a web application

3
Introduction
  • An important idea
  • A programmer can create Internet application
    software without understanding the underlying
    network technology or communication protocols.
  • The key using libraries

4
Network Communication
  • Network data transfer without interpreting them
  • Applications data processing and exchange
  • doing it in pairs, both must know data format and
    meaning, e.g.,
  • one side database services
  • another side send requests

5
Client-Server computing
  • How the pairs know the location of each other?
  • Server starts first and waits for the other
    application to contact it
  • Client must know the location of the server and
    contact it.

6
Specify the server
  • The location of the server is given by a pair of
    identifiers
  • (computer, application)
  • The client can use alphabetic names as
    identifiers
  • The network only knows binary numbers
  • Network software does the translation

7
Communication paradigm
  • Sequence of operations
  • The server application starts first, and waits
    for contact from a client
  • The client contacts the server by specifying its
    location and requesting communication
  • The client and server exchange messages
  • After they finish sending data, the client and
    server each send an end-of-file to terminate
    communication

8
An example Application Program Interface (API)
  • API the set of operations available to an
    application programmer
  • API specifies the arguments for each operation as
    well as the semantics

9
The API for network programming
10
An intuitive look at the API
11
Definition of the API
  • Data types used

12
Functions defined by the API (1)
  • connection await_contact(appnum a)
  • connection make_contact(computer c, appnum a)
  • appnum appname_to_appnum(char a)
  • computer cname_to_comp(char c)

13
Functions defined by the API (2)
  • int send(connection con, char buffer, int
    length, int flags)
  • con The connection identifier
  • buffer Data buffer
  • length Number of bytes to send.
  • flag 0
  • return length if successful negative
  • otherwise

14
Functions defined by the API (3)
  • int recv(connection con, char buffer, int
    length, int flags)
  • Block until data arrives.
  • con The connection identifier
  • buffer Data buffer
  • length Buffer size.
  • return of bytes in the buffer
  • 0 if receives EOF
  • negative otherwise

15
Functions defined by the API (4)
  • int recvln(connection con, char buffer, int
    length)
  • int send_eof(connection con)

16
Summary of API types
17
Code for an echo application
  • Server echoes back all the data it receives
  • Client repeatedly does the following
  • prompts the user for a line of input
  • sends the line to the server
  • displays whatever the server sends back

18
Where the client and the server reside?
19
How to run the programs?
  • invoke the server on computer cms07201.uhd.edu
  • gt echoserver 20000
  • invoke the client
  • gt echoclient cms07201.uhd.edu

20
Example echo server code (1)
  • / echoserver.c /
  • include ltstdlib.hgt
  • include ltstdio.hgt
  • include ltcnaiapi.hgt
  • define BUFFSIZE 256
  • /------------------------------------------------
    ------------
  • Program echoserver
  • Purpose wait for a connection from an
    echoclient and echo data
  • Usage echoserver ltappnumgt
  • ------------------------------------------------
    ------------
  • /

21
Example echo server code (2)
  • int
  • main(int argc, char argv)
  • connection conn
  • int len
  • char buffBUFFSIZE
  • if (argc ! 2)
  • (void) fprintf(stderr, "usage s ltappnumgt\n",
  • argv0)
  • exit(1)
  • / wait for a connection from an echo client /
  • conn await_contact((appnum) atoi(argv1))
  • if (conn lt 0)
  • exit(1)

22
Example echo server code (3)
  • / iterate, echoing all data received until end
    of file /
  • while((len recv(conn, buff, BUFFSIZE, 0)) gt 0)
  • (void) send(conn, buff, len, 0)
  • send_eof(conn)
  • return 0

23
Example echo client code (1)
  • / echoclient.c /
  • include ltstdlib.hgt
  • include ltstdio.hgt
  • include ltcnaiapi.hgt
  • define BUFFSIZE 256
  • define INPUT_PROMPT "Input gt "
  • define RECEIVED_PROMPT "Receivedgt "
  • int readln(char , int)
  • /------------------------------------------------
    ------------
  • Program echoclient
  • Purpose contact echoserver, send user input
    and print server response
  • Usage echoclient ltcompnamegt appnum
  • Note Appnum is optional. If not specified
    the standard echo appnum
  • (7) is used.

24
Example echo client code (2)
  • int
  • main(int argc, char argv)
  • computer comp
  • appnum app
  • connection conn
  • char buffBUFFSIZE
  • int expect, received, len
  • if (argc lt 2 argc gt 3)
  • (void) fprintf(stderr,
  • "usage s ltcompnamegt appnum\n", argv0)
  • exit(1)
  • / convert the arguments to binary format comp
    and appnum /
  • comp cname_to_comp(argv1)
  • if (comp -1)

25
Example echo client code (3)
  • if (argc 3)
  • app (appnum) atoi(argv2)
  • else
  • if ((app appname_to_appnum("echo")) -1)
  • exit(1)
  • / form a connection with the echoserver /
  • conn make_contact(comp, app)
  • if (conn lt 0)
  • exit(1)
  • (void) printf(INPUT_PROMPT)
  • (void) fflush(stdout)

26
Example echo client code (4)
  • / iterate read input from the user, send to
    the server, /
  • / receive reply from the server, and
    display for user /
  • while((len readln(buff, BUFFSIZE)) gt 0)
  • / send the input to the echoserver /
  • (void) send(conn, buff, len, 0)
  • (void) printf(RECEIVED_PROMPT)
  • (void) fflush(stdout)

27
Example echo client code (5)
  • / read and print same no. of bytes from echo
    server
  • /
  • expect len
  • for (received 0 received lt expect)
  • len recv(conn, buff, (expect - received) lt
  • BUFFSIZE ? (expect - received)
  • BUFFSIZE, 0)
  • if (len lt 0)
  • send_eof(conn)
  • return 1
  • (void) write(STDOUT_FILENO, buff, len)
  • received len
  • (void) printf("\n")
  • (void) printf(INPUT_PROMPT)
  • (void) fflush(stdout)

28
Example echo client code (6)
  • / iteration ends when EOF found on stdin /
  • (void) send_eof(conn)
  • (void) printf("\n")
  • return 0

29
Code for a chat application
  • A simplified version of chat
  • works between a single pair of users
  • client and server take turns entering text
    client starts
  • Both client and server enter a loop of reading
    and sending local users input and displaying the
    message from the other side
  • Both client and server proceed until they receive
    end-of-file

30
To run the aaplication
  • Server
  • chatserver 25000
  • Client
  • chatclient cms70201.uhd.edu 25000

31
Example chat server code (1)
  • / chatserver.c /
  • include ltstdlib.hgt
  • include ltstdio.hgt
  • include ltcnaiapi.hgt
  • define BUFFSIZE 256
  • define INPUT_PROMPT "Input gt "
  • define RECEIVED_PROMPT "Receivedgt "
  • int recvln(connection, char , int)
  • int readln(char , int)
  • /------------------------------------------------
    ------------
  • Program chatserver
  • Purpose wait for a connection from a
    chatclient allow users to chat
  • Usage chatserver ltappnumgt

32
Example chat server code (2)
  • int
  • main(int argc, char argv)
  • connection conn
  • int len
  • char buffBUFFSIZE
  • if (argc ! 2)
  • (void) fprintf(stderr, "usage s ltappnumgt\n",
  • argv0)
  • exit(1)
  • (void) printf("Chat Server Waiting For
    Connection.\n")
  • / wait for a connection from a chatclient /
  • conn await_contact((appnum) atoi(argv1))
  • if (conn lt 0)

33
Example chat server code (3)
  • / iterate, reading from the client and the
    local user /
  • while((len recvln(conn, buff, BUFFSIZE)) gt 0)
  • (void) printf(RECEIVED_PROMPT)
  • (void) fflush(stdout)
  • (void) write(STDOUT_FILENO, buff, len)
  • / send a line to the chatclient /
  • (void) printf(INPUT_PROMPT)
  • (void) fflush(stdout)
  • if ((len readln(buff, BUFFSIZE)) lt 1)
  • break
  • bufflen - 1 '\n'
  • (void) send(conn, buff, len, 0)
  • / iteration ends when EOF found on stdin or
    chat connection /

34
Example chat client code (1)
  • / chatclient.c /
  • include ltstdlib.hgt
  • include ltstdio.hgt
  • include ltcnaiapi.hgt
  • define BUFFSIZE 256
  • define INPUT_PROMPT "Input gt "
  • define RECEIVED_PROMPT "Receivedgt "
  • int recvln(connection, char , int)
  • int readln(char , int)
  • /------------------------------------------------
    ------------
  • Program chatclient
  • Purpose contact a chatserver and allow users
    to chat
  • Usage chatclient ltcompnamegt ltappnumgt

35
Example chat client code (2)
  • int
  • main(int argc, char argv)
  • computer comp
  • connection conn
  • char buffBUFFSIZE
  • int len
  • if (argc ! 3)
  • (void) fprintf(stderr,
  • "usage s ltcompnamegt ltappnumgt\n",
  • argv0)
  • exit(1)
  • / convert the compname to binary form comp /
  • comp cname_to_comp(argv1)
  • if (comp -1)

36
Example chat client code (3)
  • / make a connection to the chatserver /
  • conn make_contact(comp, (appnum)
    atoi(argv2))
  • if (conn lt 0)
  • exit(1)
  • (void) printf("Chat Connection Established.\n")
  • (void) printf(INPUT_PROMPT)
  • (void) fflush(stdout)

37
Example chat client code (4)
  • / iterate, reading from local user and then
    from chatserver /
  • while((len readln(buff, BUFFSIZE)) gt 0)
  • bufflen - 1 '\n'
  • (void) send(conn, buff, len, 0)
  • / receive and print a line from the chatserver
    /
  • if ((len recvln(conn, buff, BUFFSIZE)) lt 1)
  • break
  • (void) printf(RECEIVED_PROMPT)
  • (void) fflush(stdout)
  • (void) write(STDOUT_FILENO, buff, len)
  • (void) printf(INPUT_PROMPT)
  • (void) fflush(stdout)
  • / iteration ends when stdin or the connection
    indicates EOF /

38
Code for a web application
  • Simplifications
  • the server only supplies three web pages
  • none of the pages contains anything except text
  • pages are hard-wired into the code
  • the client does not format the web pages

39
Use standard protocol
  • The web server can communicate with a
    conventional web browser, e.g., enter the
    following in IE
  • http//cms70201.uhd.edu27000/index.html
  • The web client can inter-operate with a
    commercial web server, e.g.,
  • webclient www.uhd.edu /

40
To run the applications
  • To run the server
  • webserver 27000
  • To run the client
  • webclient cms70201.uhd.edu /index.html 27000

41
Example web client code (1)
  • / webclient.c /
  • include ltstdlib.hgt
  • include ltstdio.hgt
  • include ltcnaiapi.hgt
  • define BUFFSIZE 256
  • /------------------------------------------------
    ------------
  • Program webclient
  • Purpose fetch page from webserver and dump to
    stdout with headers
  • Usage webclient ltcompnamegt ltpathgt appnum
  • Note Appnum is optional. If not specified
    the standard www appnum (80) is used.
  • ------------------------------------------------
    ------------
  • /

42
Example web client code (2)
  • int
  • main(int argc, char argv)
  • computer comp
  • appnum app
  • connection conn
  • char buffBUFFSIZE
  • int len
  • if (argc lt 3 argc gt 4)
  • (void) fprintf(stderr, "sss", "usage ",
    argv0,
  • " ltcompanegt ltpathgt appnum\n")
  • exit(1)
  • / convert arguments to binary computer and
    appnum /
  • comp cname_to_comp(argv1)

43
Example web client code (3)
  • if (argc 4)
  • app (appnum) atoi(argv3)
  • else
  • if ((app appname_to_appnum("www")) -1)
  • exit(1)
  • / contact the web server /
  • conn make_contact(comp, app)
  • if (conn lt 0)
  • exit(1)
  • / send an HTTP/1.0 request to the webserver /
  • len sprintf(buff, "GET s HTTP/1.0\r\n\r\n",
    argv2)
  • (void) send(conn, buff, len, 0)

44
Example web client code (4)
  • / dump all data received from the server to
    stdout /
  • while((len recv(conn, buff, BUFFSIZE, 0)) gt 0)
  • (void) write(STDOUT_FILENO, buff, len)
  • return 0

45
Example web server code (1)
  • / webserver.c /
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • include lttime.hgt
  • include ltcnaiapi.hgt
  • if defined(LINUX) defined(SOLARIS)
  • include ltsys/time.hgt
  • endif
  • define BUFFSIZE 256
  • define SERVER_NAME "CNAI Demo Web Server"
  • define ERROR_400 "ltheadgtlt/headgtltbodygtlthtmlgtlth1gtEr
    ror 400lt/h1gtltpgtThe server couldn't understand
    your request.lt/htmlgtlt/bodygt\n"
  • define ERROR_404 "ltheadgtlt/headgtltbodygtlthtmlgtlth1gtEr
    ror 404lt/h1gtltpgtDo\
  • cument not found.lt/htmlgtlt/bodygt\n"

46
Example web server code (2)
  • define HOME_PAGE "ltheadgtlt/headgtltbodygtlthtmlgtlth1gtWe
    lcome to the CNAI Demo Serverlt/h1gtltpgtWhy not
    visit ltulgtltligtlta href\"http//netbook.cs.purdue.
    edu\"gtNetbook Home Pagelt/agtltligtlta
    href\"http//www.comerbooks.com\"gtComer Books
    Home Pageltagtlt/ulgtlt/htmlgtlt/bodygt\n"
  • define TIME_PAGE "ltheadgtlt/headgtltbodygtlthtmlgtlth1gtTh
    e current date is slt/h1gtlt/htmlgtlt/bodygt\n"
  • int recvln(connection, char , int)
  • void send_head(connection, int, int)
  • /------------------------------------------------
    ------------
  • Program webserver
  • Purpose serve hard-coded webpages to web
    clients
  • Usage webserver ltappnumgt
  • ------------------------------------------------
    ------------
  • /

47
Example web server code (3)
  • int
  • main(int argc, char argv)
  • connection conn
  • int n
  • char buffBUFFSIZE, cmd16, path64,
    vers16
  • char timestr
  • if defined(LINUX) defined(SOLARIS)
  • struct timeval tv
  • elif defined(WIN32)
  • time_t tv
  • endif
  • if (argc ! 2)
  • (void) fprintf(stderr, "usage s ltappnumgt\n",
    argv0)
  • exit(1)

48
Example web server code (4)
  • while(1)
  • / wait for contact from a client on specified
    appnum
  • /
  • conn await_contact((appnum) atoi(argv1))
  • if (conn lt 0)
  • exit(1)
  • / read and parse the request line /
  • n recvln(conn, buff, BUFFSIZE)
  • sscanf(buff, "s s s", cmd, path, vers)
  • / skip all headers - read until we get \r\n
    alone /
  • while((n recvln(conn, buff, BUFFSIZE)) gt 0)
  • if (n 2 buff0 '\r' buff1
  • '\n')

49
Example web server code (5)
  • / check for unexpected end of file /
  • if (n lt 1)
  • (void) send_eof(conn)
  • continue
  • / check for a request that we cannot
    understand /
  • if (strcmp(cmd, "GET") (strcmp(vers,
    "HTTP/1.0")
  • strcmp(vers, "HTTP/1.1")))
  • send_head(conn, 400, strlen(ERROR_400))
  • (void) send(conn, ERROR_400,
  • strlen(ERROR_400),0)
  • (void) send_eof(conn)
  • continue

50
Example web server code (6)
  • / send the requested web page or a "not found"
    error
  • /
  • if (strcmp(path, "/") 0)
  • send_head(conn, 200, strlen(HOME_PAGE))
  • (void) send(conn, HOME_PAGE,
  • strlen(HOME_PAGE),0)
  • else if (strcmp(path, "/time") 0)
  • if defined(LINUX) defined(SOLARIS)
  • gettimeofday(tv, NULL)
  • timestr ctime(tv.tv_sec)
  • elif defined(WIN32)
  • time(tv)
  • timestr ctime(tv)
  • endif
  • (void) sprintf(buff, TIME_PAGE, timestr)
  • send_head(conn, 200, strlen(buff))
  • (void) send(conn, buff, strlen(buff), 0)
  • else / not found /

51
Example web server code (7)
  • /------------------------------------------------
    ------------
  • send_head - send an HTTP 1.0 header with given
    status and content-len
  • ------------------------------------------------
    ------------
  • /
  • void
  • send_head(connection conn, int stat, int len)
  • char statstr, buffBUFFSIZE
  • / convert the status code to a string /
  • switch(stat)
  • case 200
  • statstr "OK"
  • break
  • case 400
  • statstr "Bad Request"
  • break
  • case 404

52
Example web server code (8)
  • default
  • statstr "Unknown"
  • break
  • /
  • send an HTTP/1.0 response with Server,
    Content-Length,
  • and Content-Type headers.
  • /
  • (void) sprintf(buff, "HTTP/1.0 d s\r\n", stat,
    statstr)
  • (void) send(conn, buff, strlen(buff), 0)
  • (void) sprintf(buff, "Server s\r\n",
    SERVER_NAME)
  • (void) send(conn, buff, strlen(buff), 0)
  • (void) sprintf(buff, "Content-Length d\r\n",
    len)
  • (void) send(conn, buff, strlen(buff), 0)

53
Example web server code (9)
  • (void) sprintf(buff, "\r\n")
  • (void) send(conn, buff, strlen(buff), 0)

54
The format of HTTP header
  • HTTP/1.0 status status_string CRLF
  • Server CNAI Demo Server CRLF
  • Content-Length datasize CRLF
  • Content-Type text/html CRLF
  • CRLF

55
The end of Chapter 3 4
Write a Comment
User Comments (0)
About PowerShow.com