这是我的代码:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);//suggested by fge
Socket s = new Socket("127.0.0.1",port);
mapper.writeValue(s.getOutputStream(),anObjectRequest);//this works
Aresponse res = mapper.readValue(s.getInputStream(),AresponseClass.class);
我知道套接字已关闭。
但是,如果查看wireshark,我发现服务器正确接收请求并正确发送答案! 问题出在哪里?
最佳答案
http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#getOutputStream()
Closing the returned OutputStream will close the associated socket
映射器是什么类型?此方法是否:
mapper.writeValue(s.getOutputStream(),anObjectRequest);//this works
对 OutputStream
对象调用 close?
假设您使用的是 ObjectMapper
:
Note: method does not close the underlying stream explicitly here; however, JsonFactory this mapper uses may choose to close the stream depending on its settings (by default, it will try to close it when JsonGenerator we construct is closed).
您必须注意您正在使用哪个 JsonFactory
。
关于java - 当我尝试通过套接字流发送/接收 jxrpc 时出现 "socket closed"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22513053/