java TCP简易网络编程

该博客介绍了如何使用Java的Socket实现客户端和服务端之间的图片传输,包括创建流、读写操作、服务端确认和解决阻塞问题。展示了建立连接、数据交换及最终流的关闭过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.IDEA中模块中有一张图片,建立客户端和服务端,客户端建立读写流读取照片的信息,然后通过Socket建立输出流,写出读写流读取的字节。

2.服务端建立ServerSoket指定端口名,让客户端连接,服务端建立一个输出流,服务端调用accept建立输入流,输入流读写客户端发送过来的信息,然后输出流写出输入流读写的信息。

3.确定服务端收到图片,给客户端一个反馈,与以上类似操作。

4.要注意有反馈的情况下客户端输出图片会出现阻塞,需要在客户端读写图片下手动shutdownOutput(),才能结束。

5.最后关闭流操作。

package SocketTest;

import org.junit.jupiter.api.Test;

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

public class Test2 {
    @Test
    public void Client(){
        Socket socket = null;
        OutputStream out = null;
        BufferedInputStream bs = null;
        ByteArrayOutputStream bs1 = null;
        InputStream in = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"),7788);
            out = socket.getOutputStream();
            bs = new BufferedInputStream(new FileInputStream("th.jpg"));
            byte[]arr  = new byte[1024];
            int len;
            while ((len=bs.read(arr))!=-1)
            {
                out.write(arr,0,len);
            }
            socket.shutdownOutput();
            bs1 = new ByteArrayOutputStream();
            in = socket.getInputStream();
            byte[]arr1 = new byte[122];
            int len1 ;
            while ((len1 = in.read(arr1))!=-1)
            {
                bs1.write(arr1,0,len1);
            }
            System.out.println(bs1.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bs1!=null)
            {
                try {
                    bs1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in!=null)
            {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null)
            {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out!=null)
            {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bs!=null)
            {
                try {
                    bs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    @Test
    public void Server(){
        ServerSocket ss = null;
        Socket accept = null;
        InputStream in = null;
        OutputStream out= null;
        BufferedOutputStream bo = null;
        try {
            ss = new ServerSocket(7788);
            accept = ss.accept();
            in = accept.getInputStream();
            bo = new BufferedOutputStream(new FileOutputStream("th1.jpg"));
            byte [] arr = new byte[1024];
            int len;
            while ((len = in.read(arr))!=-1)
            {
                bo.write(arr,0,len);
            }

            out= accept.getOutputStream();
            out.write("已经收到图片".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out!=null)
            {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ss!=null)
            {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ss!=null)
            {
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ss!=null)
            {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ss!=null)
            {
                try {
                    bo.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值