














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A commentated java code for a client-server programming assignment from the cs 580 course at san diego state university, spring semester 2007. The comments explain various parts of the code, such as the purpose of each method, the role of different variables, and best practices. The code demonstrates how to create a socket connection to a server, send and receive messages, and handle exceptions.
Typology: Assignments
1 / 22
This page cannot be seen from the preview
Don't miss anything!















public void sendMessage(String message) { String host = "bismarck.sdsu.edu"; int port = 8009;
private String send(String text) { try { // creates a stream socket and connects it to the server Socket connection = new Socket(strServer, iPort); //getOutputStream will return an output stream for this socket OutputStream rawOut = connection.getOutputStream(); //OutputStream is super class to BufferedOutputStream. PrintStream creates a new // print stream. //BufferedOutputStream creates a new buffered output stream to write data to the // specified output stream PrintStream out = new PrintStream(new BufferedOutputStream(rawOut)); //getInputStream returns an inputstream for this socket InputStream rawIn = connection.getInputStream(); //BufferedReader creates a buffering character-input stream tht uses a default input // buffer //InputStreamReader creates an InputStreamReader that uses the default-size input // buffer BufferedReader in = new BufferedReader(new InputStreamReader(rawIn)); out.print(text); //flush() will flush the stream out.flush();
private String send(String text) { try { Socket connection = new Socket(strServer, iPort); OutputStream rawOut = connection.getOutputStream(); PrintStream out = new PrintStream(new BufferedOutputStream(rawOut)); InputStream rawIn = connection.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(rawIn)); out.print(text); out.flush();
public class Client { public Client(String serverAddress, int portNumber) { Improved names reduce the need for the given comments
public class Client { /*
public class Client { public String add() { System.out.print("Type name to add to server"); String name = Console.readLine(); Socket connection = new Socket(strServer, iPort); OutputStream rawOut = connection.getOutputStream(); PrintStream out = new PrintStream(new BufferedOutputStream(rawOut)); InputStream rawIn = connection.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(rawIn)); out.print("add " + name + ';'); out.flush(); etc. Just Say No
UI & Domain Code Perform different tasks Change at different rates No need to tie doman code to particular UI Put UI code in separate layer Keep Domain code ignorant of UI code
pubic class ClientTUI { private Client voter; private static ENDLINE = System.getProperty("line.separator"); string mainMenu() { return "Select one of the following options:" + ENDLINE + "1) add" + ENDLINE + "2) list" + ENDLINE + "3) vote" + ENDLINE + "4) result" + ENDLINE + "5) quit"; } void add() { name = Console.readLine ("Enter a name to add"); String result = voter.add( name); Console.println(" The result: " + result); } public void run() { while (true) { int method = Console.readInt( mainMenu()); switch (method) { case: 1: add(); break; case: 2: list(); break; etc.
Build some structure that can be reused
Avoid readline (what is a line?) Does not work with protocol on server
private String serverResponse(Reader fromServer) { StringBuffer response = new StringBuffer(); boolean hasMoreChars = true; int next = 0; while (hasMoreChars) { next = fromServer.read(); if (-1 == next) hasMoreChars = false; else if(';' ==(char) next) hasMoreChars = false; else response.append((char) currentChar); } return response.toString(); } What role does the flag play
private String serverResponse(Reader fromServer) { StringBuffer response = new StringBuffer(); int next = 0; while (true) { next = fromServer.read(); if (isEOF(next)) return response.toString(); else if (isEndOfMessage(next)) return response.toString(); else response.append((char) next); } }