2013-03-19 52 views
2

請幫我弄清楚爲什麼Mac OS X Java比Windows XP Java要長5倍。socket mac os x vs windows java慢

我有一些代碼在Mac和PC上的行爲有所不同。我有一個Java GUI,可以與Windows XP上的許多「服務器」進行對話。

當我在另一臺Windows XP機器或Linux機器上運行GUI時,LabView接收消息並在1秒內響應。

當它從Mac OS X框運行時,需要5秒。我認爲我發送字符串「pot 7 \ r \ n」的時間與LabView實際收到的時間之間的暫停似乎是(從我們可以判斷的所有調試中)。

當從Windows進來時,LabView會立即看到pot 7命令(我們有一個顯示器來檢查),但根據LabView程序員的命令,直到從Mac OS發送5秒鐘後才顯示在屏幕上機。

我會嘗試在這裏提供足夠的代碼,以便知道更多的人可能會說aha!

String IPaddress; 
int commPort; 
BufferedReader reader; 
PrintWriter writer; 
Socket sock; 
long timeout = 8000; 

...

public synchronized void connectCommPort() { 
    if (commportconnected != true) { 
     try { 
      sock = new Socket(); 
      InetSocketAddress endpoint = new InetSocketAddress(IPaddress, commPort); 
      sock.connect(endpoint, timeout); 
      sock.setSoTimeout(timeout); 
      reader = new BufferedReader(new InputStreamReader(sock.getInputStream())); 
      writer = new PrintWriter(sock.getOutputStream()); 
      commportconnected = true; 
      errorstatus = false; 
     } catch (IOException ex) { 
      logwriter("LabV: WARNING - network connection to Labview command port failed."+ex.toString()); 
      commportconnected = false; 
      errorstatus = true; 
     } 
    } 
} 

...

public synchronized float[] readpots() { 

    String message = "pot 7"; 
    connectCommPort(); 

    if (commportconnected) { 
     try { 
      writer.print(message + "\r\n"); 
      writer.flush(); 
      logwriter("LabV: [sent] " + message); 

      shortpotvalues = potslistener(); 
     } catch (Exception ex) { 
     } 
     disconnectCommPort(); 
    } 
    potvalues[0] = shortpotvalues[0]; 
    potvalues[1] = shortpotvalues[1]; 
    potvalues[2] = shortpotvalues[2]; 
    potvalues[3] = shortpotvalues[3]; 
    potvalues[4] = shortpotvalues[4]; 
    potvalues[5] = shortpotvalues[5]; 
    potvalues[6] = shortpotvalues[6]; 
    potvalues[7] = 5.0f; 


    return potvalues; 
} 

public synchronized float[] potslistener() { 

    String message = null; 
    int i = 0; 
    try { 
     //while ((message = reader.readLine()) != null && i < shortpotvalues.length) { 
     while (i < shortpotvalues.length) { 
      message = reader.readLine(); 
      if (message != null) 
      { 
       logwriter("LabV: [received] " + message); 
       if (message.contains(".")) 
       { 
        shortpotvalues[i] = Float.parseFloat(message); 
        i++; 
       } 
      } 
      else 
      { 
       logwriter("LabV: received NULL unexpectedly, may not have all pots correct"); 
      } 
     } // close reader-ready-while 
    } catch (ArrayIndexOutOfBoundsException aiofbex) { 
     logwriter("LabV: in potslistener() Array out of bounds! " + aiofbex.toString()); 
    } catch (Exception ex) { 
     logwriter("LabV: in potslistener() got exception: " + ex.toString()); 
    } 
    return shortpotvalues; 
} 

在Mac OS X 10.8。運行Java的版本給出:

marks-Mac-mini:~ mark$ java -version 
java version "1.6.0_43" 
Java(TM) SE Runtime Environment (build 1.6.0_43-b01-447-11M4203) 
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01-447, mixed mode) 

在Windows XP(當GUI是在Windows上運行,並工作正常使用的Java)給出:

Microsoft Windows XP [Version 5.1.2600] 
(C) Copyright 1985-2001 Microsoft Corp. 

C:\Documents and Settings\gus>java -version 
java version "1.7.0_07" 
Java(TM) SE Runtime Environment (build 1.7.0_07-b10) 
Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode, sharing) 

最後這裏是日誌從的一部分PC:

2013-03-19T11:45:22.000 LabV: [sent] pot 7 
2013-03-19T11:45:22.921 LabV: [received] 2.310835 
2013-03-19T11:45:22.921 LabV: [received] 2.447397 

並相應地從Mac(注意發送的5秒,第一個接收):

在此先感謝您的任何幫助/建議。

後續:

因爲我已經發現,如果我使用的是10.7.5的Mac OS機器在同一個Java的速度是相同的Windows XP。我現在正在研究10.8.3和網絡和/或安全防火牆設置的問題。再次,任何幫助表示讚賞。

+0

您可能嘗試禁用nagle算法:socket.setTcpNoDelay(true) – 2013-03-19 22:01:53

+0

感謝您的想法,但它沒有幫助。 – evernoob 2013-03-20 13:41:59

+0

我懷疑它是PrintWriter *自動沖洗引起的問題,即使你強制flush()。您可以嘗試直接寫入流,而不是「PrintWriter」。 'socket.getOutputstream()寫((消息+ 「\ r \ n」 個)的getBytes( 「UTF-8」)。); socket.getOutputstream()。flush();' – 2013-03-20 16:00:13

回答

0

Maye這會幫助別人。

當在一個孤立的網絡上運行時,這個問題變成了一個阻礙。在完成所有這些工作(模擬器,輸出流,禁用bonjour等)之後,解決方案是編輯Microsoft Windows XP盒上的hosts文件。

我認爲這有效,因爲沒有花時間尋找WINS或DNS服務器。令人討厭的是,在同一網絡的Mac或Linux機器上,尋址IP地址會自動消除任何導致超時的DNS調用。編輯文件完全消除了奇怪的延遲。當我再次訪問機器時,我將更具體地瞭解編輯內容。

導致我們遇到這個問題的調試注意到一些需要很長時間才能響應的netbios請求。當他們看到這個時,我不在那裏。我的理解是,他們在mac os機器上運行了一個數據包嗅探器,並看到了這些請求。當他們編輯Windows機器主機文件時,問題就消失了(它可以找到沒有netbios廣播的mac)。

「的一些數據包嗅探後,我們已經確定,查詢從GUI安捷倫當我們已經 正在經歷的延遲已因 爲[Windows XP的機器]揮舞關於尋找安捷倫[一 通過WINS服務電機控制器。不能說我完全 明白髮生了什麼事情,但我們驗證了延遲消失 當我們把NetBIOS服務關閉。

這將是所有好,除了 該Windows文件共享似乎取決於NetBIOS服務,因此 我們無法訪問[Windows XP計算機]上的磁盤。

所以我們在主機文件」。

這就是我的同事給我發了添加了兩行,所以這是從我的Java代碼,這兩個系統了......我解決在Windows XP一個LabVIEW應用程序我爲LabView應用程序編寫了一個模擬器,但網絡問題出現在LabView應用程序和另一臺設備之間,所以他的解決方案是在Windows \ System32 \ Drivers \ etc \ hosts中放置兩行來解析Mac和安捷倫在Windows端和網絡延遲消失!