Guest User

Untitled

a guest
Jan 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. public class Server {
  2. private ServerSocket serverSocket = null;
  3. private Socket clientSocket = null;
  4.  
  5. public Server() {
  6. try {
  7. serverSocket = new ServerSocket(7003);
  8. } catch (IOException e) {
  9. System.err.println("Could not listen on port: 7003");
  10. System.exit(1);
  11. }
  12.  
  13. try {
  14. clientSocket = serverSocket.accept();
  15. } catch (IOException e) {
  16. System.err.println("Accept failed");
  17. System.exit(1);
  18. }
  19. }
  20.  
  21. public void startServer() throws IOException {
  22. PrintWriter output = new PrintWriter(clientSocket.getOutputStream(), true);
  23. BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  24.  
  25. String inputLine, outputLine;
  26.  
  27. outputLine = "Connected to Server";
  28. output.println(outputLine);
  29.  
  30. while ((inputLine = input.readLine()) != null) {
  31. // This just determines users input and server ruturns output based on that
  32.  
  33. outputLine = this.getServerOutput(inputLine);
  34. output.println(outputLine);
  35.  
  36. if (outputLine.equals("Bye"))
  37. break;
  38. }
  39.  
  40. output.close();
  41. input.close();
  42. clientSocket.close();
  43. serverSocket.close();
  44. }
  45. }
  46.  
  47. public Server()
  48. {
  49. try
  50. {
  51. serverSocket = new ServerSocket(7003);
  52. }
  53. catch (IOException e)
  54. {
  55. System.err.println("Could not listen on port: 7003");
  56. System.exit(1);
  57. }
  58.  
  59. try
  60. {
  61. while(true) {
  62. final Socket socket = serverSocket.accept();
  63. new Thread(new Runnable() {
  64. public void run() {
  65. try {
  66. startServer(socket);
  67. } catch(IOException e) {e.printStackTrace();}
  68. }
  69. }).start();
  70. }
  71. }
  72. catch(IOException e)
  73. {
  74. System.err.println("Accept failed");
  75. System.exit(1);
  76. }
  77. }
  78.  
  79. clientSocket = serverSocket.accept();
  80.  
  81. public void startServer() throws IOException {
  82. PrintWriter output = new PrintWriter(clientSocket.getOutputStream(), true);
  83. ...
  84.  
  85. public class Server {
  86. private ServerSocket serverSocket = null;
  87.  
  88. public Server(int portNumber) throws IOException {
  89. serverSocket = new ServerSocket(portNumber);
  90. }
  91.  
  92. // this could be run in a thread in the background
  93. public void acceptClients() throws IOException {
  94. // create an open ended thread-pool
  95. ExecutorService threadPool = Executors.newCachedThreadPool();
  96. try {
  97. while (!Thread.currentThread().isInterrupted()) {
  98. // wait for a client to connect
  99. Socket clientSocket = serverSocket.accept();
  100. // create a new client handler object for that socket,
  101. // and fork it in a background thread
  102. threadPool.submit(new ClientHandler(clientSocket));
  103. }
  104. } finally {
  105. // we _have_ to shutdown the thread-pool when we are done
  106. threadPool.shutdown();
  107. }
  108. }
  109.  
  110. // if server is running in background, you stop it by killing the socket
  111. public void stop() throws IOException {
  112. serverSocket.close();
  113. }
  114.  
  115. // this class handles each client connection
  116. private static class ClientHandler implements Runnable {
  117. private final Socket clientSocket;
  118. public ClientHandler(Socket clientSocket) {
  119. this.clientSocket = clientSocket;
  120. }
  121. public void run() {
  122. // use the client socket to handle the client connection
  123. ...
  124. }
  125. }
  126. }
  127.  
  128. public void acceptClients() throws IOException {
  129. while (!Thread.currentThread().isInterrupted()) {
  130. // wait for a client to connect
  131. Socket clientSocket = serverSocket.accept();
  132. // fork a background client thread
  133. new Thread(new ClientHandler(clientSocket)).start();
  134. }
  135. }
  136.  
  137. public class MyServer {
  138.  
  139. private static MyServer server;
  140. private ServerSocket serverSocket;
  141.  
  142. /**
  143. * This executor service has 10 threads.
  144. * So it means your server can process max 10 concurrent requests.
  145. */
  146. private ExecutorService executorService = Executors.newFixedThreadPool(10);
  147.  
  148. public static void main(String[] args) throws IOException {
  149. server = new MyServer();
  150. server.runServer();
  151. }
  152.  
  153. private void runServer() {
  154. int serverPort = 8085;
  155. try {
  156. System.out.println("Starting Server");
  157. serverSocket = new ServerSocket(serverPort);
  158.  
  159. while(true) {
  160. System.out.println("Waiting for request");
  161. try {
  162. Socket s = serverSocket.accept();
  163. System.out.println("Processing request");
  164. executorService.submit(new ServiceRequest(s));
  165. } catch(IOException ioe) {
  166. System.out.println("Error accepting connection");
  167. ioe.printStackTrace();
  168. }
  169. }
  170. }catch(IOException e) {
  171. System.out.println("Error starting Server on "+serverPort);
  172. e.printStackTrace();
  173. }
  174. }
  175.  
  176. //Call the method when you want to stop your server
  177. private void stopServer() {
  178. //Stop the executor service.
  179. executorService.shutdownNow();
  180. try {
  181. //Stop accepting requests.
  182. serverSocket.close();
  183. } catch (IOException e) {
  184. System.out.println("Error in server shutdown");
  185. e.printStackTrace();
  186. }
  187. System.exit(0);
  188. }
  189.  
  190. class ServiceRequest implements Runnable {
  191.  
  192. private Socket socket;
  193.  
  194. public ServiceRequest(Socket connection) {
  195. this.socket = connection;
  196. }
  197.  
  198. public void run() {
  199.  
  200. //Do your logic here. You have the `socket` available to read/write data.
  201.  
  202. //Make sure to close
  203. try {
  204. socket.close();
  205. }catch(IOException ioe) {
  206. System.out.println("Error closing client connection");
  207. }
  208. }
  209. }
  210. }
Add Comment
Please, Sign In to add comment