socket
作者:互联网
package com.atguigu;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public class TCPTest3 {
@Test
public void client() {
Socket socket = null;
OutputStream outputStream = null;
BufferedInputStream bufferedInputStream = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
socket = new Socket(InetAddress.getLocalHost(), 8899);
outputStream = socket.getOutputStream();
bufferedInputStream = new BufferedInputStream(new FileInputStream("hello.txt"));
byte[] bytes = new byte[1024];
int len;
while ((len = bufferedInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
// 此处如果不写shutdownOutput,客户端会向下继续执行等待输入,但是服务端的read方法会阻塞,持续等待客户端的输入,不会执行outputStream的write方法,致使客户端的inputStream的read方法阻塞,最终客户端与服务端的程序都无法顺利结束
socket.shutdownOutput();
inputStream = socket.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, len);
}
System.out.println(byteArrayOutputStream.toString("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void server() {
ServerSocket serverSocket = null;
Socket acceptSocket = null;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
OutputStream outputStream = null;
try {
serverSocket = new ServerSocket(8899);
acceptSocket = serverSocket.accept();
bufferedInputStream = new BufferedInputStream(acceptSocket.getInputStream());
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("hello1.txt"));
byte[] bytes = new byte[1024];
int len;
while ((len = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, len);
}
outputStream = acceptSocket.getOutputStream();
outputStream.write("已收到".getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (acceptSocket != null) {
try {
acceptSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedOutputStream != null) {
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
标签:outputStream,socket,try,printStackTrace,IOException,catch,null 来源: https://www.cnblogs.com/kanaliya/p/15898282.html