简体   繁体   English

Java客户端/服务器套接字中断管道

[英]Java Client/Server Socket Broken Pipe

I'm creating an update client via Sockets and I'm getting a Broken Pipe on the server side. 我正在通过套接字创建一个更新客户端,并且在服务器端出现了管道中断的情况。 The server accepts a client socket and responds to the same socket with either a message or a large byte array (~180MB). 服务器接受客户端套接字,并使用消息或大字节数组(〜180MB)响应相同的套接字。 The error does not happen when testing locally (both client and server on the same machine) and it seems that it happens while sending the byte array. 在本地测试(同一台计算机上的客户端和服务器)时,不会发生该错误,并且似乎在发送字节数组时会发生。 I'm not specifying a time out on the client socket and don't know why it is closing before reading the full response. 我没有在客户端套接字上指定超时,并且在读取完整响应之前不知道它为什么关闭。 Its my first time working with sockets and any help would be appreciated. 这是我第一次使用套接字,我们将不胜感激。

My Client Socket Code: 我的客户端套接字代码:

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 is just a POJO holding the response (string/byte[] and other data) 响应只是保存响应(字符串/字节[]和其他数据)的POJO

My Server Code (copied an example of Sun/Oracle site and added my code to it) 我的服务器代码(复制了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); 
            } 
        }
    }

}

If the client is, in fact, running out of memory: 实际上,如果客户端的内存不足:

java -Xmx512m -jar <the jar>

or 要么

java -Xmx512m com.foo.blah.YourClass

would increase the maximum heap for the client/server. 将增加客户端/服务器的最大堆。 Keep in mind you may have to increase the heap for both sides of the pipe since both sides would be reading all ~180mb into memory at runtime. 请记住,您可能必须增加管道两边的堆,因为在运行时,两边都将读取所有〜180mb的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM