Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. class ClientSystem
  5. {
  6. public static void main(String[] args)
  7. {
  8. String hostname = "127.0.0.1";
  9. int port = 1234;
  10.  
  11. Socket clientsocket = null;
  12. DataOutputStream output =null;
  13. BufferedReader input = null;
  14.  
  15. try
  16. {
  17. clientsocket = new Socket(hostname,port);
  18. output = new DataOutputStream(clientsocket.getOutputStream());
  19. input = new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
  20. }
  21. catch(Exception e)
  22. {
  23. System.out.println("Error occured"+e);
  24. }
  25.  
  26. try
  27. {
  28. while(true)
  29. {
  30. System.out.println("Enter input string ('exit' to terminate connection): ");
  31. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  32. String inputstring = br.readLine();
  33. output.writeBytes(inputstring+"n");
  34.  
  35. //int n = Integer.parseInt(inputstring);
  36. if(inputstring.equals("exit"))
  37. break;
  38.  
  39. String response = input.readLine();
  40. System.out.println("Reversed string is: "+response);
  41.  
  42. output.close();
  43. input.close();
  44. clientsocket.close();
  45. }
  46. }
  47. catch(Exception e)
  48. {
  49. System.out.println("Error occured."+e);
  50. }
  51. }
  52. }
  53.  
  54. import java.io.*;
  55. import java.net.*;
  56.  
  57. public class ServerSystem
  58. {
  59. ServerSocket server = null;
  60. Socket clientsocket = null;
  61. int numOfConnections = 0, port;
  62.  
  63. public ServerSystem(int port)
  64. {
  65. this.port = port;
  66. }
  67.  
  68. public static void main(String[] args)
  69. {
  70. int port = 1234;
  71. ServerSystem ss = new ServerSystem(port);
  72. ss.startServer();
  73. }
  74.  
  75. public void startServer()
  76. {
  77. try
  78. {
  79. server = new ServerSocket(port);
  80. }
  81. catch(Exception e)
  82. {
  83. System.out.println("Error occured."+e);
  84. }
  85.  
  86. System.out.println("Server has started. Ready to accept connections.");
  87.  
  88. while(true)
  89. {
  90. try
  91. {
  92. clientsocket = server.accept();
  93. numOfConnections++;
  94. ServerConnection sc = new ServerConnection(clientsocket, numOfConnections, this);
  95. new Thread(sc).start();
  96. }
  97. catch(Exception e)
  98. {
  99. System.out.println("Error occured."+e);
  100. }
  101. }
  102. }
  103.  
  104. public void stopServer()
  105. {
  106. System.out.println("Terminating connection");
  107. System.exit(0);
  108. }
  109. }
  110.  
  111. class ServerConnection extends Thread
  112. {
  113. BufferedReader br;
  114. PrintStream ps;
  115. Socket clientsocket;
  116. int id;
  117. ServerSystem ss;
  118.  
  119. public ServerConnection(Socket clientsocket, int numOfConnections, ServerSystem ss)
  120. {
  121. this.clientsocket = clientsocket;
  122. id = numOfConnections;
  123. this.ss = ss;
  124.  
  125. System.out.println("Connection "+id+" established with "+clientsocket);
  126. try
  127. {
  128. br = new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
  129. ps = new PrintStream(clientsocket.getOutputStream());
  130. }
  131. catch(Exception e)
  132. {
  133. System.out.println("Error occured."+e);
  134. }
  135. }
  136.  
  137. public void run()
  138. {
  139. String line, reversedstring = "";
  140.  
  141. try
  142. {
  143. boolean stopserver = false;
  144. while(true)
  145. {
  146. line = br.readLine();
  147. System.out.println("Received string: "+line+" from connection "+id);
  148. //long n = Long.parseLong(line.trim());
  149.  
  150. if(line.equals("exit"))
  151. {
  152. stopserver = true;
  153. break;
  154. }
  155. else
  156. {
  157. int len = line.length();
  158. for (int i=len-1; i>=0; i--)
  159. reversedstring = reversedstring + line.charAt(i);
  160. ps.println(""+reversedstring);
  161. }
  162. }
  163. System.out.println("Connection "+id+" is closed.");
  164. br.close();
  165. ps.close();
  166. clientsocket.close();
  167.  
  168. if(stopserver)
  169. ss.stopServer();
  170. }
  171. catch(Exception e)
  172. {
  173. System.out.println("Error occured."+e);
  174. }
  175. }
  176. }
  177.  
  178. Enter input string ('exit' to terminate connection):
  179. usa
  180. Reversed string is: asu
  181. Enter input string ('exit' to terminate connection):
  182. usa
  183. Error occured.java.net.SocketException: Socket closed
  184.  
  185. Server has started. Ready to accept connections.
  186. Connection 1 established with Socket[addr=/127.0.0.1,port=3272,localport=1234]
  187. Received string: usa from connection 1
  188. Received string: null from connection 1
  189. Error occured.java.lang.NullPointerException
  190.  
  191. output.close();
  192. input.close();
  193. clientsocket.close();
  194.  
  195. try {
  196. while(true) {
  197. // client code here
  198. }
  199. } catch (Exception e) {
  200. e.printStackTrace(); // notice this line. Will save you a lot of time!
  201. } finally {
  202. output.close(); //close resources here!
  203. input.close();
  204. clientsocket.close();
  205. }
  206.  
  207. LOG.error("Unexpected error when deionizing the flux capacitor",e);
  208.  
  209. e.printStackTrace();
  210.  
  211. else
  212. {
  213. int len = line.length();
  214.  
  215. reversedString=""; //this line erases the previous content of the reversed string
  216.  
  217. for (int i=len-1; i>=0; i--) { //always use brackets!!!
  218. reversedstring = reversedstring + line.charAt(i);
  219. }
  220. ps.println(""+reversedstring);
  221. }
  222.  
  223. while(true)
  224. {
  225. line = br.readLine();
  226. System.out.println("Received string: "+line+" from connection "+id);
  227.  
  228. if(line.equals("exit"))
  229. {
  230. break; //just stop this connection, don't kill server
  231. }
  232. else if(line.equals("stop"))
  233. {
  234. stopserver = true; //stop server too
  235. break;
  236. }
  237. else
  238. {
  239. int len = line.length();
  240. for (int i=len-1; i>=0; i--) {
  241. reversedstring = reversedstring + line.charAt(i);
  242. }
  243. ps.println(""+reversedstring);
  244. }
  245. }
  246.  
  247. output.close();
  248. input.close();
  249. clientsocket.close();
  250.  
  251. } finally {
  252. output.close();
  253. input.close();
  254. clientsocket.close();
  255. }
  256.  
  257. } finally {
  258. br.close();
  259. ps.close();
  260. clientsocket.close();
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement