如图:客户端与服务端连接不上,服务端处于阻塞状态:
首先,使用FileInputStream流输入文件,使用FileInputStream.read()方法读取文件返回int型(与题无关,加深记忆,总是忘记强转成int);
其次,关于read()方法的介绍:Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available; 表明read()方式是一个阻塞式方法;
解决方法:使用socket.shutdownOutput();关闭数据的输出;
如图:
客户端:
@Test
public void client(){
InetAddress inet = null;
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;//读入文件
try {
//1.创建Socket
// socket = new Socket(InetAddress.getByName(("127.0.0.1"),9988));
inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 9988);
//2.输出流
os = socket.getOutputStream();
//3.输入流
fis = new FileInputStream(new File("程潇.jpg"));
//4.读写过程
byte[] buffer =