UDP::
1.接口:
导入java.nio.channels.selectionkey;导入java.io.ioexception;公共接口Echoprotocol {void handleaccept(selectionkey键)抛出ioexception; void handleread(selectionkey键)抛出ioexception; void handlewrite(selectionkey键)抛出ioexception; }2.实现::
导入java.net.socketAddress;导入Java.nio.Channels。*;导入Java.nio.Bytebuffer;导入java.io.ioexception;公共类UdpechoselectorProtocol实施<span style =“ font-size:1em; line-height:1.5;“> echoprotocol </span> <span style =” font-size:1em; line-height; line-height:1.5;“> {</> {</> {</> {</> private static static static final int echomax = 255; //回波数据报静态类客户端{public socketAddress clientaddress; public bytebuffer buffer = bytebuffer.allocate(echomax); } public void handleaccept(selectionkey键)抛出ioexception {} public void handleread(selectionkey键)抛出ioexception {datagramChannel channel =(datagramChannel)key.channel(); clientRecord clntrec =(clientRecord)key.attachment(); clntrec.buffer.clear(); //准备接收clntrec.clientaddress = Channel.receive(clntrec.buffer)的缓冲区; if(clntrec.clientaddress!= null){//我们收到了一些东西吗? //注册在selector key.interestops(selectionkey.op_write)上写入; }} public void handlewrite(selectionkey键)抛出ioexception {datagramChannel channel =(datagramChannel)key.channel(); clientRecord clntrec =(clientRecord)key.attachment(); clntrec.buffer.flip(); //准备用于发送int bytessent = channel.send的缓冲区(clntrec.buffer,clntrec.clientaddress);如果(bytessent!= 0){//缓冲区完全编写? //不再对writes key.interestops(selectionkey.op_read)感兴趣; }}}}二、 nio udp::
导入java.net.inetsocketaddress;导入java.net.socketException;导入Java.nio.Bytebuffer;导入Java.nio.Channels.DatagramChannel;公共类Udpechoclientnonblocking {私有静态最终int超时= 3000; //重新启动超时(毫秒)私有静态最终int maxtries = 255; //使用默认的charset byte [] bytestosend =“ 0123456789ABCDEFGHIJKLMNOPQRMNOPQRSTUVWXYZ” .getBytes()()()()(),最大重新转载式公共静态void main(String args [])抛出异常{//将输入字符串转换为字节。 //创建频道并将设置为非块datagramChannel datagramChannel = datagramChannel.open(); datagramChannel.configureBlocking(false); datagramChannel.socket()。setSotimeout(timeout); bytebuffer writebuf = bytebuffer.wrap(bytestosend); bytebuffer dechbuf = bytebuffer.allocate(maxtries); datagramChannel = datagramChannel.connect(new InetSocketAddress(“ 127.0.0.1”,5500)); int totalbytesrcvd = 0; //到目前为止收到的总字节int bytesrcvd; //在上次读取中收到的字节(totalBytesrcvd <bytestosend.length){if(writebuf.hasremaining()){datagramChannel.write(writebuf); } if(((bytesrcvd = datagramChannel.read(readbuf)))== -1){投掷new SocketException(“连接过早关闭”); } totalbytesrcvd += bytesrcvd; system.out.print(“。”); //做其他事情} system.out.println(“接收:” + new String(dectBuf.Array(),0,totalbytesrcvd)); datagramChannel.close(); }}}三、 nio udp::
导入java.io.ioexception;导入java.net.inetsocketaddress;导入Java.nio.Channels。*;导入java.util.iterator;公共类udpechoserverselector {私有静态最终int timout = 3000; //等待超时(毫秒)public static void main(string [] args)抛出ioexception {//创建一个选择器以多路复用客户端连接。选择器selector = selector.open(); datagramChannel Channel = datagramChannel.open(); channel.configureblocking(false); channel.socket()。绑定(new InetSocketAddress(5500)); Channel.Register(selector,selectionKey.op_read,新的udpechoselectorprotocol.clientrecord()); udpechoselectorProtocol echoselectorProtocol =新的udpechoselectorProtocol(); while(true){//永远运行,接收和回声数据报//等待任务或直到超时到期if(selector.select(timeout)== 0){system.out.out.print(“。”);继续; } //用I/O在键上获取迭代器以处理iTerator <SelectionKey> keyiter = selector.selectedkeys()。iterator(); while(keyiter.hasnext()){selectionkey key = keyiter.next(); //键是bit mask //客户端套接字通道有待处理的数据吗? if(key.isradable())echoselectorProtocol.handleread(key); //客户端套接字通道可用于编写,//键是有效的(即,频道未关闭)。 if(key.isvalid()&& key.iswritable())echoselectorProtocol.handleWrite(key); keyiter.remove(); }}}}}以上就是本文的全部内容,java的语法,大家可以关注:《在Java jdk 1.7 参考手册官方英文版》、《 jdk 1.6 api java jdk jdk jdk jdk jdk 1.5 api java 中文参考手册》,也希望大家多多支持武林网。,也希望大家多多支持武林网。