简单的Echo服务器和客户端_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 简单的Echo服务器和客户端

简单的Echo服务器和客户端

 2012/2/23 9:34:21  小菌子  程序员俱乐部  我要评论(0)
  • 摘要:packagetcp;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket
  • 标签:客户 服务器 客户端 服务
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();
            }
            
        }
        
    }

}
发表评论
用户名: 匿名