2010-01-28 171 views
-2

我正在本地計算機上運行客戶端服務器java代碼,我正在使用java套接字連接它們。java.net.SocketException:連接重置

我能夠將客戶端連接到服務器並初始發送一串數據。當服務器獲取數據時,它最初也會返回一個字符串。

通信後,整個事情只是崩潰。雙方都沒有迴應。

我正在使用javafx GUI n從服務器返回的數據應該會觸發一些操作(將節點插入javafx階段)。我有一個觀察這個。但即使沒有javafx的東西,客戶端從服務器獲取數據(它實際上會收到)也會掛起。它與輸入/輸出流有關。

顯然很多人都有這個問題,但沒有人發佈解決方案。哦,我已經禁用了我的防火牆。

什麼是重置連接?它可能是javafx?我不是很擅長。另一件事,我使用一個單獨的java類(除了javafx類)來連接。 javafx類創建一個處理連接的對象。所以我只是調用方法(例如,偵聽套接字輸入流的streamFromServer)。只有當我調用這個方法時,客戶端崩潰了,所以這就是我知道它與輸入/輸出流有關的事情。


我能夠解決這個問題,在客戶端側執行正由一個while循環懸浮,如下:

try{ 
    String ins; 
    while((ins = (String)in.readObject()) != null){ 
     //do something with the data read in. 
     //'in' is an ObjectInputStream bound to a client socket 
    } 
} catch(Exception e){ 
    e.printStackTrace(); 
} 

while循環捆綁在客戶端等的執行程序不能超越這一點,所以作爲無知的用戶,我認爲程序崩潰了。

我處理這通過創建實現可運行一個新的類並創建在其內的​​螺紋,它檢查輸入流而不是主客戶端程序,像這樣:

try{ 
    in = new ObjectInputStream(clientSocket.getInputStream()); 
    listenerThread = new StreamListener(in); 
} catch(Exception e){ 
    e.printStackTrace();   
} 

而StreamListener類別:

import java.io.ObjectInputStream; 
import java.util.Observable; 

public class StreamListener extends Observable implements Runnable{ 
    private ObjectInputStream in = null; 
    public String[] bubbles = null; 
    private boolean connectionOpen = true; 
    public StreamListener(ObjectInputStream in){ 
     this.in = in; 
     Thread t = new Thread(this); 
     t.start(); 
    } 
    @Override 
    public void run(){ 
     while(connectionOpen){ 
      try{ 
       String ins; 
       while((ins = (String)in.readObject()) != null){ 
        System.out.println("Received: " + ins); 
        if(ins.equals("noInitial")){ 
         System.out.println("Reply of no initial from server, I must be the first to connect"); 
        } 
        if(ins.contains("initial#")){ 
         initiateBubbles(ins.substring(8)); 
        } 
        if(ins.contains("new#")){ 
         int index = bubbles.length; 
         String s = ins.substring(4); 
         bubbles[index] = s; 
        } 
        if(ins.contains("drag#")){ 
         String s = ins.substring(5), owner = s.substring(0,s.indexOf("#")), x = "", y = ""; 
         String coordinates = s.substring(s.indexOf("#") + 1); 
         for(int i = 0; i < coordinates.length(); i++){ 
          if(coordinates.charAt(i) == '#'){ 
           x = coordinates.substring(0, i); 
           y = coordinates.substring(i + 1, coordinates.length() - 1); 
          } 
         } 
         String[] str; 
         for(int i = 0; i < bubbles.length; i++){ 
          str = bubbles[i].split("#"); 
          if(str[0].equals(owner)){ 
           str[2] = x; 
           str[3] = y; 
          } 
         } 
        } 
        continue; 
       } 
      } catch(Exception e){ 
       connectionOpen = false; 
       System.err.println("Could not receive bubble data from server: " + e.toString()); 
      } 
     } 
    } 
    public String[] getBubbles(){ 
     return bubbles; 
    } 
    public void initiateBubbles(String s){ 
     bubbles = s.split("@"); 
     System.out.println("Bubbles initialised"); 
     fireNotify(); 
    } 
    public void moveBubble(String s){ 
     fireNotify(s); 
    } 
    public void fireNotify(){ 
     setChanged(); 
     notifyObservers(); 
     System.out.println("Observer notified"); 
    } 
    public void fireNotify(String s){ 
     setChanged(); 
     notifyObservers(s); 
     System.out.println("Observer notified"); 
    } 
    public void close(){ 
     connectionOpen = false; 
     try{ 
      in.close(); 
     } catch(Exception e) { 
      System.err.println("Could not close listener thread: " + e.toString()); 
     } 
    } 
} 

和瞧!這讓我想到我的下一個問題,不知何故,觀察者沒有得到通知。誰能告訴我爲什麼?這裏的JavaFX的觀察類:

import java.util.Observable; 
import java.util.Observer; 
import java.lang.System; 

public class BubbleAdapter extends Observer{ 
    public-read var bubbles : Bubble[]; 
    public-read var bubblesInitialised : Boolean = false; 
    public-read var bubbleString : String[]; 
    public-init var connector : StreamListener 
     on replace {connector.addObserver(this)}; 

    override function update(observable : Observable, arg : Object){ 
     FX.deferAction(
      function() : Void { 
       System.out.println("Observer called"); 
       if(arg == null){ 
        bubbleString = connector.getBubbles(); 
        var str : String[]; 
        for(i in [0..sizeof bubbleString]){ 
         if(bubbleString[i].contains("#")){ 
          str = bubbleString[i].split("#"); 
          bubbles[i] = Bubble { 
           name : bind str[0] 
           time : bind str[1] 
           translateX : bind Float.parseFloat(str[2]) 
           translateY : bind Float.parseFloat(str[3]) 
          } 
          //insert bubble after Main.stage.scene.content[Main.currentIndex++]; 
         } 
        } 
        bubblesInitialised = true; 
       } 
       else if(arg instanceof String){ 

       } 

      } 
     ); 
    } 
} 

沒關係一些本質gritties,這個觀測器,首先應該打印出「觀察家稱爲」,這不會發生。所以再次請幫助。

+1

如果你不顯示套接字相關的代碼,我懷疑任何人都可以指出該代碼中的錯誤。除了;正如你提到的崩潰,你應該有一個堆棧跟蹤。如果它不再響應,可能是你不讀緩衝區(所以沒有地方可以再接收),或者你嘗試讀取一個空緩衝區。沒有代碼,很難看到原因。 – extraneon 2010-01-28 14:13:27

回答

1

首先看看你的錯誤日誌。例外情況,您的程序或調用您的應用程序的第三方容器引發並捕獲的消息。

對於調試網絡通信,只需在該LAN上的任何PC上運行Wireshark, 並觀察雙方之間的數據包 - 使用端口號,協議-tcp/udp過濾掉數據包。

+0

嗨周杰倫,你的建議幫助..thanx。你現在可以幫我解決這個問題嗎?觀察員?我已經在下面的答案中發佈了所有內容。 – Joey 2010-02-05 11:26:39

相關問題