Project 1 Helpful Hints
Useful Java Resources
Exceptions
Some of the code that you will need to use in your program will throw Exceptions. It is your responsibility to catch those exceptions and take appropriate action. The following piece of code illustrates how you would catch an exception and print an error message to the standard output:
try {
Socket clientSocket = new Socket("hostname", 6789);
} catch (Exception exception) {
System.out.println("Exception occurred in class CLASS method METHOD");
exception.printStackTrace(); //this gives you more detailed info about the exception
}
String Manipulation
You may need to take an entire string and manipulate/analyze only certain pieces of it. There are a number of methods in the String class that will let you do this (illustrated below).
String mystring = "HELLO this is my string";
if(mystring.startsWith("HELLO", 0)) {
//this will be true since the substring starting at position 0 starts with HELLO
}
String anotherstring = mystring.substring(6); //anotherstring will be "this is my string"
Running a Client/Server Application
A few things to remember about client/server apps:
- Always start your server first.
- You can run your client and server on the same machine, or on different machines. In the client program, you specify where the server is running when you open the socket. If you are running on different machines, the "hostname" should be the IP address or machine name of the machine where the server is running. If you are running on the same machine, "hostname" can be the IP address or machine, or you can simply specify "localhost".
- Pick a large number for the port (greater than 1024). I would suggest that you do not use the default (i.e., 6789). But, remember the port specified should be the same in the client and the server.
Sami Rollins