NIO實現UDP資料傳輸
最近用NIO寫了一個UDP傳輸,伺服器和客戶端都存在,測試了併發數至少1000
NIO寫UDP----server
用NIO寫UDP
/////////serverTest.java
import java.io.*;
import java.net.*;
import java.nio.*;
import java.lang.*;
import java.nio.channels.*;
import java.util.*;
import test.control.ConnProtocol;
import test.util.Constant;
public class ServerTest implements Runnable
{
private int port;
public ServerTest( int port ) {
this.port = port;
new Thread( this ).start();
}
public void run() {
try {
//建立
DatagramChannel dc = DatagramChannel.open();
dc.configureBlocking( false ) ;
SocketAddress address = new InetSocketAddress(port ) ;
//本地繫結埠
DatagramSocket ds = dc.socket() ;
ds.setReceiveBufferSize(20480);
ds.bind( address ) ;
//註冊
Selector select = Selector.open() ;
dc.register( select , SelectionKey.OP_READ ) ;
System.out.println( "Listening on port "+port );
ByteBuffer buffer = ByteBuffer.allocateDirect( Constant.BUFFERSIZE ) ;
int number = 0; //只為記錄接受的位元組數
while(true){
int num = select.select() ;
//如果選擇器數目為0,則結束迴圈
if (num == 0) {
continue;
}
//得到選擇鍵列表
Set Keys = select.selectedKeys() ;
Iterator it = Keys.iterator() ;
while( it.hasNext() ){
SelectionKey k = ( SelectionKey )it.next() ;
if( ( k.readyOps() & SelectionKey.OP_READ )
== SelectionKey.OP_READ ){
DatagramChannel cc = ( DatagramChannel )k.channel() ;
//非阻塞
cc.configureBlocking(false);
//接收資料並讀到buffer中
buffer.clear();
SocketAddress client = cc.receive( buffer ) ;
buffer.flip();
if(buffer.remaining()<=0)
{System.out.println("bb is null");}
//記錄接收到的位元組總數
number += buffer.remaining();
byte b[] =new byte[buffer.remaining()];
for(int i =0;i<buffer.remaining();i++) {
b[i] = buffer.get(i);
}
String in = new String(b,"gb2312");
System.out.println("number::::"+number);
//執行操作,並回傳送
//……省略……
}
}
Keys.clear();
}
} catch( IOException ie ) {
System.err.println( ie );
}
}
static public void main( String args[] ) throw* **ception {
int port = 1111;//Integer.parseInt( args[0] );
new ServerTest( port );
}
}
/////////////NIO寫UDP----client
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
public class ClientTest extends Thread{
private String host;
private int port;
int j = 0;
int number;
public ClientTest( String host, int port, int numThreads ) {
this.host = host;
this.port = port;
for (int i=0; i<numThreads; ++i) {
new Thread( this ).start();
/*
try
{
sleep(100);
}
catch (Exception e)
{
}
*/
}
}
public void run(){
//構造一個數據報Socket
DatagramChannel dc = null;
try {
dc = DatagramChannel.open();
}
catch (IOException ex4) {
}
SocketAddress address = new InetSocketAddress(host, port);
try {
dc.connect(address);
}
catch (IOException ex) {
}
//dc = Socket.getChannel();
//傳送請求
ByteBuffer bb = ByteBuffer.allocate(130);
byte[] b = new byte[130];
String s = "sdfas";
s = "sss";
b = s.getBytes();
bb.clear();
bb.put(b);
bb.flip();
//測試
if(bb.remaining()<=0)
{System.out.println("bb is null");}
//while(true) {
try {
int num = dc.send(bb,address);
number = number + num;
System.out.println("number:::"+number);
bb.clear();
dc.receive(bb);
bb.flip();
byte [] by = new byte[bb.remaining()];
for(int i=0 ;i<bb.remaining();i++ ){
by[i] = bb.get(i);
}
String ss = new String(by,"gb2312");
System.out.println(ss);
}
catch (Exception ex1) {
}
//}
}
//start
public static void main(String[] args) {
String host = "127.0.0.1";//args[0];
int port = 1111;//Integer.parseInt( args[1] );
int numThreads = 3;//Integer.parseInt( args[2] );
new ClientTest( host, port, numThreads );
}
}