JAVA中Socket的用法模拟服务端和客户端

《看透springMvc源代码分析与实践》学习笔记

Socket分为ServerSocket和Socket两个大类

ServerSocket用于服务端,可以通过accept方法监听请求,监听到请求后返回Socket,Socket用于具体完成数据传输,客户端直接使用Socket发起请求并传输数据。

从JDK1.4开始,java增加了新的io模式,nio在底层采用了新的处理方式,极大的提高了IO效率,我们使用的Socket也属于IO的一种,nia提供了相应的工具,ServerSocketChannel和SocketChannel,分别对应原来的ServerSocket和Socket。

Buffer、Channel和Selector

现在的快递模式不会一件一件的送,而是将很多件货一起拿去送,而且在中转站都有专门的分拣员负责按配送范围把货物分给不同的送货员,这样效率就提高了很多。这种模式就相当于NioSocket的处理模式,Buffer就是所要送的货物,Channel就是送货员,Selector就是中转站的分拣员。

NioSocket使用中首先要创建ServerSocketChannel,然后注册Selector,接下来就可以用Selector接受请求并处理了。

NIOServer服务端代码
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;


public class TestSocket {

public static void main(String[] args) throws IOException {
    //创建ServerSocketChannel,监听8080端口
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(new InetSocketAddress(8080));
    //设置为非阻塞模式
    ssc.configureBlocking(false);
    //为ssc注册选择器
    Selector selector=Selector.open();
    ssc.register(selector, SelectionKey.OP_ACCEPT);
    //创建处理器
    Handler handler = new Handler(1024);
    while(true){
        //等待请求,每次等待阻塞3s,超过3s后线程继续向下运行,如果传入0或者不传参数,将一直阻塞
        if(selector.select(3000)==0){
            System.out.println("等待请求超时......");
            continue;
        }
        System.out.println("处理请求......");
        //获取待处理的SelectionKey
        Iterator keyIter=selector.selectedKeys().iterator();
        
        while(keyIter.hasNext()){
            SelectionKey key = keyIter.next();
            try {
                //接收到连接请求时
                if(key.isAcceptable()){
                    handler.handleAccept(key);
                }
                //读数据
                if(key.isReadable()){
                    handler.handleRead(key);
                }
            } catch (Exception e) {
                keyIter.remove();
                continue;
            }
            //处理完后,从待处理的SelectionKey迭代器中移除当前所使用的key
            keyIter.remove();
        }
    }
}

private static class Handler{
    private int bufferSize = 1024;
    private String localCharset = "UTF-8";
    public Handler(){}
    public Handler(int bufferSize){
        this(bufferSize,null);
    }
    public Handler(String LocalCharset){
        this(-1,LocalCharset);
    }
    public Handler(int bufferSize,String localCharset){
        if(bufferSize>0)
            this.bufferSize = bufferSize;
        if(localCharset != null)
            this.localCharset= localCharset;
    }
    public void handleAccept(SelectionKey key)throws IOException{
        SocketChannel sc=((ServerSocketChannel)key.channel()).accept();
        sc.configureBlocking(false);
        sc.register(key.selector(), SelectionKey.OP_READ,ByteBuffer.allocate(bufferSize));
    }
    
    public void handleRead(SelectionKey key) throws IOException{
        //获取channel
        SocketChannel sc = (SocketChannel)key.channel();
        //获取buffer并重置
        ByteBuffer buffer = (ByteBuffer)key.attachment();
        buffer.clear();
        //没有独到内容则关闭
        if(sc.read(buffer)==-1){
            sc.close();
        }else{
            //将buffer转换为读状态
            buffer.flip();
            //将buffer中接收到的值按localCharset格式编码后保存到receicedString
            String receivedString = Charset.forName(localCharset).newDecoder()
                    .decode(buffer).toString();
            System.out.println("receiced from client:"+receivedString);
            
            //返回数据给客户端
            String sendString = "received data:"+receivedString;
            buffer = ByteBuffer.wrap(sendString.getBytes(localCharset));
            sc.write(buffer);
            //关闭Socket
            sc.close();
        }
    }
}
}
Client客户端代码
 package cn.webmvct.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {

public static void main(String[] args) {
    String msg = "Client Data";
    try {
        //创建一个Socket,跟本机的8080端口连接
        Socket socket = new Socket("127.0.0.1",8080);
        //使用Socket创建PrintWriter和BufferedReader进行读写数据
        PrintWriter pw = new PrintWriter(socket.getOutputStream());
        BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //发送数据
        pw.println(msg);
        pw.flush();
        //接收数据
        String line = is.readLine();
        System.out.println("received from server:"+line);
        //关闭资源
        pw.close();
        is.close();
        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}
先执行服务端代码,再执行客户端代码,使用jdk1.7版本。服务端打印出:receiced from client:Client Data,客户端打印:received from server:received data:Client Data,则模拟socket传输成功。

转载于:https://www.cnblogs.com/FWebApp/p/7155779.html

你可能感兴趣的:(JAVA中Socket的用法模拟服务端和客户端)