首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

简略的Echo服务器和客户端

2012-07-19 
简单的Echo服务器和客户端package tcpimport java.io.BufferedReaderimport java.io.BufferedWriterimp

简单的Echo服务器和客户端

package tcp;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;public class EchoServer {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        ServerSocket serverSocekt = null;        Socket socket = null;        BufferedReader inBuffer = null;        PrintWriter out = null;        try        {            serverSocekt = new ServerSocket(12);            System.out.println("The server is start:" + serverSocekt);            socket = serverSocekt.accept();            System.out.println("Accpet the Client:" + socket);                        inBuffer = new BufferedReader(new InputStreamReader(socket.getInputStream()));            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);            while(true)            {                String instr = inBuffer.readLine();                System.out.println("In Server reveived the info:" + instr);                if (instr.equals("bye"))                {                    break;                }                out.println(instr);            }        }        catch (IOException e)        {            e.printStackTrace();        }        finally        {            try            {                if (socket != null)                {                                        socket.close();                }                                if (inBuffer != null)                {                    inBuffer.close();                }                                if (out != null)                {                    out.close();                }            }            catch (IOException e)            {                e.printStackTrace();            }        }    }}


package tcp;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.NetworkInterface;import java.net.Socket;import java.net.UnknownHostException;public class EchoClient {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        String hostname = "localhost";        if (args.length > 0)        {            hostname = args[0];        }                PrintWriter out = null;        BufferedReader networkIn = null;                Socket theSocket;        try {            System.out.println(hostname);            theSocket = new Socket(hostname, 12);            networkIn = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));            BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(theSocket.getOutputStream())), true);            System.out.println("connect to echo server.");            while(true)            {                String theLine = userIn.readLine();                if (theLine.equals("."))                {                    break;                }                out.println(theLine);                out.flush();                System.out.println(networkIn.readLine());            }                    } catch (UnknownHostException e) {            // TODO Auto-generated catch block            System.err.println(e);        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        finally        {            if (networkIn != null)            {                try                {                    networkIn.close();                                    }                catch (IOException e)                {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            if (out != null)            {                out.close();            }                    }            }}

热点排行