2012-06-26 142 views
1

我正在通過套接字創建更新客戶端,並且在服務器端出現斷開的管道。服務器接受一個客戶端套接字並使用消息或大字節數組(〜180MB)響應相同的套接字。在本地測試(同一臺計算機上的客戶端和服務器)時不會發生此錯誤,並且似乎在發送字節數組時發生此錯誤。我沒有在客戶端套接字上指定超時時間,也不知道在閱讀完整響應之前它爲什麼會關閉。這是我第一次使用套接字和任何幫助,將不勝感激。Java客戶端/服務器套接字斷開的管道

我的客戶套接字代碼:

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; 
    } 

響應僅僅是一個POJO保持響應(串/字節[]和其他數據)

我的服務器代碼(複製太陽/ 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); 
      } 
     } 
    } 

} 
+1

客戶端內存不足的機會?你在客戶端有沒有例外? – Gray

+1

你能發佈完整的異常和跟蹤嗎?謝謝 – jedwards

+3

格雷,我總是在應有的地方給予獎勵。當我收到沒有解決問題的答案/解決方案時,我只發佈了8個問題並回答了幾個問題。我確實接受並且感謝的幾個問題得到了答案。客戶端內存不足!我應該調查,因爲我在客戶端上使用默認的內存設置。你可能是絕對正確的! – user979051

回答

0

如果客戶是,實際上,運行內存:

java -Xmx512m -jar <the jar> 

java -Xmx512m com.foo.blah.YourClass 

將增加最大堆爲客戶機/服務器。請記住,您可能必須增加管道兩側的堆,因爲在運行時雙方都將讀取所有〜180mb的內存。

相關問題