• 无题2


    package nio;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SocketChannel;
    
    public class SocketChannelDemo {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
                SocketChannel socketChannel = SocketChannel.open();
                socketChannel.connect(new InetSocketAddress("127.0.0.1", 9999));
    
                ByteBuffer bb = ByteBuffer.allocate(48);
                int byteNum;
    
                int bytes = socketChannel.read(bb);
                bb.flip();
                while (bb.hasRemaining()) {
                    System.out.print((char) bb.get());
                }
                bb.clear();
                
                socketChannel.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
    View Code
    package nio;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    
    public class ServerSocketChannelDemo {
        public static void main(String[] args) {
    
            try {
                ServerSocketChannel ssc = ServerSocketChannel.open();
                ssc.socket().bind(new InetSocketAddress(9999));
                String msg = "send something to you";
                ByteBuffer bb = ByteBuffer.allocate(48);
                bb.clear();
                bb.put(msg.getBytes());
                bb.flip();
                while (true) {
                    SocketChannel socketChannel = ssc.accept();
                    while (bb.hasRemaining()) {
                        socketChannel.write(bb);
                    }
                    socketChannel.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    }
    View Code

    我一直在想为什么socket客户端只能读一次或得返回信息,原来是我server端写错了,应该把设置byteBuffer内容的这段代码写在 while循环里

    如下:

      while (true) {
                    SocketChannel socketChannel = ssc.accept();
                    String msg = "send something to you";
                    ByteBuffer bb = ByteBuffer.allocate(48);
                    bb.clear();
                    bb.put(msg.getBytes());
                    bb.flip();
                    while (bb.hasRemaining()) {
                        socketChannel.write(bb);
                    }
    //                socketChannel.close();
                }

        sc.connect(new InetSocketAddress("127.0.01",9999));          //可耻的错误

  • 相关阅读:
    Java将对象保存到文件中/从文件中读取对象
    Python爬虫框架--Scrapy安装以及简单实用
    Python--网络爬虫模块requests模块之响应--response
    用Pycharm创建指定的Django版本
    python网络爬虫之requests模块
    Python---异常处理
    Python函数的装饰器修复技术(@wraps)
    Django Rest framework
    Vue的基础使用
    Vue
  • 原文地址:https://www.cnblogs.com/jarman/p/6189561.html
Copyright © 2020-2023  润新知