fork download
  1. package yju.socket;
  2.  
  3. import java.io.IOException;
  4. import java.net.InetAddress;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10.  
  11. public abstract class TcpServer implements Runnable {
  12.  
  13. private int port;
  14. private boolean runFlag;
  15. private List<SocketTransceiver> clients = new ArrayList<SocketTransceiver>();
  16.  
  17.  
  18. public TcpServer(int port) {
  19. this.port = port;
  20. }
  21.  
  22.  
  23. public void start() {
  24. runFlag = true;
  25. new Thread(this).start();
  26. }
  27.  
  28.  
  29. public void stop() {
  30. runFlag = false;
  31. }
  32.  
  33.  
  34. @Override
  35. public void run() {
  36. try {
  37. final ServerSocket server = new ServerSocket(port);
  38. while (runFlag) {
  39. try {
  40. final Socket socket = server.accept();
  41. startClient(socket);
  42. } catch (IOException e) {
  43.  
  44. e.printStackTrace();
  45. this.onConnectFailed();
  46. }
  47. }
  48.  
  49. try {
  50. for (SocketTransceiver client : clients) {
  51. client.stop();
  52. }
  53. clients.clear();
  54. server.close();
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. } catch (IOException e) {
  59.  
  60. e.printStackTrace();
  61. }
  62. this.onServerStop();
  63. }
  64.  
  65.  
  66. private void startClient(final Socket socket) {
  67. SocketTransceiver client = new SocketTransceiver(socket) {
  68.  
  69. @Override
  70. public void onReceive(InetAddress addr, String s) {
  71. TcpServer.this.onReceive(this, s);
  72. }
  73.  
  74. @Override
  75. public void onDisconnect(InetAddress addr) {
  76. clients.remove(this);
  77. TcpServer.this.onDisconnect(this);
  78. }
  79. };
  80. client.start();
  81. clients.add(client);
  82. this.onConnect(client);
  83. }
  84.  
  85.  
  86. public abstract void onConnect(SocketTransceiver client);
  87.  
  88.  
  89. public abstract void onConnectFailed();
  90.  
  91.  
  92. public abstract void onReceive(SocketTransceiver client, String s);
  93.  
  94.  
  95. public abstract void onDisconnect(SocketTransceiver client);
  96.  
  97.  
  98. public abstract void onServerStop();
  99. }
  100.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:11: error: class TcpServer is public, should be declared in a file named TcpServer.java
public abstract class TcpServer implements Runnable {
                ^
Main.java:15: error: cannot find symbol
	private List<SocketTransceiver> clients = new ArrayList<SocketTransceiver>();
	             ^
  symbol:   class SocketTransceiver
  location: class TcpServer
Main.java:86: error: cannot find symbol
	public abstract void onConnect(SocketTransceiver client);
	                               ^
  symbol:   class SocketTransceiver
  location: class TcpServer
Main.java:92: error: cannot find symbol
	public abstract void onReceive(SocketTransceiver client, String s);
	                               ^
  symbol:   class SocketTransceiver
  location: class TcpServer
Main.java:95: error: cannot find symbol
	public abstract void onDisconnect(SocketTransceiver client);
	                                  ^
  symbol:   class SocketTransceiver
  location: class TcpServer
Main.java:15: error: cannot find symbol
	private List<SocketTransceiver> clients = new ArrayList<SocketTransceiver>();
	                                                        ^
  symbol:   class SocketTransceiver
  location: class TcpServer
Main.java:50: error: cannot find symbol
				for (SocketTransceiver client : clients) {
				     ^
  symbol:   class SocketTransceiver
  location: class TcpServer
Main.java:67: error: cannot find symbol
		SocketTransceiver client = new SocketTransceiver(socket) {
		^
  symbol:   class SocketTransceiver
  location: class TcpServer
Main.java:67: error: cannot find symbol
		SocketTransceiver client = new SocketTransceiver(socket) {
		                               ^
  symbol:   class SocketTransceiver
  location: class TcpServer
9 errors
stdout
Standard output is empty