码迷,mamicode.com
首页 > 其他好文 > 详细

网络编程之qq

时间:2016-08-26 18:22:39      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

public class ChatClient {

    public void chat() {  

        try {  

            // 连接到服务器    

         /**连接到服务器0.5*/

            Socket socket = new Socket("localhost", 9000);  

            try {  

                /** 读取服务器端传过来信息的输入流对象 --0.5*/

               InputStream is=socket.getInputStream();

               Reader r=new InputStreamReader(is);

               BufferedReader br=new BufferedReader(r);

                /** 向服务器端发送信息的输出流对象--0.5分  */

               BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));  

                // 装饰标准输入流,用于从控制台输入  

                Scanner scanner = new Scanner(System.in);  

                while (true) {  

                    String send = scanner.nextLine();  

                    System.out.println("客户端:" + send);  

                    /** 把从控制台得到的信息传送给服务器  0.5*/

                    bw.write(send);

                    /** 读取来自服务器的信息  0.5*/

                    String accpet =br.readLine();  

                    System.out.println(accpet);  

                }  

            } finally {  

                socket.close();  /**关闭0.5*/

            }  

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

    }  

    public static void main(String[] args) {  

        new ChatClient().chat(); /**测试0.5*/

}  

}

 

服务器端

public class ChatServer {

    // 提供服务  

    public void service() {  

        try {/**建立服务器连接  0.5*/

            ServerSocket server = new ServerSocket(9000);  

            /**等待客户连接  0.5*/

            Socket socket = server.accept();  

            try {  

               /** 读取客户端传过来信息的输入流对象 --0.5*/

                InputStream is=socket.getInputStream();

                Reader r=new InputStreamReader(is);

                BufferedReader br=new BufferedReader(r);

                 /** 向客户端发送信息的输出流对象--0.5分  */

                BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));  

                // 获取控制台输入的Scanner  

                Scanner scanner = new Scanner(System.in);  

                while (true) {  

                   /** 读取来自服务器的信息  0.5*/

                    String accpet =br.readLine();  

                    System.out.println(accpet);  

                   String send = scanner.nextLine();  

                      System.out.println("客户端:" + send);  

                      /** 把从控制台得到的信息传送给服务器  0.5*/

                      bw.write(send);

                     

                }  

            } finally {// 建立连接失败的话不会执行socket.close();  

                socket.close();  

            }  

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

    }    

    public static void main(String[] args) {  

        new ChatServer().service();  /**0.5*/

    }  

}

网络编程之qq

标签:

原文地址:http://www.cnblogs.com/lgf428/p/5811099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!