| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 795 人关注过本帖
标题:小弟刚刚开始学习java网络编程,麻烦看看这篇代码问题在哪?(我是在一本书 ...
只看楼主 加入收藏
scymore
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-11-3
结帖率:0
收藏
已结贴  问题点数:5 回复次数:3 
小弟刚刚开始学习java网络编程,麻烦看看这篇代码问题在哪?(我是在一本书上敲过来的,确认没有地方输错!)谢谢各位!
程序代码:
package AIO;

import import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AIOServer 
{
    static final int PORT = 30000 ;
    final static String UTF_8 = "utf-8" ;
    static List<AsynchronousSocketChannel> channelList = new ArrayList<>();
    public void startListen() throws InterruptedException , Exception
    {
        //创建一个线程池
        ExecutorService executor = Executors.newFixedThreadPool(20);
        //以指定线程池来创建一个AsynchronousChannelGroup
        AsynchronousChannelGroup channelGroup = AsynchronousChannelGroup.withThreadPool(executor);
        //以指定线程池来创建一个AsynchronousServerSocketChannel
        AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open(channelGroup).bind(new InetSocketAddress(PORT));
        serverChannel.accept(null, new AcceptHandler(serverChannel));
    }
    public static void main(String args[]) throws Exception
    {
        AIOServer server = new AIOServer();
        server.startListen();
    }
}
//实现自己的CompletionHandler类
class AcceptHandler implements CompletionHandler<AsynchronousSocketChannel,Object>
{
    private AsynchronousServerSocketChannel serverChannel ;
    public AcceptHandler(AsynchronousServerSocketChannel sc)
    {
        this.serverChannel = sc ;
    }
    //定义一个ByteBuffer准备读取数据
    ByteBuffer buff = ByteBuffer.allocate(1024);
    //当实际IO操作完成时触发该方法
    @Override
    public void completed(final AsynchronousSocketChannel sc , Object attachment)
    {
        AIOServer.channelList.add(sc);
        serverChannel.accept(null , this );
        sc.read(buff, null, new CompletionHandler<Integer,object>()
                {
            public void completed (Integer result , Object attachment)
            {
                buff.flip();
                //将buff中的内容转换为字符串
                String content = StandardCharsets.UTF_8.decode(buff).toString();
                //遍历每个Channel,将收到的信息写入各Chaneel中
                for(AsynchronousSocketChannel c : AIOServer.channelList)
                {
                    try
                    {
                        c.write(ByteBuffer.wrap(content.getBytes(AIOServer.UTF_8))).get();
                    }
                    catch(Exception ex )
                    {
                        ex.printStackTrace();
                    }
                }
                buff.clear();
                sc.read(buff,null,this);
            }
            @Override
            public void failed(Throwable ex ,Object attachment)
            {
                System.out.println("读取数据失败:"+ex);
                //从该Channel中读取数据失败,就将该Channel删除
                AIOServer.channelList.remove(sc);
            }
                });
        }
    @Override
    public void failed(Throwable ex , Object attachment )
    {
        System.out.println("连接失败:"+ex);
    }
}
搜索更多相关主题的帖子: 网络编程 java 
2013-11-06 22:04
经哥
Rank: 3Rank: 3
来 自:代码空间
等 级:论坛游侠
威 望:1
帖 子:289
专家分:124
注 册:2012-9-8
收藏
得分:3 
failed好像是关键字,这里是方法吧?换一个试试?

我只是个演员,还是业余的!!
2013-11-07 20:43
xstar海绵
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:26
专家分:160
注 册:2013-11-3
收藏
得分:3 
sc.read(buff, null, new CompletionHandler<Integer,object>() Object 的O需要大写 。。。
2013-11-12 23:07
jinshuang93
Rank: 1
等 级:新手上路
帖 子:6
专家分:2
注 册:2013-12-10
收藏
得分:0 
java的代码很麻烦的  而且以后是php的时代  php正在逐步取代java  可以学下php  要比java简单的很多。
2013-12-10 18:50
快速回复:小弟刚刚开始学习java网络编程,麻烦看看这篇代码问题在哪?(我是在一 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019217 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved