2012-09-11 39 views
1

我正在創建一個桌面應用程序,其中多個客戶端必須使用它們之間的套接字連接來連接服務器。我成功地將它們連接在一起,但是當我同時連接多個客戶端向服務器出現問題,服務器得到一個錯誤「套接字寫入錯誤」 我的代碼如下PLZ建議我一個答案..當我將多個客戶端同時連接到java服務器時發生套接字寫入錯誤

public class SocketConnection implements Runnable { 
    // password of oracle database 

    ServerSocket serverSocket = null; 
    Socket socket = null; 
    DataInputStream dataInputStream = null; 
    DataOutputStream dataOutputStream = null; 
    Socket clientSocket = null; 
    DBConnection dbConnection; 


    public SocketConnection() { 
     // TODO Auto-generated constructor stub 

     dbConnection = new DBConnection(); 

     if (con != null) { 

      serverSocket = dbConnection.createSocket(); 

      if (serverSocket != null) { 

       System.out.println("Server Started. Looking for the connections."); 
       System.out.println("Listening Port:8888......."); 
      } 



      Thread t = new Thread(this); 
      t.start(); 
     } 

    } 


    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     while (true) { 
      try { 
       clientSocket = serverSocket.accept(); 
       System.out.println("Connection Accepted"); 
       Connect m_connect = new Connect(clientSocket); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

    } 

    public class Connect implements Runnable { 
     Socket clientSocket = null; 
     Thread t = null; 
     private ResultSet res1; 
     private ResultSet res2; 
     Statement stmt; 
     private File mkFolder; 

     public Connect(Socket clientSocke) { 
      // TODO Auto-generated constructor stub 
      this.clientSocket = clientSocke; 

      try { 

       stmt = con.createStatement(); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 

      } catch (Exception e) { 
       // TODO: handle exception 
       e.printStackTrace(); 
      } 
      t = new Thread(this); 
      t.start(); 
     } 

     @Override 
     public void run() { 

      try { 
       dataInputStream = new DataInputStream(
         clientSocket.getInputStream()); 
       dataOutputStream = new DataOutputStream(
         clientSocket.getOutputStream()); 
       System.out.println("Connection established::" 
         + clientSocket.getInetAddress()); 
       String pass = dataInputStream.readUTF(); 
       System.out.println(pass); 
       if (pass.equals("1")) { 
        //here is read n write operation 

       } else if (pass.equals("3")) { 

        //here is read n write operation 

       } else if (pass.equals("2")) { 

        //here is read n write operation 

       } else if (pass.equals("4")) { 
        //here is read n write operation 

       } else if (pass.equals("5")) { 
        //here is read n write operation 

       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } finally { 
       if (socket != null) { 
        try { 
         socket.close(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 

       if (dataOutputStream != null) { 
        try { 
         dataOutputStream.close(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 

       if (dataInputStream != null) { 
        try { 
         dataInputStream.close(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
       if (stmt != null) { 
        try { 
         stmt.close(); 
        } catch (SQLException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 




    } 

} 
+0

你知道是什麼行拋出的錯誤? –

+0

我在讀取和寫入操作時遇到錯誤.. –

+0

哪個讀取/寫入操作,代碼*中的*會拋出該錯誤?很顯然,這是一個讀/寫操作,因爲這是錯誤消息的標題。 –

回答

-1

爲什麼你是以這種低級的方式來做到這一點?選擇一個記錄良好的API並通過它進行交流!

你已經看過這樣的文章:Multithread client server chat on sockets. Load test. recv failed

+0

我不知道如何實現這個 –

+0

對不起,但我不能教你開發一個協議。爲什麼你不使用RMI或其他?然後你可以發送代表你的命令的愚蠢簡單的pojos並返回一些數據。但是我永遠不會實現這個,你試過,我自己 – Mirko

+0

Thanx爲你的考慮。 但我的問題現在解決.. ñ我會嘗試學習這也.. –

相關問題