fork download
  1. package ist.ass2;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.IOException;
  5. import java.io.PrintStream;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8.  
  9.  
  10. public class NCBServer {
  11. private static ServerSocket serverSocket = null;
  12. private static Socket socket = null;
  13. private static final int PORT = 6565;
  14. private static final int maxClients = 10;
  15. private static final clientThread[] threads = new clientThread[maxClients];
  16.  
  17. public static void main(String[] args){
  18. try{
  19. serverSocket = new ServerSocket(PORT);
  20. } catch(IOException e){
  21. e.printStackTrace();
  22. }
  23.  
  24. while(true){
  25. try {
  26. socket = serverSocket.accept();
  27. PrintStream os = new PrintStream(socket.getOutputStream());
  28. os.println("hi");
  29. } catch (IOException e1) {
  30. e1.printStackTrace();
  31. }
  32. int i = 0;
  33. for(i = 0; i < maxClients; i++){
  34. if(threads[i] == null){
  35. threads[i] = new clientThread(socket, threads);
  36. threads[i].start();
  37. break;
  38. }
  39. }
  40.  
  41. if(i == maxClients){
  42. try {
  43. os = new PrintStream(socket.getOutputStream());
  44. os.println("Server too busy. Try later.");
  45. os.close();
  46. socket.close();
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52. }
  53.  
  54. }
  55.  
  56. class clientThread extends Thread{
  57.  
  58. private DataInputStream is = null;
  59. private PrintStream os = null;
  60. private Socket socket = null;
  61. private final clientThread[] threads;
  62. private int maxClients;
  63.  
  64. public clientThread(Socket socket, clientThread[] threads){
  65. this.socket = socket;
  66. this.threads = threads;
  67. maxClients = threads.length;
  68. }
  69.  
  70. public void run(){
  71.  
  72. int maxClients = this.maxClients;
  73. clientThread[] threads = this.threads;
  74.  
  75. try{
  76. is = new DataInputStream(socket.getInputStream());
  77. os = new PrintStream(socket.getOutputStream());
  78.  
  79. while(true){
  80. //noch unklar
  81. break;
  82. }
  83.  
  84. is.close();
  85. os.close();
  86. socket.close();
  87. } catch (IOException e){
  88. e.printStackTrace();
  89. }
  90.  
  91.  
  92. }
  93.  
  94.  
  95. }
  96.  
  97.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:10: error: class NCBServer is public, should be declared in a file named NCBServer.java
public class NCBServer {
       ^
1 error
stdout
Standard output is empty