Search code examples
javawindowsperformanceosx-lionosx-mountain-lion

socket mac os x vs windows java slow


Please help me figure out why the Mac OS X Java takes 5 times longer than the Windows XP Java.

I have some code that behaves differently on Mac and PC Java. I have a Java GUI that talks to a number of "servers" on a Windows XP box.

When I run the GUI on another Windows XP machine or a linux machine the LabView receives the messages and responds within 1 second.

When it runs from the Mac OS X box, it takes 5 seconds. The pause seems to be (from all we can tell debugging) between the time I THINK I send the string "pot 7\r\n" and the time it is actually received by LabView.

LabView sees the pot 7 command (we have a display to check) immediately when coming from Windows but according to the LabView programmer the command does not show up on the screen until the 5 seconds has passed when sent from the Mac OS machine.

I'll try to provide enough code here that someone who knows more might say 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;
}

On Mac OS X 10.8. running java -version gives:

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)

On Windows XP (the java used when the GUI is run on the windows and works fine) gives:

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)

Finally here is part of the log from the 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

And correspondingly from the Mac (note the 5 seconds between the send and the first received):

2013-03-13T12:13:17.092 LabV: [sent] pot 7
2013-03-13T12:13:22.513 LabV: [received] 2.300508
2013-03-13T12:13:22.514 LabV: [received] 2.112090

Thanks in advance for any help/advice.

Follow-up:

I've since discovered that if I use a 10.7.5 Mac OS machine with the same Java the speed is the same as the Windows XP. I am now looking into problems with 10.8.3 and networking and or security firewall settings. Again, any help is appreciated.


Solution

  • Maye this will help someone else.

    When were running on an isolated network, this problem became a show-stopper. After all that work (simulator, outputstream, disabling bonjour, etc), the solution was to edit the hosts file on the Microsoft Windows XP box.

    I think this worked because there was no time spent looking for a WINS or DNS server. What is annoying is that on a mac or linux box on the same network, addressing an IP address automatically eliminated any DNS calls that resulted in timeouts. Editing the file removed the strange delay completely. When I have access to the machine again, I will be more specific about what the edits were.

    The debugging that led us to this problem was noticing some netbios request that took a long time to respond. I was not there when they saw this. My understanding is that they ran a packet sniffer on the mac os machine and saw these requests. When they edited the Windows machine hosts file, the problem went away (it could find the mac without that netbios broadcast).

    "After some packet sniffing we have determined that the delay we've been experiencing when querying the Agilent from the GUI has been due to [the Windows XP machine] flailing about looking for the Agilent [a motor controller] via the WINS service. Can't say that I completely understand what's going on, but we verified that the delay goes away when we turn the NetBIOS service off.

    Which would be all good, except that windows file sharing seems to depend on the NetBIOS service, thus we can't access the disk on [the Windows XP machine] without it.

    So we added two lines in the host file"

    That is what my colleague sent me. So it was two systems away from my java code... I addressed a LabView app on a Windows XP box. I wrote a simulator for the LabView app. But the network problem was between the LabView app and another piece of equipment. So his solution was to put two lines in Windows\System32\Drivers\etc\hosts to resolve the Mac and the Agilent on the Windows side and the network delay went away!