Advertisement
Guest User

simpleServer

a guest
May 2nd, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. public class simpleServer {
  4. public final static int TESTPORT = 5000;
  5. public static void main(String args[]) {
  6. ServerSocket checkServer = null;
  7. String line;
  8. BufferedReader is = null;
  9. DataOutputStream os = null;
  10. Socket clientSocket = null;
  11. try {
  12. checkServer = new ServerSocket(TESTPORT);
  13. System.out.println("Aplikasi Server hidup ...");
  14. } catch (IOException e) {
  15. System.out.println(e);
  16. }
  17. try {
  18. clientSocket = checkServer.accept();
  19. is = new BufferedReader(new
  20. InputStreamReader(clientSocket.getInputStream()));
  21. os = new DataOutputStream(clientSocket.getOutputStream());
  22. } catch (Exception ei) {
  23. ei.printStackTrace();
  24. }
  25. try {
  26. line = is.readLine();
  27. System.out.println("Terima : " + line);
  28. if (line.compareTo("salam") == 0) {
  29. os.writeBytes("salam juga");
  30. } else {
  31. os.writeBytes("Maaf, saya tidak mengerti");
  32. }
  33. } catch (IOException e) {
  34. System.out.println(e);
  35. }
  36.  
  37. try {
  38. os.close();
  39. is.close();
  40. clientSocket.close();
  41. } catch (IOException ic) {
  42. ic.printStackTrace();
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement