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

通过TCP进行通信

时间:2019-05-07 13:07:30      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:get   shu   close   div   rac   known   edr   info   cep   

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket =  new ServerSocket(8888);
            System.out.println("服务器即将启动,等待客户端的连接");
            Socket socket = serverSocket.accept();
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String info = null;
            while ((info=br.readLine())!=null) {
                System.out.println("我是服务器,客户端说" + info);
            }
            br.close();
            isr.close();
            is.close();
            socket.close();
            serverSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
public class Client {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost",8888);
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os);
            pw.write("用户名:admin;密码:123");
            pw.flush();
            socket.shutdownOutput();
            pw.close();
            os.close();
            socket.close();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

通过TCP进行通信

标签:get   shu   close   div   rac   known   edr   info   cep   

原文地址:https://www.cnblogs.com/dulute/p/10824723.html

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