首页 > 编程语言 > 详细

java socket网络编程

时间:2020-08-19 12:43:31      阅读:62      评论:0      收藏:0      [点我收藏+]

一、socket

  服务端

public class A01server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        Socket socket = serverSocket.accept();

        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int len = inputStream.read(bytes);
        System.out.println(new String(bytes, 0, len));


        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("收到谢谢".getBytes());

        socket.close();
        serverSocket.close();
    }
}

  客户端

public class A01client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 8888);

        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("你好服务器".getBytes());

        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int len = inputStream.read(bytes);
        System.out.println(new String(bytes, 0, len));

        socket.close();
    }
}

 

java socket网络编程

原文:https://www.cnblogs.com/linding/p/13527896.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!