Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.DataInputStream;
  3. import java.io.EOFException;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.DataOutputStream;
  7. import java.io.FileOutputStream;
  8. import java.util.Scanner;
  9.  
  10.  
  11.  
  12. class MyServer
  13. {
  14. // Your program begins with a call to main().
  15. // Prints "Hello, World" to the terminal window.
  16.  
  17. MyServer(int port)
  18. {
  19.  
  20. Scanner scanner = new Scanner(System.in);
  21. try {
  22. // System.out.println("Hello, World");
  23. ServerSocket server = new ServerSocket(port);
  24.  
  25.  
  26. System.out.print("Username is: ");
  27. String userName = scanner.nextLine();
  28. // String sentname;
  29.  
  30. System.out.println("Waiting to connect...");
  31. Socket s = server.accept();
  32. // System.out.println("Username is: " + userName);
  33.  
  34. System.out.println("Connected");
  35. // System.out.println("Connected");
  36.  
  37. DataInputStream incoming = new DataInputStream(s.getInputStream());
  38.  
  39. DataOutputStream outgoing = new DataOutputStream(s.getOutputStream());
  40.  
  41.  
  42. outgoing.writeUTF(userName);
  43. outgoing.flush();
  44. String sentname = incoming.readUTF();
  45.  
  46. while(true)
  47. {
  48.  
  49. String send, received;
  50.  
  51. //THIS NEEDS TO BE TAKEN INPUT
  52. send = scanner.nextLine();
  53.  
  54.  
  55.  
  56. // CLOSE BUTTON PRESS
  57. if ( send.equals("close"))
  58. {
  59.  
  60. System.out.println("Server Closed");
  61. incoming.close();
  62. outgoing.close();
  63. s.close();
  64. }
  65.  
  66. outgoing.writeUTF(send);
  67. outgoing.flush();
  68.  
  69.  
  70. // THIS NEEDS TO BE SEEN ON THE SCREEN
  71. System.out.println(userName + ": " + send);
  72.  
  73. received = incoming.readUTF();
  74.  
  75. // THIS NEEDS TO BE SEEN ON THE SCREEN
  76. System.out.println(sentname + ": " + received);
  77. // if()
  78.  
  79.  
  80. }
  81.  
  82.  
  83. // outgoing.flush();
  84.  
  85.  
  86.  
  87.  
  88.  
  89. // System.out.println("Hello");
  90. } catch(Exception e){}
  91. }
  92.  
  93.  
  94. public static void main(String args[])
  95. {
  96. MyServer s = new MyServer( (Integer.parseInt(args[0]) ) );
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement