Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. //build a socket.
  2. public void connectclient() throws IOException
  3. {
  4. socket = new Socket("localhost", 9097);
  5. System.out.println("connect to server on port 9097");
  6. }
  7. public void startstreams() throws IOException , ClassNotFoundException
  8. {
  9. in = socket.getInputStream();
  10. out = socket.getOutputStream();
  11. dos = new DataOutputStream(out);
  12. dis = new DataInputStream(in);
  13. writer = new OutputStreamWriter(out);
  14. reader = new InputStreamReader(in);
  15. breader = new BufferedReader(reader);
  16. bwriter = new BufferedWriter (writer);
  17. w = new PrintWriter(out, true);
  18. }
  19. public void writeSocketMyJson(String n) throws IOException
  20. {
  21. w = new PrintWriter(out, true);
  22. w.println(n);
  23. w.flush();
  24. w.close();
  25. }
  26.  
  27. massage1 = "username";
  28. //send username that get from login form to server.
  29. public void sendusername() throws IOException
  30. {
  31. writeSocketMyJson(massage1);
  32. }
  33.  
  34. massage2 ="admin";
  35. //send password that get from login form to server.
  36. public void sendpassword() throws IOException
  37. {
  38. writeSocketMyJson(massage2);
  39. }
  40.  
  41. //send access level to server.
  42. public void sendaccess(int l) throws IOException
  43. {
  44. dos.writeInt(l);
  45. dos.flush();
  46. }
  47. sendaccess(21);
  48.  
  49. //build server.
  50. public void connectserver() throws IOException
  51. {
  52. listener = new ServerSocket(9097);
  53. System.out.println("Server is running on port 9097 ...");
  54. }
  55. //wait for new connection.
  56. public void waitforclient() throws IOException
  57. {
  58. socket = listener.accept();
  59. System.out.println("A new client connected to the server");
  60. }
  61. public void startstreams() throws IOException
  62. {
  63. in = socket.getInputStream();
  64. out = socket.getOutputStream();
  65. dos = new DataOutputStream(out);
  66. dis = new DataInputStream(in);
  67. writer = new OutputStreamWriter(out);
  68. reader = new InputStreamReader(in);
  69. bwriter = new BufferedWriter (writer);
  70. breader = new BufferedReader (reader);
  71. }
  72. public String readSocket() throws IOException
  73. {
  74. breader = new BufferedReader(reader);
  75. while (true)
  76. {
  77. massage = new String();
  78. massage = breader.readLine();
  79. if (massage.equals(null) == false)
  80. {
  81. break;
  82. }
  83. }
  84. return(massage);
  85. }
  86.  
  87. //get username that client send it.
  88. public String getusername() throws IOException, ClassNotFoundException
  89. {
  90. try
  91. {
  92. username = new String();
  93. username = readSocket();
  94. System.out.println("the username is : " + username);
  95. }
  96. catch(IOException IOE)
  97. {
  98. IOE.printStackTrace();//if there is an error, print it out
  99. }
  100. return(username);
  101. }
  102.  
  103. //get password that client send it.
  104. public String getpassword() throws IOException, ClassNotFoundException
  105. {
  106. try
  107. {
  108. password = new String();
  109. password = readSocket();
  110. System.out.println("the password is : " + password);
  111. }
  112. catch(IOException IOE)
  113. {
  114. IOE.printStackTrace();//if there is an error, print it out
  115. }
  116. return(password);
  117. }
  118.  
  119. //get commend from client.
  120. //admin or user send which commend 21-add new information 22-show information
  121. public int getaccess() throws IOException
  122. {
  123. System.out.println("server get access : " + dis.readInt());
  124. return(dis.readInt());
  125. }
  126.  
  127. //build Server & Client
  128. Server server = new Server();
  129. Client client = new Client();
  130.  
  131. //Start Server & Client
  132. server.connectserver();
  133. client.connectclient();
  134.  
  135. //Server wait for new connection
  136. server.waitforclient();
  137.  
  138. //start the Streams
  139. server.startstreams();
  140. client.startstreams();
  141.  
  142. client.sendusername();
  143. String msg1 =server.readSocket();
  144.  
  145. client.sendpassword();
  146. String msg2 =server.readSocket();
  147.  
  148. client.sendaccess();
  149. int n = getaccess();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement