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();



        }


    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值