免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3315 | 回复: 1

SocketChannel 例子 [复制链接]

论坛徽章:
0
发表于 2012-03-19 20:42 |显示全部楼层

SocketChannel 例子










这几天在看mina  
mina 基于socketChannel 和 DatagramChannel 建立的无阻塞链接。

所以就看了看socket channel 的使用方式,做一份备忘吧。
socketChannel 的使用方式

server端

  1. package com.jimmy.nio;

  2. import java.io.IOException;
  3. import java.net.InetSocketAddress;
  4. import java.net.SocketAddress;
  5. import java.nio.ByteBuffer;
  6. import java.nio.ByteOrder;
  7. import java.nio.channels.ServerSocketChannel;
  8. import java.nio.channels.SocketChannel;
  9. import java.util.Date;

  10. public class TestServerSocketChannel {
  11.     public static void main(String[] args) throws IOException {
  12.         int port=37;

  13.         ByteBuffer in=ByteBuffer.allocate(4);
  14.         ByteBuffer out=ByteBuffer.allocate(8);
  15.         
  16.         //设置绑定端口
  17.         SocketAddress address=new InetSocketAddress(port);

  18.         //Opens a server-socket channel.
  19.         ServerSocketChannel serverSocketChannel=ServerSocketChannel.open();

  20.         //Binds the ServerSocket to a specific address (IP address and port number).
  21.          serverSocketChannel.socket().bind(address);

  22.         System.err.println("bound to " + address);
  23.         
  24.         //Accepts a connection made to this channel's socket
  25.         SocketChannel channel=serverSocketChannel.accept();
  26.         if(channel == null) {
  27.             return;
  28.         }
  29.         //clear byte buffer
  30.         in.clear();
  31.         //read byte to "in" buffer
  32.         channel.read(in);
  33.         //Flips this buffer
  34.         in.flip();
  35.         int a=in.getInt();
  36.         System.out.println("a:" + a);

  37.         //get remote socket address
  38.         SocketAddress client=channel.socket().getRemoteSocketAddress();
  39.         System.err.println(client);
  40.         long secondsSince1970=System.currentTimeMillis();

  41.         //clear "out" byte buffer
  42.         out.clear();
  43.         //put currentTimeMillis value into "out" byte buffer
  44.         out.putLong(secondsSince1970);
  45.         //Flips this buffer
  46.         out.flip();

  47.         //Writes a sequence of bytes to this channel from the given buffer
  48.         //write data from position to limit of buffer
  49.         //so before write ,buffer need call flip method
  50.         channel.write(out);

  51.         channel.close();

  52.         serverSocketChannel.close();
  53.     }
  54. }
复制代码
client 端
  1. package com.jimmy.nio;

  2. import java.io.IOException;
  3. import java.net.InetSocketAddress;
  4. import java.net.SocketAddress;
  5. import java.nio.ByteBuffer;
  6. import java.nio.channels.SocketChannel;
  7. import java.util.Date;

  8. public class TestClientSocketChannel {

  9.     public static void main(String[] args) throws IOException {
  10.         int port=37;
  11.         SocketChannel channel=SocketChannel.open();
  12.         SocketAddress server=new InetSocketAddress("192.168.1.79", port);
  13.         channel.connect(server);

  14.         ByteBuffer buffer=ByteBuffer.allocate(4);
  15.         ByteBuffer readBuffer=ByteBuffer.allocate(8);
  16.         
  17.         // send a byte of data to the server
  18.         buffer.putInt(100);
  19.         buffer.flip();
  20.         channel.write(buffer);

  21.         // get the buffer ready to receive data
  22.         channel.read(readBuffer);
  23.         readBuffer.flip();

  24.         // convert seconds since 1900 to a java.util.Date
  25.         long currentTimeMillis=readBuffer.getLong();

  26.         Date time=new Date(currentTimeMillis);

  27.         System.out.println(time);

  28.         channel.close();
  29.     }
  30. }
复制代码
socket channel 操作的对象是ByteBuffer
在read的时候如果byte length 超过了 byteBuffer分配的空间 会抛出java.nio.BufferUnderflowException 异常。
在设置 bytebuffer 空间的时候建议设置大一些。

论坛徽章:
0
发表于 2012-03-19 20:42 |显示全部楼层
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP