實現一個非阻塞IO的服務器

先來實現一個簡單的服務器,這個服務器簡單的回送任何客戶端的輸入

EchoServer.java

package server;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * This program implements a simple server that listens to port 8189 and echoes back all client input
 * @author zhangchen
 *
 */
public class EchoServer {

    public static void main(String[] args) throws IOException
    {
        //establish server socket
        try(ServerSocket s=new ServerSocket(8189))
        {
            //wait for client connection
            try(Socket incoming=s.accept())
            {
                InputStream inStream=incoming.getInputStream();
                OutputStream outStream=incoming.getOutputStream();

                try(Scanner in=new Scanner(inStream))
                {
                    PrintWriter out=new PrintWriter(outStream,true);
                    out.println("hello,enter bye to exit");

                    //echo client input
                    boolean done=false;
                    while(!done&&in.hasNextLine())
                    {
                        String line=in.nextLine();
                        out.println("Echo: "+line);
                        if(line.trim().equalsIgnoreCase("BYE")) done=true;
                    }

                }
            }
        }
    }

}

可以在命令行中使用telnet localhost 8189來連接這個服務器
這裏寫圖片描述

這個服務器有一個問題,那就是不能同時爲多個客戶端服務。自然而然的,我們想到了使用多線程,下面實現一個多線程的服務器,功能同上:

ThreadedEchoServer.java

package threaded;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * This program implementsa multithreaded server that listens to port 8189 and echoes back
 * all client input.
 * @author zhangchen
 *
 */
public class ThreadedEchoServer {
    public static void main(String[] args)
    {
        try
        {
            int i=1;
            ServerSocket s=new ServerSocket(8189);
            while(true)
            {
                Socket incoming=s.accept();
                System.out.println("Spawning"+i);
                Runnable r=new ThreadEchoHandler(incoming);
                Thread t=new Thread(r);
                t.start();
                i++;
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();

        }
    }

}

/**
 * This class handles the client input for one server socket connection
 * @author zhangchen
 *
 */
class ThreadEchoHandler implements Runnable
{

    private Socket incoming;

    /**
     * Constructs a handler
     * @param i the incoming socket
     */
    public ThreadEchoHandler(Socket i) {
        // TODO Auto-generated constructor stub
        incoming=i;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        try
        {
            try
            {
                InputStream inStream=incoming.getInputStream();
                OutputStream outStream=incoming.getOutputStream();

                Scanner in=new Scanner(inStream);
                PrintWriter out=new PrintWriter(outStream,true);

                out.println("hello gougou");

                //echo client input
                boolean done=false;
                while(!done&&in.hasNextLine())
                {
                    String line=in.nextLine();
                    out.println("Echo: "+line);
                    if(line.trim().equalsIgnoreCase("BYE")) done=true;
                }
            }
            finally
            {
                incoming.close();
            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
    }

每當一個新的客戶端連接到服務器,服務器都建立一個新的線程來爲這個客戶端服務。
這裏寫圖片描述

在這個程序中,我們爲每個連接生成一個獨立的線程。這種方法不能滿足高性能服務器的要求,爲了實現更高的吞吐量,我們利用java.nio包中的特性來實現一個非阻塞IO的服務器,這個服務器的功能是將客戶端輸入的文件路徑對應文件的內容發送給客戶端:

NonBlockingServer.java


import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.util.*;
import java.nio.charset.*;
import java.lang.*;


public class NonBlockingServer
{
    public Selector sel = null; 
    public ServerSocketChannel server = null;
    public SocketChannel socket = null;
    public int port = 4900;
    String result = null;


    public NonBlockingServer()
    {
        System.out.println("Inside default ctor");
    }

    public NonBlockingServer(int port)
    {
        System.out.println("Inside the other ctor");
        port = port;
    }

    //初始化
    public void initializeOperations() throws IOException,UnknownHostException
    {
        System.out.println("Inside initialization");
        sel = Selector.open();
        server = ServerSocketChannel.open();
        server.configureBlocking(false); //將通道的阻塞模式設置爲非阻塞模式
        InetAddress ia = InetAddress.getLocalHost();
        InetSocketAddress isa = new InetSocketAddress(ia,port);
        server.socket().bind(isa);
    }

    public void startServer() throws IOException
    {
        System.out.println("Inside startserver");
        initializeOperations();
        System.out.println("Abt to block on select()");

        //Registers this channel with the given selector, returning a selection key.
        //A channel should be registered according to the events it will handle.
        //for instance, a channel that accepts incoming connection should be registered as follow
        SelectionKey acceptKey = server.register(sel, SelectionKey.OP_ACCEPT ); 


        while (acceptKey.selector().select() > 0 )
        {   

            Set readyKeys = sel.selectedKeys();
            Iterator it = readyKeys.iterator();

            while (it.hasNext()) {
                SelectionKey key = (SelectionKey)it.next();
                it.remove();//After a key is processed, it is removed from the list of ready keys

                if (key.isAcceptable()) {
                    System.out.println("Key is Acceptable");
                    ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
                    socket = (SocketChannel) ssc.accept();
                    socket.configureBlocking(false);

                    //this "another" key binds with a channel responsible for read and write -by zc
                    SelectionKey another = socket.register(sel,SelectionKey.OP_READ|SelectionKey.OP_WRITE);
                }
                if (key.isReadable()) {
                    System.out.println("Key is readable");
                    String ret = readMessage(key);//從key對應的SocketChannel中讀出字符串
                    if (ret.length() > 0) {
                        writeMessage(socket,ret);//將ret指向的文件中的內容寫入socket指向的SocketChannel
                    }
                }
                if (key.isWritable()) {
                    System.out.println("THe key is writable");
                    String ret = readMessage(key);//從key對應的SocketChannel中讀出字符串
                    socket = (SocketChannel)key.channel();
                    if (result.length() > 0 ) {
                        writeMessage(socket,ret);//將ret指向的文件中的內容寫入socket指向的SocketChannel
                    }
                }
            }
        }
    }

    //將ret指向的文件中的內容寫入socket指向的SocketChannel
    public void writeMessage(SocketChannel socket,String ret)
    {
        System.out.println("Inside the loop");

        if (ret.equals("quit") || ret.equals("shutdown")) {
            return;
        }
        File file = new File(ret);
        try
        {

            RandomAccessFile rdm = new RandomAccessFile(file,"r");
            FileChannel fc = rdm.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            fc.read(buffer);
            buffer.flip();

            Charset set = Charset.forName("us-ascii");
            CharsetDecoder dec = set.newDecoder();
            CharBuffer charBuf = dec.decode(buffer);
            System.out.println(charBuf.toString());
            buffer = ByteBuffer.wrap((charBuf.toString()).getBytes());
            int nBytes = socket.write(buffer);
            System.out.println("nBytes = "+nBytes);
                result = null;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }

    //從key對應的SocketChannel中讀出字符串
    public String readMessage(SelectionKey key)
    {
        int nBytes = 0;
        socket = (SocketChannel)key.channel();
        ByteBuffer buf = ByteBuffer.allocate(1024);
        try
        {
            nBytes = socket.read(buf);
            buf.flip();
            Charset charset = Charset.forName("us-ascii");
            CharsetDecoder decoder = charset.newDecoder();
            CharBuffer charBuffer = decoder.decode(buf);
            result = charBuffer.toString();

        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String args[])
    {
        NonBlockingServer nb = new NonBlockingServer();
        try
        {
            nb.startServer();
        }
        catch (IOException e)
        {
            e.printStackTrace();
            System.exit(-1);
        }

    }
}



Client.java


import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.io.*;
import java.nio.channels.spi.*;
import java.nio.charset.*;
import java.lang.*;


public class Client
{
    public SocketChannel client = null;
    public InetSocketAddress isa = null;
    public RecvThread rt = null;

    public Client() 
    {
    }

    public void makeConnection()
    {
        int result = 0;
        try
        {

            client = SocketChannel.open();
                isa = new InetSocketAddress("zhangchen-PC",4900);
            client.connect(isa);
            client.configureBlocking(false);
            receiveMessage();    
        }
        catch(UnknownHostException e)
        {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        while ((result = sendMessage()) != -1)
        {
        }

        try
        {
            client.close();
            System.exit(0);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

    public int sendMessage()
    {
        System.out.println("Inside SendMessage");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String msg = null;
        ByteBuffer bytebuf = ByteBuffer.allocate(1024);
        int nBytes = 0;
        try
        {
            msg = in.readLine();
            System.out.println("msg is "+msg);
            bytebuf = ByteBuffer.wrap(msg.getBytes());
            nBytes = client.write(bytebuf);
            System.out.println("nBytes is "+nBytes);
            if (msg.equals("quit") || msg.equals("shutdown")) {
                System.out.println("time to stop the client");
                interruptThread();
                try
                {
                    Thread.sleep(5000);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                client.close();
                return -1;
            }

        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        System.out.println("Wrote "+nBytes +" bytes to the server");
        return nBytes;
    }

    public void receiveMessage()
    {
        rt = new RecvThread("Receive THread",client);
        rt.start();

    }

    public void interruptThread()
    {
        rt.val = false;
    }

    public static void main(String args[])
    {
        Client cl = new Client();
        cl.makeConnection();
    }

    public class RecvThread extends Thread
    {
        public SocketChannel sc = null;
        public boolean val = true;

        public RecvThread(String str,SocketChannel client)
        {
            super(str);
            sc = client;
        }

        public void run() {

            System.out.println("Inside receivemsg");
            int nBytes = 0;
            ByteBuffer buf = ByteBuffer.allocate(2048);
            try
            {
                while (val)
                {
                    while ( (nBytes = nBytes = client.read(buf)) > 0){
                        buf.flip();
                        //Flips this buffer. 
                        //The limit is set to the current position and then the position is set to zero. 
                        //If the mark is defined then it is discarded. 


                        Charset charset = Charset.forName("us-ascii");
                        CharsetDecoder decoder = charset.newDecoder();
                        CharBuffer charBuffer = decoder.decode(buf);
                        String result = charBuffer.toString();
                                    System.out.println(result);
                        buf.flip();

                    }
                }

            }
            catch(IOException e)
            {
                e.printStackTrace();

            }


        }
    }
}

運行結果如圖:
這裏寫圖片描述

發佈了44 篇原創文章 · 獲贊 48 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章