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

一、回传协议接口和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的语法,大家可以关注:《Thinking in Java 中文手册》、《JDK 1.7 参考手册官方英文版》、《JDK 1.6 API java 中文参考手册》、《JDK 1.5 API java 中文参考手册》,也希望大家多多支持我们。

(0)

相关推荐

  • Java基于Socket实现网络编程实例详解

    一,网络编程中两个主要的问题 一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后如何可靠高效的进行数据传输. 在TCP/IP协议中IP层主要负责网络主机的定位,数据传输的路由,由IP地址可以唯一地确定Internet上的一台主机. 而TCP层则提供面向应用的可靠(tcp)的或非可靠(UDP)的数据传输机制,这是网络编程的主要对象,一般不需要关心IP层是如何处理数据的. 目前较为流行的网络编程模型是客户机/服务器(C/S)结构.即通信双方一方作为服务器等待客户提出请求并予以响应.客户则

  • Java Socket编程实例(一)- TCP基本使用

    一.服务端代码: import java.net.*; // for Socket, ServerSocket, and InetAddress import java.io.*; // for IOException and Input/OutputStream public class TCPEchoServer { private static final int BUFSIZE = 32; // Size of receive buffer public static void main

  • Java编程小实例—数字时钟的实现代码示例

    本文的实例是Java编程实现一个数字时钟,代码测试可用,练练手吧.代码如下: package me.socketthread; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Calendar; import java.util.GregorianCalenda

  • Java Socket编程实例(四)- NIO TCP实践

    一.回传协议接口和TCP方式实现: 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(

  • Java Socket编程实例(二)- UDP基本使用

    一.服务端代码: import java.io.*; import java.net.*; public class UDPEchoServer { private static final int ECHOMAX = 255; // Maximum size of echo datagram public static void main(String[] args) throws IOException { int servPort = 5500; // Server port Datagr

  • 实现了基于TCP的Java Socket编程实例代码

    实现了基于TCP的Java Socket编程,功能很简单:客户端向服务器端输出一名话"connect",服务器端接收输出到控制台并向客户端输出一名话"Hello",客户端接收并输出. 1.服务器端 复制代码 代码如下: package javase.net.socket; import java.io.DataInputStream;  import java.io.DataOutputStream;  import java.io.IOException;  im

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

    一.回传协议接口和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(

  • java socket编程实例代码讲解

    1.所谓socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络发出请求或者应答网络请求. 操作java socket时用到的最多的三个方法为: accept():主要用于服务器端产生"阻塞",等待客户端的链接请求,并且返回一个客户端的Socket实例: getInputStream():方法主要用来获得网络连接输入,同时返回一个InputStream对象实例: getOutputStream

  • Java Socket编程(五) 简单的WEB服务器

    文章来源:aspcn 作者:孙雯 简单的WEB服务器 一个简单的WEB服务器将由列表9.2这样构建.当然,还必须要对方法和回应事件进行改进.简单的服务器不会分析和存储请求头.新的WEB服务器将分析和存储请求,为以后的处理作准备.为了达到这个目的,你必须有一个包含HTTP请求的类. HTTPrequest类 列表9.5列出了一个完整的HTTPrequest类.这个类必须包括一个请求头所需的所有信息. 列表9.5.HTTPrequest类. import java.io.*; import java

  • Java Socket编程实例(三)- TCP服务端线程池

    一.服务端回传服务类: import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; public class EchoProtocol implements Runnable { private static f

  • Java Socket编程心跳包创建实例解析

    1.什么是心跳包? 心跳包就是在客户端和服务器间定时通知对方自己状态的一个自己定义的命令字,按照一定的时间间隔发送,类似于心跳,所以叫做心跳包. 用来判断对方(设备,进程或其它网元)是否正常运行,采用定时发送简单的通讯包,如果在指定时间段内未收到对方响应,则判断对方已经离线.用于检测TCP的异常断开.基本原因是服务器端不能有效的判断客户端是否在线,也就是说,服务器无法区分客户端是长时间在空闲,还是已经掉线的情况.所谓的心跳包就是客户端定时发送简单的信息给服务器端告诉它我还在而已.代码就是每隔几分

  • Java Socket编程服务器响应客户端实例代码

    通过输入流来读取客户端信息,相应的时候通过输出流来实现. 服务端类的代码: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; impo

随机推荐