Skip to content

Commit

Permalink
Merge pull request #118 from keithc-ca/simpler-attach
Browse files Browse the repository at this point in the history
Simplify creation of a socket connection to the attacher
  • Loading branch information
pshipton authored Sep 26, 2017
2 parents a9fb06b + 9b442e5 commit 61ff3e4
Showing 1 changed file with 18 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
package com.ibm.tools.attach.target;

import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;
Expand Down Expand Up @@ -106,44 +106,33 @@ private static final class MethodRefsHolder {
* @return true if successfully connected
*/
boolean connectToAttacher(int portNum) {

try {
InetAddress localHost;
try {
IPC.logMessage("connectToAttacher using \"localhost\" portNum=", portNum); //$NON-NLS-1$
localHost = InetAddress.getByName("localhost"); //$NON-NLS-1$
attacherSocket = new Socket(localHost, portNum);
} catch (UnknownHostException otherException) {
IPC.logMessage("connectToAttacher using getLocalHost() portNum=", portNum); //$NON-NLS-1$
localHost = InetAddress.getLocalHost();
attacherSocket = new Socket(localHost, portNum);
}
InetAddress localHost = InetAddress.getLoopbackAddress();
attacherSocket = new Socket(localHost, portNum);
IPC.logMessage("connectToAttacher localPort=", attacherSocket.getLocalPort(), " remotePort=", Integer.toString(attacherSocket.getPort())); //$NON-NLS-1$//$NON-NLS-2$
responseStream = attacherSocket.getOutputStream();
commandStream = attacherSocket.getInputStream();
AttachmentConnection.streamSend(responseStream, Response.CONNECTED + ' '
+ key + ' ');
AttachmentConnection.streamSend(responseStream, Response.CONNECTED + ' ' + key + ' ');
return true;
} catch (IOException e) {
IPC.logMessage("connectToAttacher exception " + e.getMessage() + " " + e.toString()); //$NON-NLS-1$ //$NON-NLS-2$
closeQuietly(responseStream);
closeQuietly(commandStream);
closeQuietly(attacherSocket);
} catch (Exception otherException) {
IPC.logMessage("connectToAttacher exception ", otherException.toString()); //$NON-NLS-1$
}
return false;
}

private static void closeQuietly(Closeable stream) {
if (null != stream) {
try {
if (null != responseStream) {
responseStream.close();
}
if (null != commandStream) {
commandStream.close();
}
if (null != attacherSocket) {
attacherSocket.close();
}
stream.close();
} catch (IOException e1) {
return false;
// ignore
}
return false;
} catch (Exception otherException) {
IPC.logMessage("connectToAttacher exception ", otherException.toString()); //$NON-NLS-1$
return false;
}
return true;
}

@Override
Expand Down

0 comments on commit 61ff3e4

Please sign in to comment.