2012-05-03 269 views
1

這是我的客戶:的Java通信客戶端/服務器

public static void logIn(String name, String pass) { 
try { 
    Socket s = new Socket("localhost", 9000); 
    PrintStream out = new PrintStream(s.getOutputStream()); 
    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 
    out.print(name + "," + pass); 
    out.flush(); 
    out.close(); 

    System.out.println(in.readLine()); 

    in.close(); 
    s.close(); 
} 
catch(UnknownHostException exp) 
{ 
    exp.printStackTrace(); 

} 
catch(java.io.IOException exp) 
{ 
    exp.printStackTrace(); 
} 
} 

,這我在我的服務器:

public static void main(String[] args){ 
boolean clientExists=false; 
ArrayList<User> users = new ArrayList<User>(); 
users.add(new User("jmeno","heslo")); 
ServerSocket ss; 
try { 
    ss = new ServerSocket(9000); 
    while(true) { 
       clientExists=false; 
     Socket s = ss.accept(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 
     PrintStream out = new PrintStream(s.getOutputStream()); 
     String xmlpacket=""; 
       String pom; 

       while ((pom = in.readLine())!=null){ 
        xmlpacket+=pom; 
        xmlpacket+="\n"; 
       } 

       for(User us: users) 
        { 
          if(us.isUserNameAndPasswordRight(login, passwd)) 
          { 
            out.print("user is connected"); 
            out.flush(); 
            clientExists=true; 
          } 
        } 
       } 
       if(clientExist != true) 
       out.print("bad login"); 
     out.flush(); 
     out.close(); 
     in.close(); 
     s.close(); 
} 
catch(java.io.IOException exp) 
{ 
    System.out.println("chyba u socketu!"); 
} 

}

它更多鈔票,以這樣的工作?我不能得到這個工作,因爲在客戶端嘗試從服務器讀取答案時出現異常。

編輯: 這是堆棧跟蹤:

java.net.SocketException: socket closed 
    at java.net.SocketInputStream.socketRead0(Native Method) 
    at java.net.SocketInputStream.read(Unknown Source) 
    at java.net.SocketInputStream.read(Unknown Source) 
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source) 
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source) 
    at sun.nio.cs.StreamDecoder.read(Unknown Source) 
    at java.io.InputStreamReader.read(Unknown Source) 
    at java.io.BufferedReader.fill(Unknown Source) 
    at java.io.BufferedReader.readLine(Unknown Source) 
    at java.io.BufferedReader.readLine(Unknown Source) 
    at client.client.logIn(client.java:79) 


    at client.GUI.GUI.jMenuItem1ActionPerformed(GUI.java:379) 
    at client.GUI.GUI.access$5(GUI.java:367) 
    at client.GUI.GUI$5.actionPerformed(GUI.java:151) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.AbstractButton.doClick(Unknown Source) 
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source) 
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$000(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 
+1

請提交例外加上堆棧跟蹤和突出問題的方法/線路 –

回答

2

我做了幾件事情,所以給所有的代碼。

  • 稍後放置緩衝讀取器,以防止阻塞。
  • 增加了編碼,因此它不使用默認的操作系統編碼(不同操作系統上的客戶端)。
  • Did println i.o.打印。
  • 自動清理。
  • 重要:沒有PrintStream,但PrintWriter。
  • xmlpacket上沒有循環;那是後來的代碼。

    public static void logIn(String name, String pass) { 
    try { 
        Socket s = new Socket("localhost", 9000); 
        //PrintStream out = new PrintStream(s.getOutputStream(), true, "UTF-8"); 
        PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8"), true); 
        out.println(name + "," + pass); 
        out.flush(); 
    
        BufferedReader in = new BufferedReader(new InputStreamReader(
          s.getInputStream(), "UTF-8")); 
    
        System.out.println(in.readLine()); 
    
        out.close(); 
        in.close(); 
        s.close(); 
    } catch (UnknownHostException exp) { 
        exp.printStackTrace(); 
    
    } catch (java.io.IOException exp) { 
        exp.printStackTrace(); 
    } 
    } 
    
    
    public static void main(String[] args) { 
    ArrayList<User> users = new ArrayList<User>(); 
    users.add(new User("jmeno", "heslo")); 
    ServerSocket ss; 
    try { 
        ss = new ServerSocket(9000); 
        while (true) { 
         Socket s = ss.accept(); 
         System.out.println("Accept..."); 
         BufferedReader in = new BufferedReader(new InputStreamReader(
           s.getInputStream(), "UTF-8")); 
         PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8"), true); 
         String xmlpacket = ""; 
         String pom; 
    
         pom = in.readLine(); 
         //while ((pom = in.readLine()) != null) { 
          xmlpacket += pom; 
          xmlpacket += "\n"; 
         //} 
         int commaPos = xmlpacket.indexOf(','); 
         int newlinePos = xmlpacket.indexOf('\n'); 
         String login = xmlpacket.substring(0, commaPos); 
         String passwd = xmlpacket.substring(commaPos + 1, newlinePos);       
    
         boolean clientExists = false; 
         for (User us : users) { 
          if (us.isUserNameAndPasswordRight(login, passwd)) { 
           out.println("user is connected"); 
           clientExists = true; 
           break; 
          } 
         } 
         if (!clientExists) 
          out.println("bad login"); 
         out.close(); 
         in.close(); 
         s.close(); 
        } 
    } catch (java.io.IOException exp) { 
        System.out.println("chyba u socketu!"); 
    } 
    } 
    
2

嘗試帶走了out.close()看看會發生什麼。我相信關閉OutputStream也會關閉相關的套接字。在閱讀回覆之前,你不想這樣做。

更新:只是要清楚,我的意思是帶走(或移動到末尾)在客戶端,而不是服務器的out.close()

+0

這就是想的太多,但是當我把它添加到末尾,則客戶端沒有響應,並與登錄服務器鴕鳥政策GET消息和密碼。 –

+1

你只是打敗了我! –

+0

@Bibo - 如果你已經做了一些調試,那麼你應該告訴我們關於它的事情!如果你不給我們所有的信息,我們只是在浪費我們的時間。 –

2

'out.print(name +「,」+ pass);'似乎不會發出行結束符,因此您的客戶端中的'readLine()'將不會返回。嘗試'println'並擺脫封閉的陷阱。

+0

@MartnJames不錯的一個! –

+0

仍然無法通過。我用該println讀取所有數據,但不能繼續使用代碼。 –

+0

它看起來像in.ReadLine()仍然在等待更多的線條閱讀,或者我不知道爲什麼在debbuger我在,然後當它應該去下一行的代碼,但綠線在debbuger錯過(我使用日食)和gui在客戶端沒有響應。 –