关闭 BufferedReader/PrintWriter 是否会关闭套接字连接? [英] Does closing the BufferedReader/PrintWriter close the socket connection?

查看:43
本文介绍了关闭 BufferedReader/PrintWriter 是否会关闭套接字连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它使用简单的套接字在两个系统之间传递一些字符.我的 Java 应用程序作为服务器运行.我很好地建立了连接,甚至传递了一条消息.但是,发送完一条消息后,我的连接就会关闭.

I have an application that uses simple sockets to pass some characters between two systems. I have my java application running as a server. I establish a connection fine, and even pass one message. However, after one message has been sent my connection closes.

据我所知,似乎在关闭 printWriterbufferedReader 时,套接字本身正在关闭?!这很糟糕,因为我要在同一个连接上发送多条消息.

From what I can tell it appears as if on closing the printWriter and bufferedReader the socket itself is being closed?! This is bad, as I have multiple messages to send on the same connection.

printWriter = new PrintWriter(theServer.getClientSocket().getOutputStream());
bufferedReader = new BufferedReader(new InputStreamReader(theServer.getClientSocket().getInputStream()));


printWriter.println("the line");

printWriter.close(); //Closing on these lines?
bufferedReader.close(); //Closing on these lines?

我吃饱了吗?我如何在 Java 中维护这种连接?

Am I full of it? How do I maintain this connection in Java?

推荐答案

是的,关闭任何 Writer/Reader 将关闭它们包装的所有其他 Writers 和 Readers.在您准备好关闭底层套接字之前不要关闭它.

Yes, closing any Writer/Reader will close all other Writers and Readers that they wrap. Don't close it until you are ready to close the underlying socket.

这篇关于关闭 BufferedReader/PrintWriter 是否会关闭套接字连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆