biz.neustar.wpm.api
Interface TcpSocket


public interface TcpSocket

Raw socket API for connecting and communicating over TCP.

This is a blocking socket, preliminary used for monitoring the uptime of socket services.

Here is an example that queries the HTTP server at example.com and prints out the HTTP headers.

 var net = require('net');

 test.beginTransaction();
 test.beginStep();
 var s = net.newSocket();
 s.connect("google.com", 80);
 s.send("GET http://www.google.com/ HTTP/1.1\r\n");
 s.send("\r\n");

 var r = s.readLine();
 while (true) {
   r = s.readLine();
   if (r == null) {
     break;
   }
   if (r == "") {
     break;
   }
   test.log(r);
 }
 s.close();

 test.endStep();
 test.endTransaction();
 


Method Summary
 void close()
          Close the connection.
 void connect(java.lang.String host, int port)
          Make a TCP connection to the given host#port.
 boolean isConnected()
          Is the connection open?
 Bytes readBytes(int maxBytes)
          Wait for data and return it.
 java.lang.String readLine()
          Read a line of text and return it.
 void send(byte[] bytes)
          Sends the given bytes over the socket to the destination.
 void send(Bytes bytes)
          Sends the given bytes over the socket to the destination.
 void send(java.lang.String str)
          Sends the given string over the socket to the destination as UTF8.
 void send(java.lang.String str, java.lang.String charset)
          Sends the given string over the socket to the destination in the given string encoding.
 void setTimeout(int timeout)
          Make a TCP connection to the given host#port.
 

Method Detail

setTimeout

void setTimeout(int timeout)
                throws java.net.SocketException
Make a TCP connection to the given host#port.

Parameters:
timeout - the connect and receive timeout in milliseconds.
Throws:
java.net.SocketException

connect

void connect(java.lang.String host,
             int port)
Make a TCP connection to the given host#port.

Parameters:
host - host to connect to
port - port to connect to

send

void send(java.lang.String str)
Sends the given string over the socket to the destination as UTF8.

Parameters:
str - the string to send

send

void send(java.lang.String str,
          java.lang.String charset)
Sends the given string over the socket to the destination in the given string encoding.

Parameters:
str - the string to send
charset - the charset to encode string.

send

void send(Bytes bytes)
Sends the given bytes over the socket to the destination.

Parameters:
bytes - the bytes to send

send

void send(byte[] bytes)
Sends the given bytes over the socket to the destination.

Parameters:
bytes - the bytes to send

readBytes

Bytes readBytes(int maxBytes)
Wait for data and return it.

This is a blocking call.

Throws an exception if readLine() has been called previously and data is now buffered.

Parameters:
maxBytes -
Returns:
the raw bytes received. This may be less than maxBytes.

readLine

java.lang.String readLine()
Read a line of text and return it.

A line ending is one of \r, \n or \r\n.

This is a blocking call.

Content is buffered after calling this, and future calls to readBytes() will throw an exception.

Returns:
a UTF-8 string

close

void close()
Close the connection.


isConnected

boolean isConnected()
Is the connection open?

Returns:
true if the connection is still open

Copyright © 2014 Neustar, Inc. All Rights Reserved.