Guest User

Untitled

a guest
Mar 31st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. import java.lang.*;
  2. import java.io.*;
  3. import java.net.*;
  4.  
  5. class Server {
  6. public static void main(String args[]) {
  7. String welcome = "Welcome! The server is now connected.";
  8. String login = "Enter username and password: ";
  9. String message;
  10.  
  11. try {
  12. //Detecting the localhost's ip address
  13. InetAddress localaddr = InetAddress.getLocalHost();
  14. System.out.println("SERVER\n");
  15. System.out.println ("Local hostnameIP: " + localaddr );
  16.  
  17. // Creating a server socket for connection
  18. ServerSocket srvr = new ServerSocket(1234);
  19. System.out.println("Waiting for connection on "+localaddr);
  20. // Accept incoming connection
  21. Socket skt = srvr.accept();
  22. System.out.print("Server has connected!\n");
  23. // get Input and Output streams
  24. PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
  25. out.flush();
  26. BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
  27. BufferedReader log = new BufferedReader(new InputStreamReader(skt.getInputStream())); //read input for login
  28. System.out.print("Sending string: '" + welcome + "'\n");
  29. out.println(welcome);
  30. out.println(login);
  31. //out.println(enterUser);
  32.  
  33.  
  34. //login
  35. //out.print("Username: ");
  36. String username = in.readLine();
  37. System.out.println("Client's username: " + username);
  38.  
  39. //out.print("Password: ");
  40. String password = in.readLine();
  41. System.out.println("Client's password: " + password);
  42.  
  43. if (username.equals("hello123") && password.equals("mypass")) {
  44. out.println("Correct login!");
  45. System.out.println ("Client's IP Address: " + localaddr.getHostAddress());
  46. }
  47. if (!username.equals("hello123")||!password.equals("mypass")) {
  48. out.println("Wrong login - server closing");
  49. out.close();
  50. skt.close();
  51. srvr.close();
  52. }
  53.  
  54.  
  55. do {
  56.  
  57. message=in.readLine();
  58. System.out.println("client> "+message);
  59. if (message.equals("password")){
  60. out.println("Access Granted");
  61. }else if (message.equals("bye")){
  62. out.println("Server closing");
  63. System.out.println("server> Server closing");
  64. }
  65. }while(!message.equals("bye"));
  66. out.close();
  67. skt.close();
  68. srvr.close();
  69. }catch(BindException e){
  70. //e.printStackTrace();
  71. System.out.print("A server is already running on the same port.");
  72. }catch(SocketException e) {
  73. //e.printStackTrace();
  74. System.out.print("Client has disconnected rudely.");
  75. }catch(Exception e){
  76. //e.printStackTrace();
  77. System.out.print(":( Whoops! It didn't work!\n");
  78.  
  79. }
  80. }
  81.  
  82.  
  83.  
  84. }
Add Comment
Please, Sign In to add comment