`
hougechuanqi
  • 浏览: 73686 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

socket混合使用阻塞模式和非阻塞模式

阅读更多
server:
package thread2;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.net.*;
import java.util.*;

public class EchoServer{
  private Selector selector = null;
  private ServerSocketChannel serverSocketChannel = null;
  private int port = 8000;
  private Charset charset=Charset.forName("GBK");

  public EchoServer()throws IOException{
    selector = Selector.open();
    serverSocketChannel= ServerSocketChannel.open();
    serverSocketChannel.socket().setReuseAddress(true);
    serverSocketChannel.socket().bind(new InetSocketAddress(port));
    System.out.println("服务器启动");
  }

  public void accept(){
      for(;;){
        try{
            SocketChannel socketChannel = serverSocketChannel.accept();
            System.out.println("接收到客户连接,来自:" +
                               socketChannel.socket().getInetAddress() +
                               ":" + socketChannel.socket().getPort());
            socketChannel.configureBlocking(false);

            ByteBuffer buffer = ByteBuffer.allocate(1024);
            synchronized(gate){
                selector.wakeup();
                socketChannel.register(selector,
                                       SelectionKey.OP_READ |
                                       SelectionKey.OP_WRITE, buffer);
            }
        }catch(IOException e){e.printStackTrace();}
      }
  }
  private Object gate=new Object();
  public void service() throws IOException{
    for(;;){
      synchronized(gate){}
      int n = selector.select();

      if(n==0)continue;
      Set readyKeys = selector.selectedKeys();
      Iterator it = readyKeys.iterator();
      while (it.hasNext()){
        SelectionKey key=null;
        try{
            key = (SelectionKey) it.next();
            it.remove();
            if (key.isReadable()) {
                receive(key);
            }
            if (key.isWritable()) {
                send(key);
            }
        }catch(IOException e){
           e.printStackTrace();
           try{
               if(key!=null){
                   key.cancel();
                   key.channel().close();
               }
           }catch(Exception ex){e.printStackTrace();}
        }
      }//#while
    }//#while
  }

  public void send(SelectionKey key)throws IOException{
    ByteBuffer buffer=(ByteBuffer)key.attachment();
    SocketChannel socketChannel=(SocketChannel)key.channel();
    buffer.flip();  //把极限设为位置
    String data=decode(buffer);
    if(data.indexOf("\n")==-1)return;
    String outputData=data.substring(0,data.indexOf("\n")+1);
    System.out.print(outputData);
    ByteBuffer outputBuffer=encode("echo:"+outputData);
    while(outputBuffer.hasRemaining())
      socketChannel.write(outputBuffer);

    ByteBuffer temp=encode(outputData);
    buffer.position(temp.limit());
    buffer.compact();

    if(outputData.equals("bye\r\n")){
      key.cancel();
      socketChannel.close();
      System.out.println("关闭与客户的连接");
    }
  }

  public void receive(SelectionKey key)throws IOException{
    ByteBuffer buffer=(ByteBuffer)key.attachment();

    SocketChannel socketChannel=(SocketChannel)key.channel();
    ByteBuffer readBuff= ByteBuffer.allocate(32);
    socketChannel.read(readBuff);
    readBuff.flip();

    buffer.limit(buffer.capacity());
    buffer.put(readBuff);
  }

  public String decode(ByteBuffer buffer){  //解码
    CharBuffer charBuffer= charset.decode(buffer);
    return charBuffer.toString();
  }
  public ByteBuffer encode(String str){  //编码
    return charset.encode(str);
  }

  public static void main(String args[])throws Exception{
    final EchoServer server = new EchoServer();
    Thread accept=new Thread(){
        public void run(){
            server.accept();
        }
    };
    accept.start();
    server.service();
  }
}


nonblock client:
package nonblock;
import java.net.*;
import java.nio.channels.*;
import java.nio.*;
import java.io.*;
import java.nio.charset.*;
import java.util.*;

public class EchoClient{
  private SocketChannel socketChannel = null;
  private ByteBuffer sendBuffer=ByteBuffer.allocate(1024);
  private ByteBuffer receiveBuffer=ByteBuffer.allocate(1024);
  private Charset charset=Charset.forName("GBK");
  private Selector selector;

  public EchoClient()throws IOException{
    socketChannel = SocketChannel.open();
    InetAddress ia = InetAddress.getLocalHost();
    InetSocketAddress isa = new InetSocketAddress(ia,8000);
    socketChannel.connect(isa);
    socketChannel.configureBlocking(false);
    System.out.println("与服务器的连接建立成功");
    selector=Selector.open();
  }
  public static void main(String args[])throws IOException{
    final EchoClient client=new EchoClient();
    Thread receiver=new Thread(){
      public void run(){
        client.receiveFromUser();
      }
    };

    receiver.start();
    client.talk();
  }

  public void receiveFromUser(){
    try{
      BufferedReader localReader=new BufferedReader(new InputStreamReader(System.in));
      String msg=null;
      while((msg=localReader.readLine())!=null){
        synchronized(sendBuffer){
            sendBuffer.put(encode(msg + "\r\n"));
         }
        if(msg.equals("bye"))
          break;
      }
    }catch(IOException e){
       e.printStackTrace();
    }
  }

  public void talk()throws IOException {
     socketChannel.register(selector,
                          SelectionKey.OP_READ |
                          SelectionKey.OP_WRITE);
     while (selector.select() > 0 ){
       Set readyKeys = selector.selectedKeys();
       Iterator it = readyKeys.iterator();
       while (it.hasNext()){
         SelectionKey key=null;
         try{
             key = (SelectionKey) it.next();
             it.remove();

             if (key.isReadable()) {
                 receive(key);
             }
             if (key.isWritable()) {
                 send(key);
             }
         }catch(IOException e){
            e.printStackTrace();
            try{
                if(key!=null){
                    key.cancel();
                    key.channel().close();
                }
            }catch(Exception ex){e.printStackTrace();}
         }
      }//#while
    }//#while
  }

  public void send(SelectionKey key)throws IOException{
    SocketChannel socketChannel=(SocketChannel)key.channel();
    synchronized(sendBuffer){
        sendBuffer.flip(); //把极限设为位置
        socketChannel.write(sendBuffer);
        sendBuffer.compact();
     }
  }
  public void receive(SelectionKey key)throws IOException{
    SocketChannel socketChannel=(SocketChannel)key.channel();
    socketChannel.read(receiveBuffer);
    receiveBuffer.flip();
    String receiveData=decode(receiveBuffer);

    if(receiveData.indexOf("\n")==-1)return;

    String outputData=receiveData.substring(0,receiveData.indexOf("\n")+1);
    System.out.print(outputData);
    if(outputData.equals("echo:bye\r\n")){
        key.cancel();
        socketChannel.close();
        System.out.println("关闭与服务器的连接");
        selector.close();
        System.exit(0);
    }

    ByteBuffer temp=encode(outputData);
    receiveBuffer.position(temp.limit());
    receiveBuffer.compact();
  }

  public String decode(ByteBuffer buffer){  //解码
    CharBuffer charBuffer= charset.decode(buffer);
    return charBuffer.toString();
  }
  public ByteBuffer encode(String str){  //编码
    return charset.encode(str);
  }
}

分享到:
评论

相关推荐

    c# socket同步、异步信息交互DEMO

    - **异步通信DEMO**:演示如何使用异步操作,如`BeginSend()`和`BeginReceive()`,以及相关的回调函数或事件处理,以实现非阻塞的通信。 - **混合通信DEMO**:可能结合同步和异步通信,比如服务端使用同步接收连接...

    利用Python中SocketServer 实现客户端与服务器间非阻塞通信

    利用SocketServer模块来实现网络客户端与服务器并发连接非阻塞通信。 首先,先了解下SocketServer模块中可供使用的类: BaseServer:包含服务器的核心功能与混合(mix-in)类挂钩;这个类只用于派生,所以不会生成这...

    Socket编程

    此外,还介绍了非阻塞和阻塞的消息获取方式,以及混合消息获取方式。 UDP协议方面,章节介绍了如何构建简单的UDP服务器和客户端,以及如何构建一个UDP阶乘服务器。UDP的应用场景包括那些对实时性要求较高,但可以...

    套接字Socket编程用主要API

    - **flags**: 控制标志,如`MSG_DONTWAIT`表示非阻塞模式。 ##### 6. **接收数据(Receive/Read)** - **APIs**: `recv(sockfd, buf, len, flags)` 和 `read(sockfd, buf, len)` - **描述**: - **sockfd**: 套...

    使用Swoole PHP 框架开发flash游戏的socket服务端.zip

    首先,**Swoole**是一个C语言编写、PHP扩展实现的异步并行网络通信引擎,它为PHP提供了原生的多线程和多进程支持,以及非阻塞I/O模型,极大地提升了PHP在处理高并发场景下的性能。在游戏服务端开发中,Swoole能够...

    Network的bsd socket的ipv6可以连接服务器3344,可以接受数据

    10. **网络编程模型**: 常见的网络编程模型有同步阻塞模型、同步非阻塞模型、异步I/O模型和事件驱动模型。根据应用需求,开发者会选择合适的模型来实现客户端和服务器间的通信。 以上就是标题和描述中涉及的网络...

    golang将多路复异步io转成阻塞io的方法详解

    然而,在某些场景下,我们可能希望将异步或非阻塞IO转换为传统的阻塞IO模式,以简化程序设计或适应特定的应用需求。本文将深入探讨如何在Golang中实现这一转换,并通过示例代码进行说明。 #### IO模型简介 为了更好...

    c++与android的通信

    当数据到达或发送完成时,会触发相应的回调函数,这样可以保持程序的响应性和非阻塞特性。 总结来说,"C++与Android的通信"涉及了C++ ActiveX控件的开发,通过Socket在浏览器和Android设备之间建立通信通道。这一...

    Jesse.Storimer.Working.with.TCP.Sockets.Oct.2012

    本书名为《Working with ...通过这本书,读者可以获得理解和实现网络服务所需的知识和技能,以及如何在Ruby环境下处理TCP连接、数据交换、非阻塞IO、套接字选项、安全性、高性能连接复用以及网络架构设计等重要方面。

    node.js + socket.io 实现点对点随机匹配聊天

    通过Node.js的事件驱动和非阻塞I/O模型,以及Socket.IO的实时双向通信功能,可以开发出高性能且响应迅速的聊天应用。这种应用在构建现代的社交网络、在线游戏和其他需要实时通信的场景中非常有用。

    tcp.udp高级编程

    对于TCP通信,数据的读写通常是阻塞的,可以通过设置非阻塞模式或者使用多线程、多进程或异步IO模型来提高并发处理能力。此外,TCP的套接字选项如SO_RCVBUF和SO_SNDBUF可以调整缓冲区大小,影响性能。 在UDP通信中...

    Linux下使用UDP协议的点对点聊天程序(含源代码)

    你还可以看到如何使用`select()`或`poll()`系统调用来实现非阻塞I/O,以便在多个套接字上进行并发操作。 总的来说,通过这个项目,你可以学习到如何在Linux环境中利用UDP协议实现基本的网络通信,这对于理解和掌握...

    Java网络程序设计 (PDF)

    Selector类用于监控多个通道的状态变化,从而实现非阻塞I/O。 四、HTTP与HTTPS协议 HTTP(Hypertext Transfer Protocol)是互联网上应用最广泛的一种网络协议,用于从Web服务器传输超文本到本地浏览器。Java的...

    BDDI3_POCS_socket

    由于其非阻塞I/O模型和事件驱动的特性,Node.js特别适合构建高性能的网络应用,如实时聊天服务。 2. **Socket.io**: Socket.io是一个实时应用框架,可以在Web浏览器和服务器之间提供实时、双向、基于事件的通信。它...

    socketreact:教我自己用 socket.ionode 服务器做出React

    Node.js使用了一个事件驱动、非阻塞I/O模型,使其轻量又高效,非常适合用于数据密集型的实时应用。 Socket.IO是Node.js的一个库,它提供了一种简单的方式来实现实时、双向的通信,即WebSocket协议。WebSocket是一种...

    RL-TCPnet -TCP服务器

    - **非阻塞I/O**:非阻塞I/O模式允许服务器在等待数据时执行其他任务,提高效率。 通过以上这些知识点,我们可以看到RL-TCPnet - TCP服务器在设计上考虑了多方面的需求,包括并发处理、连接管理、安全性和性能优化...

    JavaGPP:Java游戏编程模式

    8. **网络编程**: 多人在线游戏需要网络编程技能,Java的Socket编程或NIO(非阻塞I/O)库可以帮助实现这一目标。 9. **数据持久化**: 游戏进度、用户数据等需要保存,Java的文件I/O、序列化或数据库连接库可以实现...

    WPF开发教程和网络编程文档合集

    3. **网络编程模型**:网络编程通常涉及服务器端的并发模型,如同步阻塞I/O、非阻塞I/O、多线程、IO复用(如Select、Poll、EPoll)等。EPoll模型在处理大量并发连接时表现优秀,减少了上下文切换的开销。 综上所述...

    35C10K和C1000K回顾1

    例如,调整TCP缓冲区大小、使用SO_REUSEADDR和SO_REUSEPORT选项来加速socket的绑定和监听,以及使用sendfile和零拷贝技术提高数据传输效率。 总之,C10K和C1000K问题的解决涉及到了系统设计、I/O模型、资源管理等多...

    boost asio 最新版

    协程是另一个值得关注的功能,它允许开发者编写非阻塞的代码,从而进一步提高程序的并发性能。 #### 七、Boost.Asio-进阶 ##### 7.1 AsioVSBoost.Asio 虽然Boost.Asio基于Asio实现,但它增加了许多额外的功能和...

Global site tag (gtag.js) - Google Analytics