fork download
  1. import java.io.*;
  2. import java.net.*;
  3. import java.nio.*;
  4. import java.nio.channels.*;
  5. import java.util.*;
  6.  
  7. public class Server implements Runnable
  8. {
  9. // The port we will listen on
  10. private int port;
  11.  
  12. // A pre-allocated buffer for encrypting data
  13. private final ByteBuffer buffer = ByteBuffer.allocate( 16384 );
  14.  
  15. public Server( int port ) {
  16. this.port = port;
  17. new Thread( this ).start();
  18. }
  19.  
  20. public void run() {
  21. try {
  22. // Instead of creating a ServerSocket,
  23. // create a ServerSocketChannel
  24. ServerSocketChannel ssc = ServerSocketChannel.open();
  25.  
  26. // Set it to non-blocking, so we can use select
  27. ssc.configureBlocking( false );
  28.  
  29. // Get the Socket connected to this channel, and bind it
  30. // to the listening port
  31. ServerSocket ss = ssc.socket();
  32. InetSocketAddress isa = new InetSocketAddress( port );
  33. ss.bind( isa );
  34.  
  35. // Create a new Selector for selecting
  36. Selector selector = Selector.open();
  37.  
  38. // Register the ServerSocketChannel, so we can
  39. // listen for incoming connections
  40. ssc.register( selector, SelectionKey.OP_ACCEPT );
  41. System.out.println( "Listening on port "+port );
  42.  
  43. while (true) {
  44. // See if we've had any activity -- either
  45. // an incoming connection, or incoming data on an
  46. // existing connection
  47. int num = selector.select();
  48.  
  49. // If we don't have any activity, loop around and wait
  50. // again
  51. if (num == 0) {
  52. continue;
  53. }
  54.  
  55. // Get the keys corresponding to the activity
  56. // that has been detected, and process them
  57. // one by one
  58. Set keys = selector.selectedKeys();
  59. Iterator it = keys.iterator();
  60. while (it.hasNext()) {
  61. // Get a key representing one of bits of I/O
  62. // activity
  63. SelectionKey key = (SelectionKey)it.next();
  64.  
  65. // What kind of activity is it?
  66. if ((key.readyOps() & SelectionKey.OP_ACCEPT) ==
  67. SelectionKey.OP_ACCEPT) {
  68.  
  69. System.out.println( "acc" );
  70. // It's an incoming connection.
  71. // Register this socket with the Selector
  72. // so we can listen for input on it
  73.  
  74. Socket s = ss.accept();
  75. System.out.println( "Got connection from "+s );
  76.  
  77. // Make sure to make it non-blocking, so we can
  78. // use a selector on it.
  79. SocketChannel sc = s.getChannel();
  80. sc.configureBlocking( false );
  81.  
  82. // Register it with the selector, for reading
  83. sc.register( selector, SelectionKey.OP_READ );
  84. } else if ((key.readyOps() & SelectionKey.OP_READ) ==
  85. SelectionKey.OP_READ) {
  86.  
  87. SocketChannel sc = null;
  88.  
  89. try {
  90.  
  91. // It's incoming data on a connection, so
  92. // process it
  93. sc = (SocketChannel)key.channel();
  94. boolean ok = processInput( sc );
  95.  
  96. // If the connection is dead, then remove it
  97. // from the selector and close it
  98. if (!ok) {
  99. key.cancel();
  100.  
  101. Socket s = null;
  102. try {
  103. s = sc.socket();
  104. s.close();
  105. } catch( IOException ie ) {
  106. System.err.println( "Error closing socket "+s+": "+ie );
  107. }
  108. }
  109.  
  110. } catch( IOException ie ) {
  111.  
  112. // On exception, remove this channel from the selector
  113. key.cancel();
  114.  
  115. try {
  116. sc.close();
  117. } catch( IOException ie2 ) { System.out.println( ie2 ); }
  118.  
  119. System.out.println( "Closed "+sc );
  120. }
  121. }
  122. }
  123.  
  124. // We remove the selected keys, because we've dealt
  125. // with them.
  126. keys.clear();
  127. }
  128. } catch( IOException ie ) {
  129. System.err.println( ie );
  130. }
  131. }
  132.  
  133. // Do some cheesy encryption on the incoming data,
  134. // and send it back out
  135. private boolean processInput( SocketChannel sc ) throws IOException {
  136. buffer.clear();
  137. sc.read( buffer );
  138. buffer.flip();
  139.  
  140. // If no data, close the connection
  141. if (buffer.limit()==0) {
  142. return false;
  143. }
  144.  
  145. // Simple rot-13 encryption
  146. for (int i=0; i<buffer.limit(); ++i) {
  147. byte b = buffer.get( i );
  148.  
  149. if ((b>='a' && b<='m') || (b>='A' && b<='M')) {
  150. b += 13;
  151. } else if ((b>='n' && b<='z') || (b>='N' && b<='Z')) {
  152. b -= 13;
  153. }
  154.  
  155. buffer.put( i, b );
  156. }
  157.  
  158. sc.write( buffer );
  159.  
  160. System.out.println( "Processed "+buffer.limit()+" from "+sc );
  161.  
  162. return true;
  163. }
  164.  
  165. static public void main( String args[] ) throws Exception {
  166. int port = 7856;
  167. new Server( port );
  168. }
  169. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:7: error: class Server is public, should be declared in a file named Server.java
public class Server implements Runnable
       ^
1 error
stdout
Standard output is empty