Java NIO连接socket

NIO连接socket
一个是服务器端,一个是客户端,都是用NIO连接的,代码如下

package testnio;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Set;


public class Receive {

public static void main(String[] args) throws Exception {
boolean b = true;
ByteBuffer buffer = ByteBuffer.allocate(1024);
ServerSocketChannel ss = ServerSocketChannel.open();
ss.socket().bind(new InetSocketAddress(8888));
ss.configureBlocking(false);
Selector se = Selector.open();
ss.register(se, SelectionKey.OP_ACCEPT);
while (se.select() > 0) {
Set<SelectionKey> set = se.selectedKeys();
System.out.println("进入一个循环,大小是:" + set.size());
for (SelectionKey key : set) {
int ops = key.readyOps();
System.out.println("ops=" + ops);
if ((ops & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {
SocketChannel sc = ss.accept();
System.err.println("有新的连接了" + sc);
System.err.println("地址是:" + sc.socket());
sc.configureBlocking(false);
sc.register(se, SelectionKey.OP_READ);
}
if ((ops & SelectionKey.OP_READ) == SelectionKey.OP_READ) {
System.err.println("有新的读取");
SocketChannel sc = (SocketChannel) key.channel();
System.out.println(sc.isConnected());
sc.read(buffer);
buffer.flip();
//System.out.println(new String(buffer.array()));
Thread.sleep(5000);
if (b) {
b = false;
sc.write(buffer);
}

}
}
set.clear();
System.out.println("退出循环");
}

}
}

客户端:

package testnio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Set;


public class Send {

public static void main(String[] args) throws Exception {
SocketChannel sc = SocketChannel.open();
ByteBuffer buffer = ByteBuffer.allocate(1024);
Selector se = Selector.open();
buffer.put("我是中国人,我爱我的祖国,hadeslee".getBytes());
buffer.flip();

sc.configureBlocking(false);
sc.register(se, SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE);
sc.connect(new InetSocketAddress("172.0.0.1", 8888));
while(!sc.finishConnect());
sc.write(buffer);
System.out.println("进入循环");
Thread.sleep(10000);
int sum = se.select();
while (se.select() > 0) {
Thread.sleep(100);

System.out.println("终于大于0了");
Set<SelectionKey> set = se.selectedKeys();
System.out.println("大小是:"+set.size());
for (SelectionKey key : set) {
int ops = key.readyOps();
if ((ops & SelectionKey.OP_CONNECT) == SelectionKey.OP_CONNECT) {
sc.write(buffer);
System.out.println("连接成功");
}
if ((ops & SelectionKey.OP_READ) == SelectionKey.OP_READ) {
System.out.println(" 收到东西");
sc.read(buffer);
buffer.flip();
System.out.println("收到的是:" + new String(buffer.array(),0,buffer.limit()));
sc.write(buffer);
}
}
se.selectedKeys().clear();
}
}

private static ByteBuffer[] get(String heads) {
ByteBuffer[] bbs = new ByteBuffer[heads.length];
for (int i = 0; i < bbs.length; i++) {
String s = heads[i];
bbs[i] = ByteBuffer.allocateDirect(1024);
bbs[i].put(s.getBytes());
bbs[i].flip();
}
return bbs;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值