java nio为什么是通道(三): 网络Channel

网络channel中有三个主要的实现类,即ServerSocketChannel,SocketChannel,DatagramChannel,我们主要来看看前两个类的使用。

服务器端的使用经常会考虑到非阻塞socket通道,因为他们使同时管理很多socket通道变得更容易。

在设置了通道的阻塞模式之后,如果在使用过程当中需要修改通道的阻塞模式,那么首先必须通过通道的blockingLock()方法获取一个锁对象,然后在拥有这个所对象后才可以改变通道的阻塞模式:

Object lockObj = serverChannel.blockingLock();

synchronized(lockObj) {

boolean prevState = serverChannel.isBlocking();

serverChannel.configureBlocking(false);

socket = serverChannel.accept();

serverChannel.configureBlocking(prevState);

}

ServerSocketChannel:用静态的open()工厂方法创建一个新的ServerSocketChannel对象,将会返回同一个未绑定的java.net.ServerSocket关联的通道,可以通过socket()方法获取一个ServerSocket对象,但是这个ServerSocke对象依赖于通道实现:

ServerSocketChannel ssc = ServerSocketChannel.open();

ServerSocket serverSocket = ssc.socket();

serverSocket.bind(new InetSocketAddress(1234));

在绑定了端口之后,就可以调用通道的accept()方法来返回一个SocketChannel对象了,但是当没有传入连接在等待时,调用accept()方法会返回null。

package com.nio.channel;


import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;


public class ChannelAccept {


public static final String GREETING = "Hello I must be going.";

/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
int port = 1234;
if(args.length > 0) {
port = Integer.parseInt(args[0]);
}
ByteBuffer buffer = ByteBuffer.wrap(GREETING.getBytes());
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(port));
ssc.configureBlocking(false);
while(true) {
System.out.println("waiting for connections...");
SocketChannel sc = ssc.accept();
if(sc == null) {
Thread.sleep(1000);
} else {
System.out.println("Incoming connection from : " + sc.socket().getRemoteSocketAddress());
buffer.rewind();
sc.write(buffer);
sc.close();
}
}
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值