Advertisement
Guest User

Example Java Socket Server

a guest
Apr 29th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. import java.lang.*;
  2. import java.io.*;
  3. import java.net.*;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. public class SocketServer {
  8.  
  9. private static final Map<String, String> USER_PASSWORD_MAP = new HashMap<String, String>();
  10. private static final String USER_PROMPT = "enter username";
  11. private static final String PASSWORD_PROMPT = "enter password";
  12. private static final String WELCOME_MESSAGE = "Welcome to My Server";
  13. private static final int MAX_LOGIN_FAILS = 3;
  14.  
  15. static {
  16. USER_PASSWORD_MAP.put("1", "2");
  17. USER_PASSWORD_MAP.put("user", "password");
  18. }
  19.  
  20. private static boolean authenticateUser(BufferedReader in, PrintWriter out) throws IOException {
  21. for (int i = 0; i < MAX_LOGIN_FAILS; i++) {
  22. out.println(USER_PROMPT);
  23. String userName = in.readLine();
  24. out.println(PASSWORD_PROMPT);
  25. String password = in.readLine();
  26. if (USER_PASSWORD_MAP.containsKey(userName) == true) {
  27. String userPassword = USER_PASSWORD_MAP.get(userName);
  28. if (password.equals(userPassword) == true) {
  29. return true;
  30. }
  31. }
  32. }
  33. return false;
  34. }
  35.  
  36. private static void handshakeWithClient(BufferedReader in, PrintWriter out) throws IOException {
  37. System.out.println("Sending string: '" + WELCOME_MESSAGE);
  38. out.println(WELCOME_MESSAGE);
  39. String clientHostname = in.readLine();
  40. System.out.println("Received Client Hostname: " + clientHostname);
  41. String clientIPAddress = in.readLine();
  42. System.out.println("Received Client IPAddress: " + clientIPAddress);
  43. }
  44.  
  45. public static void main(String args[]) throws IOException {
  46.  
  47. String message;
  48. ServerSocket srvr = null;
  49. Socket skt = null;
  50. PrintWriter out = null;
  51.  
  52. try {
  53. //Detecting the localhost's ip address
  54. InetAddress localaddr = InetAddress.getLocalHost();
  55. System.out.println("Local IP Address : " + localaddr);
  56. System.out.println("Local hostname : " + localaddr.getHostAddress());
  57. // Creating a server socket for connection
  58. srvr = new ServerSocket(1234);
  59. System.out.println("Waiting for connection on " + localaddr);
  60. // Accept incoming connection
  61. skt = srvr.accept();
  62. System.out.print("Server has connected!\n"); // get Input and Output streams
  63. out = new PrintWriter(skt.getOutputStream(), true);
  64. out.flush();
  65. BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
  66.  
  67. //sends welcome message
  68. handshakeWithClient(in, out);
  69.  
  70. boolean authenticated = authenticateUser(in, out);
  71.  
  72. if (authenticated == true) {
  73. do {
  74. out.println("enter a message");
  75. message = in.readLine();
  76. System.out.println("client>" + message);
  77.  
  78.  
  79. } while (!message.equals("bye"));
  80. } else {
  81. System.out.println("Authentication Failed!");
  82. }
  83.  
  84. out.println("server>Server closing");
  85. System.out.println("server>Server closing");
  86.  
  87.  
  88. } catch (BindException e) {
  89. e.printStackTrace();
  90. System.out.print("A server is already running on the same port.");
  91. } catch (SocketException e) {
  92. e.printStackTrace();
  93. System.out.print("Client has disconnected rudely.");
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. System.out.print(e);
  97. } finally {
  98. if (out != null) {
  99. out.close();
  100. }
  101. if (skt != null) {
  102. skt.close();
  103. }
  104. if (srvr != null) {
  105. srvr.close();
  106. }
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement