Direct Socket Connection

Hi,

We need to sent and receive data through an socket connection to an external socket server. If we run a java program outside wm we dont have any problems open the connection, send and receive data. But when we implement the code below in a Java Service inside wm the application hang until the connection times out. Any ideas on how to solve the problem ?

Thanks
Örjan

Code:

// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
String host = IDataUtil.getString(pipelineCursor, “host”);
String port = IDataUtil.getString(pipelineCursor, “port”);
String data = IDataUtil.getString(pipelineCursor, “data”);
String responseEndTag = IDataUtil.getString(pipelineCursor, “responseEndTag”);
pipelineCursor.destroy();
StringBuffer xml = new StringBuffer("");
try {
InetAddress inetAddress = InetAddress.getByName(host);
SocketAddress socketAddress = new InetSocketAddress(inetAddress, Integer.parseInt(port));

// unbound socket
Socket socket = new Socket();

// TimeoutMs.
int timeoutMs = 2000; // 2 seconds
socket.connect(socketAddress, timeoutMs);
OutputStream out = socket.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

out.write(data.getBytes());
String line = “”;

if (in.ready()){
while((line = in.readLine()) != null){
xml.append(line);
if(line.endsWith(responseEndTag)){
break;
}
}
}
out.close();
in.close();
socket.close();
}
catch (UnknownHostException e) {
xml.append(e.getMessage());
}
catch (SocketTimeoutException e) {
xml.append(e.getMessage());
}
catch (IOException e) {
xml.append(e.getMessage());
}
data = xml.toString();
// pipeline
IDataCursor pipelineCursor_1 = pipeline.getCursor();
//valueList[0] = “data”;
IDataUtil.put( pipelineCursor_1, “data”, data );
pipelineCursor_1.destroy();

They should be the same.
Did you run your java code on the same box as the WM server? Make sure the remote server is reachable from the WM server.
Also try longer timeout time.

what kind of timeout occurs, connect/send/read???

if connect/send please check your link network connection (see tongwang comment).
else, may be this could be help you:

BufferedInputStream reader = new BufferedInputStream(socket.getInputStream());
byte[] b = new byte[1024];
String response = reader.read(b);

  • refer to java API of using BufferedInputStream

I think there is a sample of using sockets within IS available on Advantage. It may be worth a search.

Or there is this one in the wMUsers Shareware section from our esteemed leader, Mark Carlson. http://www.wmusers.com/software/uploads/531SocketExample.zip

Thanks for your help!

We had some proxy and firewall issues causing the problems.