Socket 中 Inputstream 客户端阻塞问题
场景
今天看小伙伴的问题,原本是TCP
请求后数据只返回了一半,第二次请求返回另一半,本地写了TCP
服务端与客户端后发现模拟场景,通过后给了小伙伴代码,但是发现客户端会阻塞,几经查找终于解决。
返回一半数据,第二次请求返回另一半
小伙伴代码
Socket client = new Socket(host, port);
new Thread(() -> {
OutputStream os = null;
InputStream is = null;
try {
while (!stopFlag) {
os = client.getOutputStream();
is = client.getInputStream();
if (request == null) {
continue;
}
os.write(request);
int capacity = 1024*32; //TODO 优化
byte[] buf = new byte[capacity];
int len = is.read(buf);
byte[] bytes = new byte[len];
System.arraycopy(buf, 0, bytes, 0, len);
response = bytes;
request = null;
}
} catch (IOException e) {
log.error("与服务端数据交互异常", e);
} finally {
// 关闭流等
}
}).start();
修改后
Socket client = new Socket(host, port);
new Thread(() -> {
OutputStream os = null;
InputStream is = null;
try {
while (!stopFlag) {
os = client.getOutputStream();
is = client.getInputStream();
if (request == null) {
continue;
}
os.write(request);
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = is.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
response = bytes;
request = null;
}
} catch (IOException e) {
log.error("与服务端数据交互异常", e);
} finally {
// 关闭流等
}
}).start();
改完后还没有解决问题,发现代码第二次走到is.read(buffer)
会阻塞。
Socket 的 InputStream 阻塞问题
首先自己写了测试
- 服务端代码
public class TCPService {
public static final String SERVICE_IP = "127.0.0.1";
public static final int SERVICE_PORT = 10101;
// public static final char END_CHAR = '#';
public void start(){
startService();
}
private void startService(){
try {
InetAddress address = InetAddress.getByName(SERVICE_IP);
Socket connect = null;
ExecutorService pool = Executors.newFixedThreadPool(5);
try (ServerSocket service = new ServerSocket(SERVICE_PORT,5,address)){
while(true){
System.out.println("TCP 服务端启动成功,正在等待新的连接……");
connect = service.accept();
System.out.println("TCP 服务端接到新的请求,已安排处理……");
//创建一个任务
ServiceTask serviceTask = new ServiceTask(connect);
//放入线程池等待运行
pool.execute(serviceTask);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(connect!=null)
connect.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
class ServiceTask implements Runnable{
private Socket socket;
ServiceTask(Socket socket){
this.socket = socket;
}
@Override
public void run() {
try {
InputStream in = socket.getInputStream();
byte[] buffer = new byte[1027];
int len = in.read(buffer);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while(len != -1) {
bos.write(buffer, 0, len);
len = in.read(buffer);
}
bos.close();
System.out.println("请求信息为:" + bos );
System.out.println("\r\n");
String response = "终于等到你了,有缘人…………,你的输入是:" + bos ;
OutputStream out = socket.getOutputStream();
out.write(response.getBytes());
}catch (Exception e){
e.printStackTrace();
}finally {
if(socket!=null)
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- 客户端代码
public class TCPClient {
public String sendAndReceive(String ip, int port, String msg){
//开启一个链接,需要指定地址和端口
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (Socket client = new Socket(ip, port)){
//向输出流中写入数据,传向服务端
OutputStream out = client.getOutputStream();
out.write(msg.getBytes());
// 关闭 输出,防止服务端继续等待
client.shutdownOutput();
//从输入流中解析数据,输入流来自服务端的响应
InputStream in = client.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = in.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
}catch (Exception e){
e.printStackTrace();
}
return bos.toString();
}
}
- 服务端测试代码
public class TestTcpService {
public static void main(String[] args) {
TCPService service1 = new TCPService();
service1.start();
}
}
- 客户端测试代码
public class TestTcpClient {
public static void main(String[] args) {
TCPClient client = new TCPClient();
SimpleDateFormat format = new SimpleDateFormat("hh-MM-ss");
Scanner scanner = new Scanner(System.in);
while (true){
String msg = scanner.nextLine();
//打印响应的数据
System.out.println("send time : " + format.format(new Date()));
System.out.println(client.sendAndReceive(TCPService.SERVICE_IP, TCPService.SERVICE_PORT,msg));
System.out.println("receive time : " + format.format(new Date()));
}
}
}
解决方案
在socket 服务端发送完数据后,使用 client.shutdownOutput();
关闭输出。使得客户端知道服务端发送数据完成了。如 客户端代码中就加了句代码,如不添加则会使服务端获取客户端请求的地方阻塞。
shutdownOutput() 记得使用!!!