005socket的基本使用

package sock;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class MySock5 {
    public static void main(String[] args) throws Exception {
        FileInputStream inputStream = new FileInputStream("d:/美女.jpg");
        byte[] arr = new byte[1234];
        int res = 0;
        Socket socket = new Socket("127.0.0.1", 12331);
        OutputStream out = socket.getOutputStream();

        while ((res = inputStream.read(arr)) != -1) {

            out.write(arr);
        }
        socket.shutdownOutput();
        InputStream in2 = socket.getInputStream();
        byte[] bytes = new byte[1234];
        int read = in2.read(bytes);
        System.out.println(new String(bytes,0,read,"utf-8"));
        inputStream.close();
        socket.close();


    }
}

class MySer5{
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(12331);
//        byte[] bytes1 = "我是中文,你翻译看看".getBytes("utf-8");
//        System.out.println(new String(bytes1));

        while (true) {
            Socket accept = serverSocket.accept();
            InputStream inputStream = accept.getInputStream();
            FileOutputStream fileOutputStream = new FileOutputStream("d:/cop有美女.jpg");
            byte[] bytes = new byte[1234];
            int res = 0;
            while ((res = inputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes);
            }
            OutputStream outputStream = accept.getOutputStream();
            outputStream.write("我也得到美女了".getBytes());


            fileOutputStream.close();
            serverSocket.close();



        }


    }
}

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值