Created
December 21, 2012 08:05
-
-
Save He-Pin/4351360 to your computer and use it in GitHub Desktop.
the test code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.sosia.net.udt; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.SelectionKey; | |
import java.nio.channels.Selector; | |
import java.nio.channels.ServerSocketChannel; | |
import java.nio.channels.SocketChannel; | |
import java.util.Iterator; | |
import java.util.Set; | |
import com.barchart.udt.nio.SelectorProviderUDT; | |
public class NIOServerTest { | |
/** | |
* @param args | |
* @throws IOException | |
*/ | |
public static void main(String[] args) throws IOException { | |
Selector selector = SelectorProviderUDT.STREAM.openSelector(); | |
ServerSocketChannel channelServer = SelectorProviderUDT.STREAM.openServerSocketChannel(); | |
channelServer.configureBlocking(false); | |
channelServer.socket().bind(new InetSocketAddress(16670), 10); | |
channelServer.register(selector, SelectionKey.OP_ACCEPT); | |
for(;;){ | |
int selected = selector.select(); | |
if (selected >0) { | |
Set<SelectionKey> selectionKeys = selector.selectedKeys(); | |
Iterator<SelectionKey> iterator = selectionKeys.iterator(); | |
while (iterator.hasNext()) { | |
SelectionKey selectionKey = (SelectionKey) iterator.next(); | |
iterator.remove(); | |
if (selectionKey.isAcceptable()) { | |
ServerSocketChannel serverSocketChannel = (ServerSocketChannel)selectionKey.channel(); | |
SocketChannel socketChannel = serverSocketChannel.accept(); | |
socketChannel.configureBlocking(false); | |
socketChannel.register(selector, SelectionKey.OP_READ); | |
System.out.println("connected"); | |
continue; | |
} | |
if (selectionKey.isReadable()) { | |
SocketChannel socketChannel = (SocketChannel)selectionKey.channel(); | |
ByteBuffer byteBuffer = ByteBuffer.allocate(1024); | |
socketChannel.read(byteBuffer); | |
System.out.println(new String(byteBuffer.array())); | |
continue; | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.sosia.net.udt; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.SelectionKey; | |
import java.nio.channels.Selector; | |
import java.nio.channels.SocketChannel; | |
import java.util.Iterator; | |
import java.util.Set; | |
import com.barchart.udt.nio.SelectorProviderUDT; | |
public class NIOTest { | |
/** | |
* @param args | |
* @throws IOException | |
* @throws InterruptedException | |
*/ | |
public static void main(String[] args) throws IOException, InterruptedException { | |
// TODO Auto-generated method stub | |
Selector selectorUDT = SelectorProviderUDT.STREAM.openSelector(); | |
SocketChannel socketChannel = SelectorProviderUDT.STREAM.openSocketChannel(); | |
socketChannel.socket().bind(new InetSocketAddress(16671)); | |
socketChannel.configureBlocking(false); | |
socketChannel.register(selectorUDT, SelectionKey.OP_CONNECT); | |
socketChannel.connect(new InetSocketAddress("localhost", 16670)); | |
for(;;){ | |
if (selectorUDT.select()>0) { | |
Set<SelectionKey> selectionKeys = selectorUDT.selectedKeys(); | |
Iterator<SelectionKey> iterator = selectionKeys.iterator(); | |
while (iterator.hasNext()) { | |
SelectionKey selectionKey2 = (SelectionKey) iterator.next(); | |
iterator.remove(); | |
if (selectionKey2.isConnectable()) { | |
System.out.println("can connect"); | |
socketChannel.register(selectorUDT, SelectionKey.OP_WRITE); | |
socketChannel.finishConnect(); | |
break; | |
}else if (selectionKey2.isWritable()) { | |
System.out.println("can wirte"); | |
ByteBuffer byteBuffer = ByteBuffer.wrap("xajlsdfkjalsdfkjlasdfkjasdf".getBytes()); | |
socketChannel.write(byteBuffer); | |
} | |
} | |
} | |
//Thread.sleep(1000); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment