基于Socket的服务端的多线程模式编程案列

来源:互联网 发布:澳洲社交软件 编辑:程序博客网 时间:2024/06/05 03:02

基于Socket的服务端的多线程模式编程

下面是服务端的实现

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class MultiThreadEchoServer {    private static ExecutorService tp= Executors.newCachedThreadPool();    static class HandleMsg implements Runnable{        Socket clientSocket;        public HandleMsg(Socket clientSocket) {            this.clientSocket = clientSocket;        }        @Override        public void run() {            BufferedReader is=null;            PrintWriter os=null;            try {                is=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));                os=new PrintWriter(clientSocket.getOutputStream(),true);                String inputLine=null;                long b=System.currentTimeMillis();                System.out.println("from client: "+is.readLine());                while ((inputLine=is.readLine())!=null){                    os.println(inputLine);                }                long e=System.currentTimeMillis();                System.out.println("spend:"+(e-b)+"ms");            } catch (IOException e) {                e.printStackTrace();            }finally {                try {                    if(is!=null)                        is.close();                    if(os!=null)                        os.close();                    clientSocket.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    public static void main(String[] args) {        ServerSocket echoServer=null;        Socket clientSocket=null;        try {            echoServer=new ServerSocket(8000);        } catch (IOException e) {            e.printStackTrace();        }        while (true){            try {                clientSocket=echoServer.accept();                System.out.println(clientSocket.getRemoteSocketAddress()+"connect");                tp.execute(new HandleMsg(clientSocket));            } catch (IOException e) {                e.printStackTrace();            }        }    }}

客户端的实现

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.InetSocketAddress;import java.net.Socket;public class Client {    public static void main(String[] args) throws IOException {        Socket client=null;        PrintWriter writer=null;        BufferedReader reader=null;        try{            client=new Socket();            client.connect(new InetSocketAddress("localhost",8000));            writer=new PrintWriter(client.getOutputStream(),true);            writer.println("hello!");            writer.flush();            reader=new BufferedReader(new InputStreamReader(client.getInputStream()));            System.out.println("from server:"+reader.readLine());        } catch (IOException e) {            e.printStackTrace();        } finally {            if(writer!=null)            writer.close();            if(reader!=null)            reader.close();            if(client!=null)                client.close();        }    }}

简单描述实现的功能服务端接收客户端的发送过来的数据,并打印客户端发送过来的数据

阅读全文
0 0
原创粉丝点击