Groovy TCP client hanging

I'm usually a .net dev (don't hurt me!) so forgive any really stupid mistakes I've made here :)

I have a TCP listener written in .net which recieves xml and sends back a response. I'm trying to write a client for it in groovy so I can load test with loadUI. Here is what I have so far:

def s = new Socket("10.208.24.59", 9061);
s.withStreams { inStream, outStream ->
  def reader = inStream.newReader()
  def responseText = reader.readLine()
  outStream << "Hello test server"
  println "response = $responseText"
}
s.close();

I'm debugging in eclipse and it is hanging on the withStreams line. What I should recieve is a "Message was not XML" message, which I can get fine via telnet.

Any ideas what I'm doing wrong?

Update I've just tried this instead of the withStreams closure:

def r = new BufferedReader(new InputStreamReader(s.getInputStream()));
def w = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
w.write("Hello test server 2");
w.flush();
println r.readLine();
w.flush();
w.close();

Its now hanging on the println r.readLine() call

Update again

It turns out that this was a problem with the way the remote service was closing (or more to the point - wasn't closing) the stream. Both .net and our mainframe were handling it correctly but the groovy script wasn't happy. I've fixed the service and the script runs happily now, worth bearing in mind for anyone else who runs up against something similar.

Answers


===== Update ====

Hey Chris;

Try putting the socket write outside the streams processor. You can direct the output straight at the socket since Groovy leftShifted the socket class. Also, not directly related, but helpful for debugging, put a socket read timeout so your thread does not block endlessly.

def s = new Socket("10.208.24.59", 9061);
s.setSoTimeout(3000);
s << "Hello test server";
s.withStreams { inStream, outStream ->
  def reader = inStream.newReader()
  def responseText = reader.readLine()
  println "response = $responseText"
}
s.close()

===== /Update ====

The problem is that newReader blocks waiting for output from your server. See the stack trace below. Since you never get to send your request, there's no response from the server. In a nutshell, do not issue a listen before sending a request when using a single thread. Change your code to this and it should work:

def s = new Socket("10.208.24.59", 9061);
s.withStreams { inStream, outStream ->
  outStream << "Hello test server"  // send request first
  def reader = inStream.newReader()
  def responseText = reader.readLine()      
  println "response = $responseText"
}
s.close();

The stack trace for the waiting thread looks like this:

Stack trace: 
java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(SocketInputStream.java:129)
sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
   - locked java.io.InputStreamReader@2119899e
java.io.InputStreamReader.read(InputStreamReader.java:167)
java.io.BufferedReader.fill(BufferedReader.java:136)
java.io.BufferedReader.readLine(BufferedReader.java:299)
   - locked java.io.InputStreamReader@2119899e
java.io.BufferedReader.readLine(BufferedReader.java:362)
java_io_BufferedReader$readLine.call(Unknown Source)
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)

Need Your Help

Spark read subgroup of binary files

python regex binary apache-spark pyspark

Is there a simple way to read only a given set of files contained in a directory with a Spark API (I use PySpark API) and the binaryFiles method?

Css 100% height just wont work

css height

I want my div.container to be 100% height to fill the whole screen.