# inet::TcpSocket

    sys::Obj
      inet::TcpSocket

`public class TcpSocket` `[javaPeer]`

TcpSocket models a bi-directional TCP stream. Client side sockets are opened using TcpSocket.connect(). Server side sockets opened via TcpServerSocket.bind() and accept(). The Sedona socket APIs are all asynchronous to be used in a single threaded environment.

READ

  `public static const define int READ`

  Bitmask for select when socket is readable

WRITE

  `public static const define int WRITE`

  Bitmask for select when socket is writable

close

  `public native void close()`

  Shutdown and close this socket.

connect

  `public native bool connect(IpAddr addr, int port)`

  Connect this socket to the specified IP address and port. This method is non-blocking. Poll the socket using finishConnect() to determine when the connection been completed (either successfully or not). Return false if there is an immediate failure, or true if this call succeeds.

finishConnect

  `public native bool finishConnect()`

  Poll the socket to see if the connection has completed. Return false if the connection is still in-progress. If the connection attempt has completed then return true and check isClosed() for success.

        - Pending: return false
        - Success: return true, closed=false
        - Failed:  return true, closed=true

  In the case that the state after a finishConnect() call is Failed, you must still call close() to properly free the socket that was opened in the connect() call.

isClosed

  `public bool isClosed()`

  Is this socket closed.

read

  `public native int read(byte[] b, int off, int len)`

  Receive the specified bytes from the socket. Return the number of bytes actually read which may be equal to or less than len. If the connection is terminated or there is any other error close the socket and return -1.

write

  `public native int write(byte[] b, int off, int len)`

  Send the specified bytes over the socket. Return the number of bytes actually written which may be equal to or less than len. If the connection is terminated or there is any other error close the socket and return -1.