zoukankan      html  css  js  c++  java
  • 网络编程两人聊天

    ServerDemo

    import java.io.* ;
    import java.net.* ;
    public class serverDemo {
        public static void main(String [] args){
            ServerSocket serverSocket = null ;
            Socket socket = null ;
            String line ;
            try{
                serverSocket = new ServerSocket (5555) ;
                socket = serverSocket.accept();
                System.out.println("Success") ;
                System.out.println();
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))  ;
                BufferedReader sin = new BufferedReader (new InputStreamReader(System.in));
                PrintWriter os = new PrintWriter(socket.getOutputStream());
                System.out.println("From Client: " + in.readLine()) ;
                line = sin.readLine();
                while(!line.equals("bye")) {
                    os.println(line) ;
                    os.flush() ;
                    System.out.println("From Client:" + in.readLine()) ;
                    line = sin.readLine();
                }
                in.close() ;
                sin.close() ;
                os.close() ;
                socket.close();
                serverSocket.close();
            } catch (Exception e) {
                e.printStackTrace() ;
            }
        }
    }

    ClientDemo。

    import java.net.* ;
    import java.io.*;
    public class ClientDemo {
        public static void main(String [] args){
            
            try{
                Socket socket = new Socket("192.168.1.116",5555) ;
                BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)) ;
                BufferedReader in = new BufferedReader (new InputStreamReader(socket.getInputStream())) ;
                PrintWriter os = new PrintWriter(socket.getOutputStream()) ;
                String line ;
                line = sin.readLine() ;
                while(!line.equals("bye")){
                    os.println(line) ;
                    os.flush() ;
                    System.out.println("Client:" + line); 
                    System.out.println("From Server:" + in.readLine()) ;
                    line = sin.readLine() ;
                    
                }
                os.close();
                sin.close() ;
                in.close();
                socket.close() ;
            }catch (Exception e) {
                e.printStackTrace() ;
            }
        }
    }
  • 相关阅读:
    php如何导出csv文件(代码示例)
    【转】Linux 进程终止后自动重启
    【转】小程序web-view覆盖原生组件
    搭建 LNMP 环境
    数据库索引的底层原理
    NoSQL 介绍
    MySQL Explain详解
    centos搭建 SVN 服务器
    【转】提高mysql千万级大数据SQL查询优化30条经验(Mysql索引优化注意)
    优化mysql slave的同步速度
  • 原文地址:https://www.cnblogs.com/da-peng/p/5141292.html
Copyright © 2011-2022 走看看