Java 客户端/服务器套接字损坏的管道
Posted
技术标签:
【中文标题】Java 客户端/服务器套接字损坏的管道【英文标题】:Java Client/Server Socket Broken Pipe 【发布时间】:2012-06-26 20:43:24 【问题描述】:我正在通过 Sockets 创建一个更新客户端,并且我在服务器端得到了一个 Broken Pipe。服务器接受客户端套接字并使用消息或大字节数组(~180MB)响应同一个套接字。在本地测试(客户端和服务器在同一台机器上)时不会发生该错误,并且似乎在发送字节数组时发生。我没有在客户端套接字上指定超时,并且在阅读完整响应之前不知道它为什么要关闭。这是我第一次使用套接字,任何帮助将不胜感激。
我的客户端套接字代码:
public static Response makeRequest(Request req) throws IOException
Response response = null;
Socket echoSocket = null;
ObjectOutputStream out = null;
ObjectInputStream in = null;
echoSocket = new Socket(serverHost, 10008);
out = new ObjectOutputStream(echoSocket.getOutputStream());
in = new ObjectInputStream(
echoSocket.getInputStream());
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
out.writeObject(req);
try
response = (Response)in.readObject();
catch (ClassNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
out.close();
in.close();
stdIn.close();
echoSocket.close();
return response;
Response 只是一个 POJO 保存响应(string/byte[] 和其他数据)
我的服务器代码(复制 Sun/Oracle 站点的示例并将我的代码添加到其中)
public class Server extends Thread
private Socket clientSocket;
public Server(Socket clientSocket)
this.clientSocket = clientSocket;
start();
public void run()
System.out.println ("New Communication Thread Started");
try
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
Request request = null;
try
request = (Request)in.readObject();
catch (ClassNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
UpdateDAO dao = new UpdateDAO();
ClientDAO cdao = new ClientDAO();
Update update = null;
Client client = null;
Session s = HibernateUtil.currentSession();
Transaction t = s.beginTransaction();
if (request != null)
client = cdao.getClient(request.getClientId());
LogItem log = new LogItem();
log.setClient(client);
log.setTimestamp(new Date());
log.setAction(request.getAction());
if (request.getResponse() != null)
update = dao.getUpdate(request.getResponse().getUpdateId());
TaskContext ctx = new TaskContext(request, client, update, log);
System.out.println("Action: " + request.getAction().getDescription());
Task task = TaskFactory.getTask(request.getAction());
System.out.println(task.getClass().getName());
Response response = task.perform(ctx);
out.writeObject(response);
log.setClientTaskDescription(request.getMessage());
log.setUpdate(ctx.getUpdate());
dao.save(ctx.getLog());
if (ctx.getUpdate() != null)
dao.update(ctx.getUpdate());
else
out.writeObject(new Response("what"));
t.commit();
out.close();
in.close();
clientSocket.close();
catch (IOException e)
e.printStackTrace();
System.exit(1);
public static void main(String[] args) throws IOException
ServerSocket serverSocket = null;
try
serverSocket = new ServerSocket(10008);
System.out.println ("Connection Socket Created");
try
while (true)
System.out.println ("Waiting for Connection");
new Server (serverSocket.accept());
catch (IOException e)
System.err.println("Accept failed.");
System.exit(1);
catch (IOException e)
System.err.println("Could not listen on port: 10008.");
System.exit(1);
finally
try
serverSocket.close();
catch (IOException e)
System.err.println("Could not close port: 10008.");
System.exit(1);
【问题讨论】:
客户端内存不足的可能性?您是否在客户端遇到异常? 你能发布完整的异常和跟踪吗?谢谢 格雷,我总是把功劳归于应得的。我只发布了 8 个问题,并在没有收到无法解决我的问题的答案/解决方案时自己回答了几个问题。我确实得到了回答的几个问题,我接受并感谢了。客户端内存不足!我应该调查一下,因为我在客户端上使用默认内存设置。你可能是绝对正确的! 【参考方案1】:如果客户端实际上内存不足:
java -Xmx512m -jar <the jar>
或
java -Xmx512m com.foo.blah.YourClass
会增加客户端/服务器的最大堆。请记住,您可能必须增加管道两侧的堆,因为双方将在运行时将所有 ~180mb 读入内存。
【讨论】:
以上是关于Java 客户端/服务器套接字损坏的管道的主要内容,如果未能解决你的问题,请参考以下文章