Introduction to Client/Server Design CSCI 392 Java Programming Dr. Dannelly
Standard Client/Server Design main server process client initial handshaking request messages response messages server subprocess
Web Example Client (Web Browser) connect to port 80 of the machine send the formatted request read reply Server Child read request if file exist send file contents else send "404 not found" Web Server for(;;) wait for connection create child to handle request
HTTP message formats Request Message Reply Message GET /path/file HTTP/1.0 misc headers blank line Reply Message HTTP/1.1 200 OK Content-Length: 3731 Content-Type: text/html Last-Modified: Tue, 16 Aug 2011 21:10:01 GMT Accept-Ranges: bytes ETag: "6178b5dc585ccc1:1819e" Server: Microsoft-IIS/6.0 MicrosoftOfficeWebServer: 5.0_Pub X-Powered-By: ASP.NET Connection: close <html> <head> <title>Steve Dannelly</title>
Sample Client Code // connect to the server Socket sock = new Socket ("faculty.winthrop.edu",80); // get the reading and writing streams InputStream sin = sock.getInputStream(); BufferedReader fromServer = new BufferedReader(new InputStreamReader(sin)); OutputStream sout = sock.getOutputStream(); PrintWriter toServer = new PrintWriter (new OutputStreamWriter(sout));
see text pages 258-262 for more details Full Code Examples web1.java web2.java see text pages 258-262 for more details
Helpful String Methods Compare two strings String name1, name2; ... if ( name1.compareTo(name2) < 0 ) Append name += " "; name = first + last;
Really Useful String Methods Find a string in a larger string int firstspace = mystring.indexOf(" "); int lastspace = mystring.lastIndexOf(" "); Extracting Substrings String pre_space; pre_space=mystring.substring(0,firstspace);
example substring code // program to test substrings public class string5 { public static void main (String[] args) String first,last; String bob = "bob jones"; int space = bob.indexOf (" "); System.out.println ("Space is at " + space); first = bob.substring ( 0 , space ); last = bob.substring ( space+1 ); System.out.println ("first = |" + first + "| and last = |" + last + "|"); }