这几天在看mina
mina 基于socketChannel 和 DatagramChannel 建立的无阻塞链接。
所以就看了看socket channel 的使用方式,做一份备忘吧。
socketChannel 的使用方式
server端
package com.jimmy.nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Date;
public class TestServerSocketChannel {
public static void main(String[] args) throws IOException {
int port=37;
ByteBuffer in=ByteBuffer.allocate(4);
ByteBuffer out=ByteBuffer.allocate(8);
//设置绑定端口
SocketAddress address=new InetSocketAddress(port);
//Opens a server-socket channel.
ServerSocketChannel serverSocketChannel=ServerSocketChannel.open();
//Binds the ServerSocket to a specific address (IP address and port number).
serverSocketChannel.socket().bind(address);
System.err.println("bound to " + address);
//Accepts a connection made to this channel's socket
SocketChannel channel=serverSocketChannel.accept();
if(channel == null) {
return;
}
//clear byte buffer
in.clear();
//read byte to "in" buffer
channel.read(in);
//Flips this buffer
in.flip();
int a=in.getInt();
System.out.println("a:" + a);
//get remote socket address
SocketAddress client=channel.socket().getRemoteSocketAddress();
System.err.println(client);
long secondsSince1970=System.currentTimeMillis();
//clear "out" byte buffer
out.clear();
//put currentTimeMillis value into "out" byte buffer
out.putLong(secondsSince1970);
//Flips this buffer
out.flip();
//Writes a sequence of bytes to this channel from the given buffer
//write data from position to limit of buffer
//so before write ,buffer need call flip method
channel.write(out);
channel.close();
serverSocketChannel.close();
}
}
client 端
package com.jimmy.nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Date;
public class TestClientSocketChannel {
public static void main(String[] args) throws IOException {
int port=37;
SocketChannel channel=SocketChannel.open();
SocketAddress server=new InetSocketAddress("192.168.1.79", port);
channel.connect(server);
ByteBuffer buffer=ByteBuffer.allocate(4);
ByteBuffer readBuffer=ByteBuffer.allocate(8);
// send a byte of data to the server
buffer.putInt(100);
buffer.flip();
channel.write(buffer);
// get the buffer ready to receive data
channel.read(readBuffer);
readBuffer.flip();
// convert seconds since 1900 to a java.util.Date
long currentTimeMillis=readBuffer.getLong();
Date time=new Date(currentTimeMillis);
System.out.println(time);
channel.close();
}
}
socket channel 操作的对象是ByteBuffer
在read的时候如果byte length 超过了 byteBuffer分配的空间 会抛出java.nio.BufferUnderflowException 异常。
在设置 bytebuffer 空间的时候建议设置大一些。
分享到:
相关推荐
SocketChannel非阻塞网络编程
java的ServerSocketChannel与SocketChannel的使用
一个关于SocketChannel、ServerSocketChannel、Selector的综合案例
这是一个非阻塞通信学习的基础模板,让你轻松掌握非阻塞通信。里面还包含了可运行的jar包,可以抢先体验效果哦~_~
NIO SSL 与阻塞IO不同,JVM不提供扩展基本套接字通道类的标准SSLSocketChannel和SSLServerSocketChannel类。 相反,必须使用手动编排SSL交换。 该项目提供了和,可以像和一样使用。入门直接下载您可以直接下载并将其...
一个 Java SocketChannel 实现,它使用提供的 Proxy 实例通过提供的代理建立网络连接。 SocketChannel 是通过表面下的 Socket 实例实现的。 限制 此实现目前仅支持阻塞模式。 请注意,这是 SocketChannel 实例的默认...
Java NIO中的SocketChannel是一个连接到TCP网络套接字的通道。可以通过以下2种方式创建SocketChannel: 打开一个SocketChannel并连接到互联网上的某台服务器。 一个新连接到达ServerSocketChannel时,会创建一个...
没有使用其他jar包 主要功能: 1、广场群聊,即发送的消息所有在线的人都能看到 2、聊天室群聊,可以创建聊天室,进入聊天室的人可以在里面聊天,创建的人或者是第一个人为管理员,拥有踢人的权限;...
【IT十八掌徐培成】Java基础第27天-02.NIO-ServerSocketChannel-SocketChannel.zip
NIO(服务端和客户端代码) 博文链接:https://songjianyong.iteye.com/blog/1757406
利用Socket实现聊天室实时聊天功能,包含服务器端以及客户端,输入指定IP和端口号进行监控
● SocketChannel:Socket的替代类,支持阻塞通信与非阻塞通信。 ● Selector:为ServerSocketChannel监控接收连接就绪事件,为SocketChannel监控连接就绪、读就绪和写就绪事件。 ● SelectionKey:代表...
SocketChannel被Selector检查,检查其声称可以接受的状态是否已经产生,如当SocketChannel在向Selector注册是设置了可接受状态为Read,此时当SocketChannel接收到数据后将进入可读状态。 如果需要实现一个线程或...
socket 长连接 简单例子,适合初学的朋友,里面有多线程 实现的,包括心跳包,数据分为两部分传送,首先双方约定用一个4字节的数组告诉对方要传送数据的长度,然后在写入数据,这样长连接的时候,双方可以知道对方...
用JAVA写的一个FPT小程序,可实现简单的Ftp功能,Server端有Ui界面。
java socket 大文件传输,快速传输, 数据包的分片,组装,涉及UDP,TCP传输技术,NIO非阻塞等等,适合对socket编程进一步学习的同学
实现了客户端和服务器之间的通信,服务器是用socketChannel实现,
基于Mina的网络通讯,分为服务端和客户端。 研究selector NIO实现时,发现了这个架构。...Mina的底层实现实际就是selector和SocketChannel。所以如果对Mina源码感兴趣的可以先去看下selector相关的例子。
Java NIO系列教程 Java NIO Channel Buffer Selector SocketChannel
异步Nio 用于 Java 的 NIO 驱动的异步套接字通道关于AsyncNio 是一个 JDK7+ 兼容的java.nio.channels.AsynchronousSocketChannels实现。 由古老的java.nio.channels.SocketChannels包系列提供支持。...