waba.io
Class Socket

waba.lang.Object
  |
  +--waba.io.Stream
        |
        +--waba.io.Socket

public class Socket
extends Stream

Socket is a TCP/IP network socket.

Under Java and Windows CE, if no network is present, the socket constructor may hang for an extended period of time due to the implementation of sockets in the underlying OS. This is a known problem.

Here is an example showing data being written and read from a socket:

 Socket socket = new Socket("www.yahoo.com", 80);
 if (!socket.isOpen())
   return;
 byte buf[] = new byte[10];
 buf[0] = 3;
 buf[1] = 7;
 socket.writeBytes(buf, 0, 2);
 int count = socket.readBytes(buf, 0, 10);
 if (count == 10)
   ...
 socket.close();
 


Constructor Summary
Socket(String host, int port)
          Opens a socket.
 
Method Summary
 boolean close()
          Closes the socket.
 boolean isOpen()
          Returns true if the socket is open and false otherwise.
 int readBytes(byte[] buf, int start, int count)
          Reads bytes from the socket into a byte array.
 boolean setReadTimeout(int millis)
          Sets the timeout value for read operations.
 int writeBytes(byte[] buf, int start, int count)
          Writes to the socket.
 
Methods inherited from class waba.lang.Object
toString
 

Constructor Detail

Socket

public Socket(String host,
              int port)
Opens a socket. This method establishes a socket connection by looking up the given host and performing the 3 way TCP/IP handshake.
Parameters:
host - the host name or IP address to connect to
port - the port number to connect to
Method Detail

close

public boolean close()
Closes the socket. Returns true if the operation is successful and false otherwise.
Overrides:
close in class Stream

isOpen

public boolean isOpen()
Returns true if the socket is open and false otherwise. This can be used to check if opening the socket was successful.

setReadTimeout

public boolean setReadTimeout(int millis)
Sets the timeout value for read operations. The value specifies the number of milliseconds to wait from the time of last activity before timing out a read operation. Passing a value of 0 sets no timeout causing any read operation to return immediately with or without data. The default timeout is 1500 milliseconds. This method returns true if successful and false if the value passed is negative or the socket is not open. Calling this method currently has no effect under Win32 or WindowsCE. The read timeout under those platforms will remain the system default.
Parameters:
millis - timeout in milliseconds

readBytes

public int readBytes(byte[] buf,
                     int start,
                     int count)
Reads bytes from the socket into a byte array. Returns the number of bytes actually read or -1 if the server closed the connection or an error prevented the read operation from occurring.
Overrides:
readBytes in class Stream
Parameters:
buf - the byte array to read data into
start - the start position in the byte array
count - the number of bytes to read

writeBytes

public int writeBytes(byte[] buf,
                      int start,
                      int count)
Writes to the socket. Returns the number of bytes written or -1 if an error prevented the write operation from occurring. If data can't be written to the socket for approximately 2 seconds, the write operation will time out.
Overrides:
writeBytes in class Stream
Parameters:
buf - the byte array to write data from
start - the start position in the byte array
count - the number of bytes to write