Guest User

simpleClient.java

a guest
Mar 29th, 2017
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  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 InputStreamReader(System.in));
  10. String userInput = null;
  11. String output = null;
  12. // Membuka koneksi ke server pada port REMOTE_PORT
  13. try {
  14. cl = new Socket(args[0], REMOTE_PORT);
  15. is = new BufferedReader(new InputStreamReader(cl.getInputStream()));
  16. os = new DataOutputStream(cl.getOutputStream());
  17. } catch(UnknownHostException e1) {
  18. System.out.println("Unknown Host: " + e1);
  19. } catch (IOException e2) {
  20. System.out.println("Erorr io: " + e2);
  21. }
  22. // Menulis ke server
  23. try {
  24. System.out.print("Masukkan kata kunci: ");
  25. userInput = stdin.readLine();
  26. os.writeBytes(userInput + "\n");
  27. } catch (IOException ex) {
  28. System.out.println("Error writing to server..." + ex);
  29. }
  30. // Menerima tanggapan dari server
  31. try {
  32. output = is.readLine();
  33. System.out.println("Dari server: " + output);
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. // close input stream, output stream dan koneksi
  38. try {
  39. is.close();
  40. os.close();
  41. cl.close();
  42. } catch (IOException x) {
  43. System.out.println("Error writing...." + x);
  44. }
  45. }
  46. }
Add Comment
Please, Sign In to add comment