Class powersoft.jcm.net.Socket
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class powersoft.jcm.net.Socket

java.lang.Object
   |
   +----powersoft.jcm.net.BaseSocket
           |
           +----powersoft.jcm.net.Socket

public class Socket
extends BaseSocket
This class serves as the base class for client sockets which are implemented using the DatagramSocket and StreamSocket classes.

Notes: Instances of DatagramSocket and StreamSocket are not used directly. Instead, the method setType() is used to specify the type of the connection. The implementation keeps a pointer to an instance of the appropriate subclass of Socket and dispatches calls through that pointer.

Example:

     // connection-based client stream socket
     Socket socket = new Socket();
     socket.Create( SockSTREAM );
     socket.setRemoteHostName( "watcom.com" );
     socket.setRemotePort( 1 );
     if ( socket.connect() ) {
         socket.send( "Hello!" );
     }
     // connection-based client datagram socket
     Socket socket = new Socket();
     socket.Create( SockDGRAM );
     socket.setRemoteHostName( "watcom.com" );
     socket.setRemotePort( 1 );
     if ( socket.connect() ) {
         socket.send( "Hello!" );
     }
     // non-connection-based client datagram socket
     Socket socket = new Socket();
     socket.Create( SockDGRAM );
     socket.setRemoteHostName( "watcom.com" );
     socket.setRemotePort( 1 );
     socket.send( "Hello!" );
See Also:
StreamSocket, DatagramSocket, BaseSocket

Variable Index

 o SockDGRAM
 o SockSTREAM
 o SockUNKNOWN
socket types

Constructor Index

 o Socket()
 o Socket(Socket)
The following constuctor is used by ServerSocket to return an accepted socket.

Method Index

 o addSocketConnectListener(SocketConnectListener)
 o addSocketDataArrivalListener(SocketDataArrivalListener)
 o addSocketSendCompleteListener(SocketSendCompleteListener)
 o close()
Close the connection if one has been made.
 o connect()
Connect to the current remote host and port.
 o create(byte)
Creates the socket.
 o finalize()
 o getBytesWaiting()
Gets the number of bytes that can be read from a socket without blocking.
 o getSocketConnectListeners()
 o getSocketDataArrivalListeners()
 o getSocketSendCompleteListeners()
 o getType()
Determines the socket type.
 o handleSocketConnect(SocketConnectEvent)
SocketConnectEvent handler
 o handleSocketDataArrival(SocketDataArrivalEvent)
SocketDataArrivalEvent handler
 o handleSocketSendComplete(SocketSendCompleteEvent)
SocketSendCompleteEvent handler
 o IsStream()
 o receive(byte[])
Receive a buffer of data from a connected socket.
 o receive(byte[], int)
Receive a buffer of data from a connected socket.
 o removeSocketConnectListener(SocketConnectListener)
 o removeSocketDataArrivalListener(SocketDataArrivalListener)
 o removeSocketSendCompleteListener(SocketSendCompleteListener)
 o ResetAsynchronous(boolean)
 o ResetLocalPort(int)
 o ResetRemotePort(int)
 o send(byte[])
Send a buffer of data to a connected socket.
 o send(byte[], int)
Send a buffer of data to a connected socket.
 o setType(byte)
Sets the socket type.
 o toString()
Override of toString method of Object.

Variables

 o SockUNKNOWN
  public final static byte SockUNKNOWN
socket types
 o SockSTREAM
  public final static byte SockSTREAM
 o SockDGRAM
  public final static byte SockDGRAM

Constructors

 o Socket
  public Socket()
 o Socket
  protected Socket(Socket socket)
The following constuctor is used by ServerSocket to return an accepted socket.

Methods

 o finalize
  protected void finalize()
Overrides:
finalize in class Object
 o addSocketConnectListener
  public synchronized void addSocketConnectListener(SocketConnectListener l)
 o getSocketConnectListeners
  public synchronized EventListenerVector getSocketConnectListeners()
 o handleSocketConnect
  public synchronized void handleSocketConnect(SocketConnectEvent data)
SocketConnectEvent handler
 o removeSocketConnectListener
  public synchronized void removeSocketConnectListener(SocketConnectListener l)
 o addSocketSendCompleteListener
  public synchronized void addSocketSendCompleteListener(SocketSendCompleteListener l)
 o getSocketSendCompleteListeners
  public synchronized EventListenerVector getSocketSendCompleteListeners()
 o handleSocketSendComplete
  public synchronized void handleSocketSendComplete(SocketSendCompleteEvent data)
SocketSendCompleteEvent handler
 o removeSocketSendCompleteListener
  public synchronized void removeSocketSendCompleteListener(SocketSendCompleteListener l)
 o addSocketDataArrivalListener
  public synchronized void addSocketDataArrivalListener(SocketDataArrivalListener l)
 o getSocketDataArrivalListeners
  public synchronized EventListenerVector getSocketDataArrivalListeners()
 o handleSocketDataArrival
  public synchronized void handleSocketDataArrival(SocketDataArrivalEvent data)
SocketDataArrivalEvent handler
 o removeSocketDataArrivalListener
  public synchronized void removeSocketDataArrivalListener(SocketDataArrivalListener l)
 o getBytesWaiting
  public int getBytesWaiting()
Gets the number of bytes that can be read from a socket without blocking.

Notes: The socket must be created before getting this property. If not, the LastError property is set to SockENotCreated and -1 is returned.

Returns:
the number of bytes available.
See Also:
SocketExceptionCode, receive
 o getType
  public byte getType()
Determines the socket type.

Notes: The socket must be created before getting this property. If not, the LastError property is set to SockENotCreated and SockUNKNOWN is returned.

Returns:
SockSTREAM if the socket is a stream socket, SockDGRAM if it is a datagram socket, or SockUNKNOWN if Create() has not yet been invoked.
See Also:
SocketExceptionCode, setType
 o setType
  public synchronized void setType(byte type)
Sets the socket type.

Notes: The socket must be created before setting this property. If not, the LastError property is set to SockENotCreated.

Parameters:
type - Specifies the socket type. One of:
  • SockSTREAM
  • SockDGRAM
See Also:
SocketExceptionCode, getType
 o create
  public synchronized boolean create(byte type)
Creates the socket.

Notes: The socket must not be created before calling this method. If not, the LastError property is set to SockEIsCreated and false is returned.

Parameters:
type - Specifies the socket type. One of:
  • SockSTREAM
  • SockDGRAM
Returns:
true if and only if the socket was successfully created.
See Also:
SocketExceptionCode, setType
 o connect
  public synchronized boolean connect()
Connect to the current remote host and port.

Notes: The socket must be created before calling this method. If not, the LastError property is set to SockENotCreated and false is returned.

The socket must not be connected before calling this method. If not, the LastError property is set to SockEIsConnected and false is returned.

If the socket is non-asynchronous, this method blocks while the connection is made.

If the socket is asynchronous, this method returns immediately, however, the socket is not connected until the SocketConnect event has been triggered.

If a connection has been made on a datagram socket, a new connection-based stream socket is created.

Returns:
true if the socket is non-asynchronous and the connection was successful, or if the socket is asynchronous and the connection attempt was sucessfully started.
See Also:
SocketExceptionCode, create, setRemoteHostName, setRemotePort
 o send
  public synchronized boolean send(byte buffer[])
Send a buffer of data to a connected socket.

Notes: The socket must be created before calling this method. If not, the LastError property is set to SockENotCreated and false is returned.

If the socket is of type SockSTREAM, then it must be connected before calling this method. If not, the LastError property is set to SockENotConnected and false is returned.

If the socket is non-asynchronous, this method blocks while the send is in progress.

If the socket is asynchronous, this method returns immediately, however, the data transfer to the network is not complete until the SocketSendComplete event has been triggered.

If a connection has been made on a datagram socket, a new connection-based stream socket is created.

Parameters:
buffer - The buffer of data to be sent.
Returns:
true if the socket is non-asynchronous and the send was successful, or if the socket is asynchronous and the send attempt was sucessfully started.
See Also:
SocketExceptionCode, create, send
 o send
  public synchronized boolean send(byte buffer[],
                                   int length)
Send a buffer of data to a connected socket.

Notes: The socket must be created before calling this method. If not, the LastError property is set to SockENotCreated and false is returned.

If the socket is of type SockSTREAM, then it must be connected before calling this method. If not, the LastError property is set to SockENotConnected and false is returned.

If the socket is non-asynchronous, this method blocks while the send is in progress.

If the socket is asynchronous, this method returns immediately, however, the data transfer to the network is not complete until the SocketSendComplete event has been triggered.

If a connection has been made on a datagram socket, a new connection-based stream socket is created.

Parameters:
buffer - The buffer of data to be sent.
length - The number of bytes of data from the buffer to send.
Returns:
true if the socket is non-asynchronous and the send was successful, or if the socket is asynchronous and the send attempt was sucessfully started.
See Also:
SocketExceptionCode, create, send
 o receive
  public synchronized int receive(byte buffer[])
Receive a buffer of data from a connected socket.

Notes: The socket must be created before calling this method. If not, the LastError property is set to SockENotCreated and -1 is returned.

If the socket is of type SockSTREAM, then it must be connected before calling this method. If not, the LastError property is set to SockENotConnected and -1 is returned.

If the socket is non-asynchronous, and the length of the buffer is greater than the number of bytes waiting, this method blocks while the receive is in progress.

If the socket is asynchronous, this method should only be called in response to a SocketDataArrival event, and only the number of bytes waiting should be received (to prevent blocking).

Parameters:
buffer - The buffer of received data.
Returns:
The number of bytes received, or -1 if an error occurred, or 0 if the server closes the connection.
See Also:
SocketExceptionCode, create, getBytesWaiting, receive
 o receive
  public synchronized int receive(byte buffer[],
                                  int length)
Receive a buffer of data from a connected socket.

Notes: The socket must be created before calling this method. If not, the LastError property is set to SockENotCreated and -1 is returned.

If the socket is of type SockSTREAM, then it must be connected before calling this method. If not, the LastError property is set to SockENotConnected and -1 is returned.

If the socket is non-asynchronous, and the length of the buffer is greater than the number of bytes waiting, this method blocks while the receive is in progress.

If the socket is asynchronous, this method should only be called in response to a SocketDataArrival event, and only the number of bytes waiting should be received (to prevent blocking).

Parameters:
buffer - The buffer of received data.
length - The maximum number of bytes of data to receive.
Returns:
The number of bytes received, or -1 if an error occurred, or 0 if the server closes the connection.
See Also:
SocketExceptionCode, create, getBytesWaiting, receive
 o close
  public boolean close()
Close the connection if one has been made.

Notes: The socket must be created before calling this method. If not, the LastError property is set to SockENotCreated and false is returned.

Returns:
true if the connection was successfully closed.
Overrides:
close in class BaseSocket
 o toString
  public String toString()
Override of toString method of Object.
Overrides:
toString in class Object
 o IsStream
  protected boolean IsStream()
 o ResetLocalPort
  protected boolean ResetLocalPort(int port)
Overrides:
ResetLocalPort in class BaseSocket
 o ResetRemotePort
  protected boolean ResetRemotePort(int port)
Overrides:
ResetRemotePort in class BaseSocket
 o ResetAsynchronous
  protected boolean ResetAsynchronous(boolean value)
Overrides:
ResetAsynchronous in class BaseSocket

All Packages  Class Hierarchy  This Package  Previous  Next  Index