Advertisement
Guest User

Untitled

a guest
May 12th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package server;
  2.  
  3. import java.net.*;
  4. import java.io.*;
  5.  
  6. public class KnockKnockServer {
  7. public static void main(String[] args) throws IOException {
  8.  
  9. ServerSocket serverSocket = null;
  10. try {
  11. serverSocket = new ServerSocket(43594);
  12. } catch (IOException e) {
  13. System.err.println("Could not listen on port: 43594.");
  14. System.exit(1);
  15. }
  16.  
  17. Socket clientSocket = null;
  18. try {
  19. clientSocket = serverSocket.accept();
  20. DataInputStream d = new DataInputStream(clientSocket.getInputStream());
  21. String User = d.readUTF();
  22. String Password = d.readUTF();
  23. System.out.println("["+User+"]: Has logged in.");
  24. } catch (IOException e) {
  25. System.err.println("Accept failed.");
  26. System.exit(1);
  27. }
  28.  
  29. PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  30. BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  31. String inputLine, outputLine;
  32. KnockKnockProtocol kkp = new KnockKnockProtocol();
  33.  
  34. outputLine = kkp.processInput(null);
  35. out.println(outputLine);
  36.  
  37. while ((inputLine = in.readLine()) != null) {
  38. outputLine = kkp.processInput(inputLine);
  39. out.println(outputLine);
  40. if (outputLine.equals("Bye."))
  41. break;
  42. }
  43. out.close();
  44. in.close();
  45. clientSocket.close();
  46. serverSocket.close();
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement