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