欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java Socket编程实例(五)- NIO UDP实践

时间:2021-09-21 08:36:06|栏目:JAVA代码|点击:

一、回传协议接口和UDP方式实现:

1.接口:

import java.nio.channels.SelectionKey; 
import java.io.IOException; 
 
public interface EchoProtocol { 
 void handleAccept(SelectionKey key) throws IOException; 
 void handleRead(SelectionKey key) throws IOException; 
 void handleWrite(SelectionKey key) throws IOException; 
} 

2.实现:

import java.net.SocketAddress; 
import java.nio.channels.*; 
import java.nio.ByteBuffer; 
import java.io.IOException; 
 
public class UDPEchoSelectorProtocol implements <span style="font-size: 1em; line-height: 1.5;">EchoProtocol </span><span style="font-size: 1em; line-height: 1.5;">{</span> 
  private static final int ECHOMAX = 255; // Maximum size of echo datagram 
 
  static class ClientRecord { 
    public SocketAddress clientAddress; 
    public ByteBuffer buffer = ByteBuffer.allocate(ECHOMAX); 
  } 
 
  public void handleAccept(SelectionKey key) throws IOException { 
     
  } 
 
  public void handleRead(SelectionKey key) throws IOException { 
    DatagramChannel channel = (DatagramChannel) key.channel(); 
    ClientRecord clntRec = (ClientRecord) key.attachment(); 
    clntRec.buffer.clear(); // Prepare buffer for receiving 
    clntRec.clientAddress = channel.receive(clntRec.buffer); 
    if (clntRec.clientAddress != null) { // Did we receive something? 
      // Register write with the selector 
      key.interestOps(SelectionKey.OP_WRITE); 
    } 
  } 
 
  public void handleWrite(SelectionKey key) throws IOException { 
    DatagramChannel channel = (DatagramChannel) key.channel(); 
    ClientRecord clntRec = (ClientRecord) key.attachment(); 
    clntRec.buffer.flip(); // Prepare buffer for sending 
    int bytesSent = channel.send(clntRec.buffer, clntRec.clientAddress); 
    if (bytesSent != 0) { // Buffer completely written? 
      // No longer interested in writes 
      key.interestOps(SelectionKey.OP_READ); 
    } 
  } 
 
}

二、NIO UDP客户端:

import java.net.InetSocketAddress; 
import java.net.SocketException; 
import java.nio.ByteBuffer; 
import java.nio.channels.DatagramChannel; 
 
public class UDPEchoClientNonblocking { 
 
  private static final int TIMEOUT = 3000; // Resend timeout (milliseconds) 
  private static final int MAXTRIES = 255; // Maximum retransmissions 
   
  public static void main(String args[]) throws Exception { 
    // Convert input String to bytes using the default charset 
    byte[] bytesToSend = "0123456789abcdefghijklmnopqrstuvwxyz".getBytes(); 
 
    // Create channel and set to nonblocking 
    DatagramChannel datagramChannel = DatagramChannel.open(); 
    datagramChannel.configureBlocking(false); 
    datagramChannel.socket().setSoTimeout(TIMEOUT); 
     
    ByteBuffer writeBuf = ByteBuffer.wrap(bytesToSend); 
    ByteBuffer readBuf = ByteBuffer.allocate(MAXTRIES); 
     
    datagramChannel = datagramChannel.connect(new InetSocketAddress("127.0.0.1", 5500)); 
 
    int totalBytesRcvd = 0; // Total bytes received so far 
    int bytesRcvd; // Bytes received in last read 
    while (totalBytesRcvd < bytesToSend.length) { 
      if (writeBuf.hasRemaining()) { 
        datagramChannel.write(writeBuf); 
      } 
      if ((bytesRcvd = datagramChannel.read(readBuf)) == -1) { 
        throw new SocketException("Connection closed prematurely"); 
      } 
      totalBytesRcvd += bytesRcvd; 
      System.out.print("."); // Do something else 
    } 
 
    System.out.println("Received: " + new String(readBuf.array(), 0, totalBytesRcvd)); 
    datagramChannel.close(); 
  } 
}

三、NIO UDP服务端:

import java.io.IOException; 
import java.net.InetSocketAddress; 
import java.nio.channels.*; 
import java.util.Iterator; 
 
public class UDPEchoServerSelector { 
 
  private static final int TIMEOUT = 3000; // Wait timeout (milliseconds) 
 
  public static void main(String[] args) throws IOException { 
    // Create a selector to multiplex client connections. 
    Selector selector = Selector.open(); 
 
    DatagramChannel channel = DatagramChannel.open(); 
    channel.configureBlocking(false); 
    channel.socket().bind(new InetSocketAddress(5500)); 
    channel.register(selector, SelectionKey.OP_READ, new UDPEchoSelectorProtocol.ClientRecord()); 
 
    UDPEchoSelectorProtocol echoSelectorProtocol = new UDPEchoSelectorProtocol(); 
    while (true) { // Run forever, receiving and echoing datagrams 
      // Wait for task or until timeout expires 
      if (selector.select(TIMEOUT) == 0) { 
        System.out.print("."); 
        continue; 
      } 
 
      // Get iterator on set of keys with I/O to process 
      Iterator<SelectionKey> keyIter = selector.selectedKeys().iterator(); 
      while (keyIter.hasNext()) { 
        SelectionKey key = keyIter.next(); // Key is bit mask 
 
        // Client socket channel has pending data? 
        if (key.isReadable()) 
          echoSelectorProtocol.handleRead(key); 
 
        // Client socket channel is available for writing and 
        // key is valid (i.e., channel not closed). 
        if (key.isValid() && key.isWritable()) 
          echoSelectorProtocol.handleWrite(key); 
 
        keyIter.remove(); 
      } 
    } 
  } 
 
}

上一篇:教你构建第一个Java Applet程序

栏    目:JAVA代码

下一篇:Java链表中元素删除的实现方法详解【只删除一个元素情况】

本文标题:Java Socket编程实例(五)- NIO UDP实践

本文地址:http://www.codeinn.net/misctech/176433.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有