• nio/mina(一) nio基本通信


    服务端:

    Server.java

    package com.nafio.nio1;
    import java.io.IOException;
    import java.net.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.util.*;
    public class Server
    {
    	ServerSocketChannel ssc;
    	public void start()
    	{
    		try
    		{
    			Selector selector = Selector.open();
    			ServerSocketChannel ssc = ServerSocketChannel.open();
    			ssc.configureBlocking(false);
    			ServerSocket ss = ssc.socket();
    			InetSocketAddress address = new InetSocketAddress(9988);
    			ss.bind(address);
    			ssc.register(selector, SelectionKey.OP_ACCEPT);
    			System.out.println("服务器_端口注册完毕!");
    			while (true)
    			{
    				System.out.println("服务端_主循环--------->");
    				selector.select();//nafio info 这里每次是阻塞的
    				//System.out.println("测试select阻塞");
    				Set<SelectionKey> selectionKeys = selector.selectedKeys();
    				Iterator<SelectionKey> iter = selectionKeys.iterator();
    				ByteBuffer echoBuffer = ByteBuffer.allocate(20);
    				SocketChannel sc;
    				while (iter.hasNext())
    				{
    					System.out.println("服务器_测试iteretor包含数量");
    					SelectionKey key = iter.next();
    					if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT)
    					{
    						ServerSocketChannel subssc = (ServerSocketChannel) key
    								.channel();
    						sc = subssc.accept();
    						sc.configureBlocking(false);
    						sc.register(selector, SelectionKey.OP_READ);
    						iter.remove();
    						System.out.println("服务器_有新连接:" + sc);
    					}
    					else if ((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ)
    					{
    						sc = (SocketChannel) key.channel();
    						while (true)
    						{
    							echoBuffer.clear();
    							int a;
    							try
    							{
    								a = sc.read(echoBuffer);
    							}
    							catch (Exception e)
    							{
    								e.printStackTrace();
    								break;
    							}
    							if (a == -1)
    								break;
    							if (a > 0)
    							{
    								byte[] b = echoBuffer.array();
    								System.out.println("服务器_接收数据<--- " + new String(b));
    								echoBuffer.flip();
    								sc.write(echoBuffer);
    								System.out.println("服务器_返回数据---> " + new String(b));
    							}
    						}
    						sc.close();
    						System.out.println("服务器_连接结束");
    						System.out.println("=============================");
    						iter.remove();
    					}
    				}
    			}
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    
    	}
    
    	public static void main(String[] args) throws IOException {
    		Server server = new Server();
    		server.start();
    	}
    }
    


    客户端

    Client.java

    package com.nafio.nio1;
    
    import java.io.IOException;
    import java.net.*;
    import java.nio.*;
    import java.nio.channels.*;
    public class Client
    {
    	public void start()
    	{
    		try
    		{
    			//SocketAddress address = new InetSocketAddress("localhost",55555);
    			SocketAddress address = new InetSocketAddress("localhost",9988);
    			SocketChannel client=SocketChannel.open(address);
    			client.configureBlocking(false);
    			String a="一二三四五六七八九十";
    			ByteBuffer buffer=ByteBuffer.allocate(20);//by nafio 20_byte 10_中文
    			buffer.put(a.getBytes());
    			buffer.clear();
    			int d=client.write(buffer);
    			System.out.println("客户端_发送数据---> "+new String(buffer.array()));
    			
    			while(true)
    			{
    				buffer.flip();
    				int i=client.read(buffer);
    				if(i>0)
    				{
    					byte[] b=buffer.array();
    					System.out.println("客户端_接收数据---> "+new String(b));
    					client.close();
    					System.out.println("客户端_连接关闭!");
    					break;
    				}
    			}
    		}
    
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    
    	}
    	public static void main(String[] args) throws IOException {
    		Client client = new Client();
    		client.start();
    	}
    }
    
    
    


     

  • 相关阅读:
    汇编 if else
    汇编  cdecl 函数调用约定,stdcall 函数调用约定
    汇编 push ,pop指令
    汇编 EBP ,ESP 寄存器
    汇编 sub减法指令 比较指令CMP JZ条件跳转指令
    thrift使用案例
    基于hiredis,redis C客户端封装
    golang 3des/ecb/cbc/pkcs5 加解密
    ortp 发送RTP实例
    go:基于时间轮定时器方案
  • 原文地址:https://www.cnblogs.com/nafio/p/9137763.html
Copyright © 2020-2023  润新知