我有一个 java 客户端和一些 Tomcat 服务器 - Web 服务器。我必须在同一台服务器上执行一系列操作。 我的想法是使用相同的 tcp session ,使用以下链: 读,写,读,写... - 在服务器端 写,读,写,读... - 在客户端
问题 - 在读取之后,在 tomcat 服务器上写入 - 下一次读取得到 -1 或 EOFException。
客户端代码:
java.net.URL u = new URL("http", "127.0.0.1", 8080, "/Dyno/BasicServlet");
HttpUrlConnection huc = (HttpURLConnection)u.openConnection();
huc.setRequestMethod("POST");
huc.setDoOutput(true);
huc.connect();
os = huc.getOutputStream();
byte[] b = info();
os.write(b)
os.flush();
is = huc.getInputStream();
byte[] b2 = new byte[10];
is.read(b2);
byte[] b = info(b2);
os.write(b)
服务器代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletInputStream is = request.getInputStream();
ServletOutputStream os = response.getOutputStream();
byte[] clientMsg = new byte[10];
is.read(clientMsg);
serverMsg = respond(clientMsg);
os.write(serverMsg)
os.flush();
is.read(); //Here I get -1
我的理论是 Tomcat 正在关闭流。 你同意? 无论如何要绕过这个?
谢谢。
最佳答案
HTTP 只是请求-响应。 但是 WebSockets 允许在客户端和服务器之间进行全双工通信。
Apache Tomcat 7 初步支持 WebSockets。
关于java - Apache Tomcat - 来回流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14419567/