-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNioSocketServer.java
More file actions
133 lines (116 loc) · 4.87 KB
/
Copy pathNioSocketServer.java
File metadata and controls
133 lines (116 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package codingTest;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class NioSocketServer
{
public NioSocketServer()
{
try
{
// Create an AsynchronousServerSocketChannel that will listen on port 5000
final AsynchronousServerSocketChannel listener =
AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(5000));
// Listen for a new request
listener.accept( null, new CompletionHandler<AsynchronousSocketChannel,Void>() {
@Override
public void completed(AsynchronousSocketChannel ch, Void att)
{
// Accept the next connection
listener.accept( null, this );
// Greet the client
ch.write( ByteBuffer.wrap( "Hello, I am Echo Server 2020, let's have an engaging conversation!\n".getBytes() ) );
// Allocate a byte buffer (4K) to read from the client
ByteBuffer byteBuffer = ByteBuffer.allocate( 4096 );
try
{
// Read the first line
int bytesRead = ch.read( byteBuffer ).get( 20, TimeUnit.SECONDS );
boolean running = true;
while( bytesRead != -1 && running )
{
System.out.println( "bytes read: " + bytesRead );
System.out.println( "running: " + running );
// Make sure that we have data to read
if( byteBuffer.position() > 2 )
{
// Make the buffer ready to read
byteBuffer.flip();
// Convert the buffer into a line
byte[] lineBytes = new byte[ bytesRead ];
byteBuffer.get( lineBytes, 0, bytesRead );
String line = new String( lineBytes );
// Debug
System.out.println( "Message: " + line );
// Echo back to the caller
ch.write( ByteBuffer.wrap( line.getBytes() ) );
// Make the buffer ready to write
byteBuffer.clear();
// Read the next line
bytesRead = ch.read( byteBuffer ).get( 20, TimeUnit.SECONDS );
}
else
{
// An empty line signifies the end of the conversation in our protocol
running = false;
}
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (ExecutionException e)
{
e.printStackTrace();
}
catch (TimeoutException e)
{
// The user exceeded the 20 second timeout, so close the connection
ch.write( ByteBuffer.wrap( "Good Bye\n".getBytes() ) );
System.out.println( "Connection timed out, closing connection" );
}
System.out.println( "End of conversation" );
try
{
// Close the connection if we need to
if( ch.isOpen() )
{
ch.close();
}
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
@Override
public void failed(Throwable exc, Void att) {
///...
}
});
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main( String[] args )
{
NioSocketServer server = new NioSocketServer();
try
{
Thread.sleep( 60000 );
}
catch( Exception e )
{
e.printStackTrace();
}
}
}