错误在 SocketTCP02Client()是Socket is closed,为什么不能写 socket.shutdownOutput();啊
public class SocketTCP02Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务端8888端口等待连接");
Socket socket = serverSocket.accept();
BufferedInputStream bis = new BufferedInputStream(
socket.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("src\\tupian.jpg"));
int readNum;
byte[] bytes = new byte[10240];
while ((readNum = bis.read(bytes)) != -1){
bos.write(bytes, 0, readNum);
}
bos.close();
bis.close();
socket.close();
serverSocket.close();
}
}
public class SocketTCP02Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
String FilePath = "e:\\tupian.jpg";
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(FilePath));
int readNum;
byte[] bytes = new byte[10240];
BufferedOutputStream bos = new BufferedOutputStream(
socket.getOutputStream());
while ((readNum = bis.read(bytes)) != -1) {
bos.write(bytes, 0, readNum);
}
bos.close();
bis.close();
socket.shutdownOutput();
socket.close();
}
}
https://blog.csdn.net/dabing69221/article/details/17351881
参考这篇文章