Reputation: 63
I was using JSR223 Sampler to send TCP Request Data in order to send to the IP. But I am not sure of the built-in functions to do so. I have only been successful to see whether the port is listening or not its code is given below:
def sock = new Socket()
def host = "myIPNumber" // change it to your host
def port = myPort // change it to your port
def timeout = 10000 // change it to your timeout (in milliseconds)
sock.setSoTimeout(10000)
sock.connect(new InetSocketAddress(host, port))
if (sock.isConnected()) {
log.info('Connection established')
SampleResult.setSuccessful(true)
}
else {
log.info('Server is not listening')
SampleResult.setSuccessful(false)
}
Can anyone please guide me how I can send some request data to my TCP IP and save its response in some variable.
Things that I've tried:
I've used TCP Sampler but I'm not getting anything in results tree unless I set some connection time and it gives a 500 error. Same scenario for HTTP Raw Request.
Upvotes: 1
Views: 1248
Reputation: 168072
If you wrote this code all you need to add is using Socket.getOutputStream() for sending the data and Socket.getInputStream() for receiving it.
If not and you copied and pasted it from somewhere:
To send data:
def output = sock.getOutputStream()
def data = "hello".getBytes()
output.write(data)
output.flush()
to receive data:
def input = sock.getInputStream()
def responseReader = new BufferedReader(new InputStreamReader(input))
def firstLine = sock.readLine()
to save it into a JMeter Variable:
vars.put('firstLine', firstLine)
More information:
Upvotes: 1