Example Client Software - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Example Client Software

Description:

... on your problem at hand without having to ever go back and change procedure ... IP Address = string. Port = integer (0 65535) ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 19
Provided by: wwwusersI
Category:

less

Transcript and Presenter's Notes

Title: Example Client Software


1
Example Client Software
  • Lecture 4

2
Start with Small Examples
  • Learning from small examples
  • Will learn
  • Atomic algorithms
  • Basic communication methods/protocols
  • Understanding of how clients and servers
    communicate
  • Reason for abstraction (even at a simple level)?
  • Although they are trivial, they are a good place
    to start

3
Hiding Details (ABSTRACTION)?
  • Need for reuse in other programs
  • Also, valuable for portability to other
    computers/operating systems (not really covered
    in this course)?
  • Ability to raise the level of the programmer by
    hiding the details of the program to FOCUS on the
    problem at hand
  • Think of how to parameterize procedures to
    maximize your ability to focus on your problem at
    hand without having to ever go back and change
    procedure

4
Example Procedure Library
  • Start simple
  • Ask yourself, what do I NEED to create a
    connection?
  • Answer
  • IP Address
  • Port
  • Protocol
  • Abstract procedure to accept these as arguments
  • What are the options for any of these arguments?
  • IP Address string
  • Port integer (0 65535)?
  • Protocol TCP/UDP (possibly a boolean, or short)?

5
Example Procedure Library
  • Build a function to meet that requirement
  • e.g.
  • connectsocket(char ip address, int port, short
    protocol)?
  • NOTE this is up to you....
  • Think about how to abstract further
  • Connect TCP
  • connectTCP(char ipaddress, int port)?
  • NOTE no protocol, that is hardcoded
  • connectTCP will simply call
  • connectsocket(ipaddress, port, TCP)?

6
connectTCP and connectsock
  • Example code on page 84 86
  • Things to note on code
  • connectsock is atomic procedure
  • accepts host, service, and transport all as
    pointers of characters
  • This allows for
  • IP address / hostname (FQDN)?
  • Service as a name or char representation of
    integer
  • Transport is either tcp or udp (case
    sensitive)?
  • connectTCP and connectUDP are what are called
    wrapper functions - just a simplified call to a
    procedure with no real functionality

7
errexit.c code
  • Page 87 code
  • int errexit(const charformat, ....)
  • va_list args
  • va_start(args, format)
  • vfprintf(stderr, format, args) / NOTE why
    stderr? /
  • va_end(args)?
  • exit(1) / NOTE why exit with 1? /

8
How to use example library
  • A few ways
  • Copy code in your source (not a good idea)?
  • Add in through include directives (an ok idea)?
  • Compile in library and link in at compile time
    (the correct way to abstract in C code)?
  • To use as a library, simply
  • compile non main code into object file
  • use -c option to only compile source to object
    file
  • use ltfilenamegt.o in compile

9
The DAYTIME Service
  • Daytime is RFC 867
  • TCP or UDP port 13
  • No specific syntax required for day / time
    representation
  • which is NOT good!
  • Does recommend
  • single line of output
  • ASCII printing characters space and CRLF

10
DAYTIME example in code
  • Page 89 in text
  • Uses connectTCP abstraction for TCPDaytime.c
  • NOTE first thing to work on for actual program
    is the USAGE of the program....
  • Know what you need to send to connectTCP
    abstraction....
  • If you do not get the correct arguments, tell
    user why (in ENGLISH words, give an example,
    reference documentation, do something, then
    exit), an error code of -6 is as useless as it
    gets!

11
TCPDaytime
  • Also note that the main code abstracts even the
    WORK of the program to a different procedure, so
    the main procedure only deals with user
    interaction
  • connectTCP will return a fully connected TCP
    socket
  • So, what if it fails? What will happen?
  • Once we have the socket, we simply need to use a
    form of read and/or write on the socket
  • NOTE Daytime does not require a write, only a
    read
  • (About as simple as it gets!)?

12
TCPDaytime read example
  • Lets look more closely at this read example
  • while (n read(s, buf, LINELEN)) gt 0 )
  • bufn '\0'
  • (void) fputs(buf, stdout)

13
TIME Service Example
  • Time protocol is RFC 868
  • Both TCP and UDP port 37
  • ntohl used to convert from Network to Host Long
  • 32bit integer long in C
  • What is EPOCH?
  • How are we converting/using it?
  • NOTE how would this be different in another OS?
  • What is the ctime function call?

14
ECHO Service
  • Echo is RFC 862
  • TCP and UDP port 7
  • NOTE different from ping (ICMP)?
  • Simply reads and writes back whatever the server
    reads

15
TCP Client for ECHO
  • page 94-96
  • Again, main procedure is for user interaction
  • Even main function will call a procedure called
    TCPecho(host,service)?

16
Lets look closer at while() loop
  • while (fgets(buf, sizeof(buf), stdin))
  • bufLINELEN '\0' / insure
    line null-terminated /
  • outchars strlen(buf)
  • (void) write(s, buf, outchars)
  • / read it back /
  • for (inchars 0 inchars lt
    outchars incharsn )
  • n read(s,
    bufinchars, outchars - inchars)
  • if (n lt 0)?
  • errexit("socket
    read failed s\n",

  • strerror(errno))
  • fputs(buf, stdout)

17
UDP Version of Echo Client
  • while (fgets(buf, sizeof(buf), stdin))
  • bufLINELEN '\0' / insure
    null-terminated /
  • nchars strlen(buf)
  • (void) write(s, buf, nchars)
  • if (read(s, buf, nchars) lt 0)?
  • errexit("socket read
    failed s\n",

  • strerror(errno))
  • fputs(buf, stdout)

18
EXERCISES
  • 7.4 add time that elapses from call to write
    for more accurate time
  • finger example
  • NOTE focus on abstractions...
  • possibly chargen
Write a Comment
User Comments (0)
About PowerShow.com