Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 9th, 2012  |  syntax: None  |  size: 2.53 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ##client thread
  2.  
  3. public ClientSocketSrvHandler(Socket socket, ClientSocket client) throws IOException {
  4.                 this.socket = socket;
  5.                 this.client = client;
  6.                 bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  7.                 //bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  8.         }
  9.  
  10.  
  11. public void run() {
  12.                 while (true) {
  13.                         try {
  14.                                 response = bufferedReader.readLine();
  15.                                 if (response.startsWith("@broadcast")) {
  16.                                         System.out.println("broadcast received");
  17.                                 }
  18.                         } catch (IOException e) {
  19.                                 // TODO Auto-generated catch block
  20.                                 e.printStackTrace();
  21.                         }
  22.                 }
  23.         }
  24.        
  25.         public String getResponse() throws InterruptedException, IOException {
  26.                 //this.client.wait();
  27.                 //response = bufferedReader.readLine();
  28.                 if (response == null) {
  29.                         System.out.println("no answer.");
  30.                 }
  31.                 else {
  32.                         System.out.println("srv thread - message received: " + response);
  33.                         //this.client.notify();
  34.                         return response;
  35.                 }
  36.                 return "";
  37.         }
  38.  
  39. ##main client
  40. public ClientSocket(String host, int port) throws UnknownHostException, IOException {
  41.                 this.host = host;
  42.                 this.port = port;
  43.                 System.out.println("Connecting to server. Host: " + host + " - port: " + port);
  44.                 socket = new Socket(host, port);
  45.                 System.out.println("Connected.");
  46.                 //bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  47.                 bufferedWriter = new BufferedWriter(
  48.                                 new OutputStreamWriter(socket.getOutputStream()));
  49.                 panel = new PanelSocket(this);
  50.                 client_sck_srv_handler = new ClientSocketSrvHandler(socket, this);
  51.         }
  52.        
  53.         /**
  54.          * Starts the client.
  55.          * @throws UnknownHostException if the IP address could not be determined.
  56.          */
  57.         public void run() {
  58.                 /*ClientSocketSrvHandler client_sck_srv_handler = new ClientSocketSrvHandler(socket, this);*/
  59.                 client_sck_srv_handler.start();
  60.                 Scanner scan = new Scanner(System.in);
  61.                
  62.             System.out.print("Username:\n");
  63.             String username = scan.next();
  64.             System.out.print("Password:\n");
  65.                 String password = scan.next();
  66.                 String command = "@login,user="+username+",pass="+password;
  67.                 bufferedWriter.write(command);
  68.                 bufferedWriter.newLine();
  69.                 bufferedWriter.flush();
  70.                 String answer = client_sck_srv_handler.getResponse();
  71.                 if (answer != null) {
  72.                         System.out.println(SERVER_RESPONSE + answer);
  73.                         if (answer.startsWith(OK)){
  74.                                 //save the token
  75.                                 String[] splitted = answer.split(",");
  76.                                 this.token = splitted[1];
  77.                                 System.out.println("token received: " + this.token);
  78.                                 return true;
  79.                         }
  80.                 }
  81.                 return false;
  82. }