END; require_once "include/calendar.php"; require_once "include/planner-include.php"; require_once "include/header.inc.php"; ?>

Networking

  1. Simple server. ../cs21b/net/Networking1.java is a program that listens on port 8888 for a connection. Answer the following questions:

    Read the source code for Networking1.java and answer the following questions.

    Tip: In pseudocode, this looks like

    start out with # of clients served as 0
    while clients served is less than 5   // infinite loop
    {
         wait for a client to connect
         do something with that client program
         close that connection
         add one to the number of clients served
    }
    
  2. Understanding clients

  3. Connecting to another program. Run your modified NetworkiProgramming Write a program that connects to a running Networking1 server. Your program should read one line from the server and print it out.

    Tip: In pseudocode, this looks like

    declare a Socket connection
    create the socket connection to a specified IP address and port
    set up the input stream reader that reads from socket.getInputStream()
    set up the buffered reader that reads from the input stream reader
    read one line
    close the connection
    
  4. Try modifying your Networking2 program to listen for a connection as well. Can a program both listen for connections and make connections?
    Yes, as long as it uses ServerSockets and Sockets!

  5. Write a program that listens for connections on port 8889. When a client connects to it, your program should read a line and prints out its uppercase equivalent.

  6. Write a program that listens for connections on port 8889. When a client connects to it, your program should read a line and prints out its uppercase equivalent over the socket. Your program should repeat until the client is finished, and should repeat listening for other clients until your program is cancelled. Test it with telnet.

  7. Write a program that listens for connections on port 8890. When a client connects to it, your program should read numbers from the client. After you read each line, send the client the running total for the current transaction.

  8. Write a simple bank server. It should accept commands of the form
    create <accountname>
    deposit <accountname> <amount>
    withdraw <accountname> <amount>
    balance <accountname>
    done
    
    Your program should print out a confirmation message after each command. For example, a typical transaction with the server might go:

    balance sacha
    SACHA BALANCE ERROR NO ACCOUNT
    create sacha
    SACHA CREATE SUCCESS
    balance sacha
    SACHA BALANCE 0.0
    withdraw sacha 100
    SACHA WITHDRAW ERROR INSUFFICIENT FUNDS
    deposit sacha 150.0
    SACHA DEPOSIT SUCCESS
    withdraw sacha 50.0
    SACHA WITHDRAW SUCCESS
    balance sacha
    SACHA BALANCE 100.0
    done
    THANK YOU AND HAVE A NICE DAY
    
  9. Write a simple inventory server for the buy-and-sell application. It should accept commands of the form

    create <productname> <cost> <price>
    cost <productname> <cost>
    price <productname> <price>
    buy <productname> <quantity>
    sell <productname> <quantity> 
    deposit <amount>
    withdraw <amount>
    balance
    inventory
    
    Product names should not have spaces.

  10. Write a simple mail server. Here are the commands:
    register <username> <password>If the username is already registered, return an error. Otherwise, store that username and password and send the client an Ok message.
    send <username>If the username does not exist, report an error. If it does, read the next line and store that in the user's mailbox. Send the client an Ok message.
    receive <username> <password>If the username and password are correct, then send the client all of the messages for that user (or "No messages" if none were found). If the username and password are wrong, send the client an error message.

StringTokenizer

Words (printing out words)

Read all the lines from a file specified on the command line and print it out one word per line. Words are delimited by spaces, commas, periods, exclamation marks, colons, semicolons, and question marks.

For example, if the program was run with

java Words poem.txt
and poem.txt contained

  Shall I compare thee to a summer's day?
  Thou art more lovely and more temperate:
your program should print out

Shall
I
compare
thee
to
a
summer's
day
Thou
art
more
lovely
and
more
temperate

Sorted words (printing out words)

Read all the lines from a file specified on the command line and print it out one word per line. The words should be lower-case and alphabetically sorted. Words are delimited by spaces, commas, periods, exclamation marks, colons, semicolons, and question marks.

For example, if the program was run with

java SortedWords poem.txt
and poem.txt contained

  Shall I compare thee to a summer's day?
  Thou art more lovely and more temperate:
your program should print out

a
and
art
compare
day
i
lovely
more
more
shall
summer's
temperate
thee
thou
to

Unique words (printing out words)

Read all the lines from a file specified on the command line and print it out one word per line. The words should be lower-case and alphabetically sorted. No duplicate words should be printed. Words are delimited by spaces, commas, periods, exclamation marks, colons, semicolons, and question marks.

For example, if the program was run with

java UniqueWords poem.txt
and poem.txt contained

I think that I shall never see
A billboard lovely as a tree.
Indeed, unless the billboards fall
I'll never see a tree at all.
your program should print out

a
all
as
at
billboard
billboards
fall
i
i'll
indeed
lovely
never
see
shall
that
the
think
tree
unless

Rhyming words (printing out words)

Read all the lines from a file specified on the command line and print it out one word per line. The words should be lower-case and sorted in roughly rhyming order. Hint: reverse the words, sort the list, then reverse the words again. No duplicate words should be printed. Words are delimited by spaces, commas, periods, exclamation marks, colons, semicolons, and question marks.

For example, if the program was run with

java RhymingWords poem.txt
and poem.txt contained

I think that I shall never see
A billboard lovely as a tree.
Indeed, unless the billboards fall
I'll never see a tree at all.
your program should print out

a
indeed
billboard
tree
see
the
i
think
i'll
all
fall
shall
never
as
billboards
unless
at
that
lovely

PrintAddressBook (loading colon-separated values)

Suppose you have a file called addressbook.csv that stores text data in the following format:

name:address:mobile:landline:email:birthday(dd-mon-yyyy)
Write a program that reads from a *.csv file and prints out data labeled accordingly. Omit fields that are not specified. Output should be of the form

Name:     Juan dela Cruz
Address:  42 J.P. Rizal, Makati
Mobile:   +639178452147
Landline: 5647414
Birthday: 01-Jan-83
For example, if addressbook.csv contains

Juan dela Cruz:42 J.P. Rizal Makati:+639178452147:5647414:juan@delacruz.com:01-Jan-1983
Maria Santos:Esteban Abada::4262312:maria@somewhere.com:05-May-1982
the output should be

Name:     Juan dela Cruz
Address:  42 J.P. Rizal, Makati
Mobile:   +639178452147
Landline: 5647414
Birthday: 01-Jan-1983

Name:     Maria Santos
Address:  Esteban Abada
Landline: 4262312
Birthday: 05-May-1982

SaveAddressBook (Save colon-separated values)

Write an application that prompts the user for data and saves it into addressbook.csv following the format above.