Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. package Worksheet3;
  2.  
  3. //
  4. // EchoServer.java
  5. // Acts as an echo server.
  6. // Client must connect on the correct port.
  7. // Server closes the connection on receiving 'END'.
  8. // But keeps running for more client connections.
  9. //
  10. // Give the port number as a command line argument:
  11. // e.g. java EchoServer [4567]
  12. //
  13. import java.io.*;
  14. import java.net.*;
  15.  
  16. public class EchoServer {
  17.  
  18. public final static int DEFAULT_PORT = 3456;
  19. public final static int qLen = 3; // number of clients that can q
  20.  
  21. public void EchoServer() {
  22. }
  23.  
  24. public static void main(String[] args) throws IOException {
  25. ServerSocket listenSocket = null;
  26. OutputStreamWriter osw = null;
  27. InputStreamReader isr = null;
  28. BufferedReader br = null;
  29. int portNum = DEFAULT_PORT;
  30. int clientNo = 0; // count clients serviced
  31.  
  32. if (args.length != 0) {
  33. try {
  34. portNum = Integer.parseInt(args[ 0]);
  35. // put some test here to allow for port number not in range
  36. } catch (NumberFormatException nfE) {
  37. System.err.println("Illegal port number: " + args[ 0]);
  38. System.err.println("\tUsing the default: " + DEFAULT_PORT);
  39. }
  40. }
  41. try {
  42. listenSocket = new ServerSocket(portNum, qLen);
  43. } catch (BindException e) {
  44. System.err.println("Could not bind to port: " + portNum);
  45. System.err.println("\tIs it already in use?");
  46. System.err.println("\tIs it a reserved port number?");
  47. System.exit(1);
  48. }
  49. while (true) { // loop forever accepting clients
  50. Socket clientSocket = null;
  51. // try changing for isr rather than br
  52. String request = null;
  53. try {
  54. clientSocket = listenSocket.accept(); // actual comms socket!
  55. ++clientNo; // count clients serviced
  56. osw = new OutputStreamWriter(clientSocket.getOutputStream());
  57. // the above line might need , "US-ASCII"); or // "UTF-8"?
  58. System.out.println("Connection from: "
  59. + clientSocket.getInetAddress());
  60. isr = new InputStreamReader(clientSocket.getInputStream());
  61. // the above line might need , "US-ASCII"); or // "UTF-8"?
  62. br = new BufferedReader(isr);
  63. do {
  64. // try changing for isr rather than br
  65. request = br.readLine();
  66. System.out.println("\tFrom client " + clientNo + ": " + request);
  67. // What happens if we don't send '\r\n' or only one of them?
  68. osw.write("Hello from " + InetAddress.getLocalHost() + " to Client no:" + clientNo + "\r\n");
  69.  
  70. osw.flush(); // make sure msg got sent from the buffer!
  71. } while (!request.equals("END"));
  72. System.out.println("Client " + clientNo + " closed connection");
  73. osw.close();
  74. isr.close();
  75. br.close();
  76. clientSocket.close();
  77. } // end try{} accepting a client connection
  78. catch (IOException ioE) {
  79. System.err.println("Connection error, maybe the client died!");
  80. } finally { // to trap any other errors!!
  81. try {
  82. if (clientSocket != null) {
  83. clientSocket.close();
  84. }
  85. } catch (IOException ioE) {
  86. }
  87. } // end of finally
  88. } // while forever waiting for a client connection
  89. } // main
  90. } // class EchoServer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement