Day27 基于TCP协议的传输

实现基于UDP协议的的,两个进程(两个java程序)互相发送,并接受各自发送的数据的功能。

//客户端,根据配置文件的地址上传文件

public class Clinet {
    public static void main(String[] args) throws IOException {
        //调用方法获取picture的地址
        String picturePath = getPicturePath();
        System.out.println("picturePath:" + picturePath);
        //创建读取文件的流对象并进行包装
        FileInputStream fis = new FileInputStream(picturePath);
        BufferedInputStream bis = new BufferedInputStream(fis);
        //创建stocket对象
        Socket socket = new Socket("127.0.0.1", 10086);
        //把客户端输出字节流包装成一个缓冲字符输出流
        OutputStream out = socket.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(out);
        //开始读取,进行数据传输
        byte[] buffer = new byte[1024];
        int len;
        while ((len = bis.read(buffer)) != -1) {
            //获取相应的流,进行数据传输
            bos.write(buffer, 0, len);
        }
        //刷新缓冲流中的缓冲
        bos.flush();
        //单方面关闭socket中的输入或者输出流
        socket.shutdownOutput();
        //接收服务器端的反馈
        byte[] byteBuffer = new byte[1024];
        InputStream in = socket.getInputStream();
        /*因为socket的inputstream中的read方法是一个阻塞方法,
        当如果没有接收到数据就会阻塞*/
        len = in.read(byteBuffer);
        System.out.println(new String(byteBuffer, 0, len));
        //关闭流释放资源
        socket.close();
        bis.close();
    }

    public static String getPicturePath() throws IOException {
        //创建读取配置文件的流对象
        FileInputStream fis = new FileInputStream("d:\\config.properties");
        //读取配置文件
        //创建一个Properties对象
        Properties properties = new Properties();
        //利用Properties对象的load方法直接读取配置文件内容,并将其存储到Properties对象中
        properties.load(fis);
        //直接从Properties对象获取目标配置信息(即一条目标配置的属性值)
        String picturePath = properties.getProperty("picturePath");
        return picturePath;
    }
}

//服务器端,根据端口号接收数据,并输出.

public class Server {
    public static void main(String[] args) throws IOException {
        boolean flag = true;
        //创建服务器端的socket对象即ServerSocket
        ServerSocket serverSocket = new ServerSocket(10086);
        //调用ServerSocket
        Socket socket = serverSocket.accept();
        //创建文件输出流
        FileOutputStream fos = new FileOutputStream("d:\\copy.jpg");
        //根据需要从Socket对象中获取流,并进行数据的读取/发送
        InputStream in = socket.getInputStream();
        //将服务器端输入字节流,包装成缓冲字符流
        BufferedInputStream br = new BufferedInputStream(in);
        //
        byte[] buffer = new byte[1024];
        int len;
        //Socket中的输入流的read方法是阻塞方法,
        // 所以服务器读取发送端数据这个循环不会自动终止
        while ((len = br.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        //服务器端给客户端发送反馈消息
        OutputStream out = socket.getOutputStream();
        //发送端的Ip地址
        InetAddress sendIp = socket.getInetAddress();
        //发送端的端口号
        int port = socket.getPort();
        String response = "来自" + sendIp + "--" + port + "的文件,上传完毕";
        out.write(response.getBytes());
        //关闭socket,释放资源
        socket.close();
        serverSocket.close();
        fos.close();
    }
}

思路:利用Socket建立连接,进行数据的输入和输出.细节在注释中已经说明.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值