Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.io.*;
  2. import java.net.*;
  3. public class simpleClient {
  4.     public final static int REMOTE_PORT = 5000;
  5.     public static void main(String args[]) throws Exception {
  6.         Socket cl = null;
  7.         BufferedReader is = null;
  8.         DataOutputStream os = null;
  9.         BufferedReader stdin = new BufferedReader(new
  10.         InputStreamReader(System.in));
  11.         String userInput = null;
  12.         String output = null;
  13. // Membuka koneksi ke server pada port REMOTE_PORT
  14.         try {
  15.             cl = new Socket(args[0], REMOTE_PORT);
  16.             is = new BufferedReader(new
  17.             InputStreamReader(cl.getInputStream()));
  18.             os = new DataOutputStream(cl.getOutputStream());
  19.         } catch(UnknownHostException e1) {
  20.             System.out.println("Unknown Host: " + e1);
  21.         } catch (IOException e2) {
  22.             System.out.println("Erorr io: " + e2);
  23.         }
  24. // Menulis ke server
  25.         try {
  26.             System.out.print("Masukkan kata kunci: ");
  27.         userInput = stdin.readLine();
  28.         os.writeBytes(userInput + "\n");
  29.         } catch (IOException ex) {
  30.         System.out.println("Error writing to server..." + ex);
  31.         }
  32. // Menerima tanggapan dari server
  33.         try {
  34.             output = is.readLine();
  35.             System.out.println("Dari server: " + output);
  36.         } catch (IOException e) {
  37.         e.printStackTrace();
  38.         }
  39.     // close input stream, output stream dan koneksi
  40.         try {
  41.         is.close();
  42.         os.close();
  43.         cl.close();
  44.         } catch (IOException x) {
  45.         System.out.println("Error writing...." + x);
  46.         }
  47.     }
  48. }