NEW: For a prettier blog interface, see the Wordpress version!
Networking
-
Simple server. ../cs21b/net/Networking1.java is a program that
listens on port 8888 for a connection. Answer the following questions:
- How would you test this program on your local machine using telnet?
telnet 127.0.0.1 8888
Read the source code for Networking1.java and answer the following questions.
- What class do you need in order to listen for a connection? ServerSocket
- What line of code says that this program will listen on a particular port?
listen = new ServerSocket(8888);
- What line of code actually waits for another program to connect to this program?
Socket socket = listen.accept();
- If no client ever connects to this program, will the "Terminated." message ever be printed out?
No, listen.accept() will wait forever or until this program is cancelled.
- Programming. Modify Networking1 so that instead of quitting right after the first client connects and finishes, Networking1 will instead serve 3 clients, one at a time. Run your modified Networking1 server and test it using telnet.
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 } - Understanding clients
- What class do you need in order to make a connection? Socket
- What pair of functions will allow you to get the input and output of the connection?getInputStream and getOutputStream
- How do you get a PrintWriter that prints over the socket? new PrintWriter(socket.getOutputStream())
- How do you get a BufferedReader that reads from the socket? new BufferedReader(new InputStreamReader(socket.getInputStream()))
- What class do you need in order to make a connection?
- 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
- 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!
- 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.
- 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.
- 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.
- 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
- 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. - 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.
- If no client ever connects to this program, will the "Terminated." message ever be printed out?
- What line of code actually waits for another program to connect to this program?
- What class do you need in order to listen for a connection?
- How would you test this program on your local machine using telnet?
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.txtand 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.txtand 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.txtand 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.txtand 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-83For 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-1982the 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.
I'd love to hear about any questions, comments, suggestions or links that you might have. Your comments will not be posted on this website immediately, but will be e-mailed to me first. You can use this form to get in touch with me, or e-mail me at sacha@sachachua.com .