public class Socket extends Stream
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.totalcross.com", 80); DataStream ds = new DataStream(socket); ds.writeBytes("GET / HTTP/1.0\n\n"); String ack = socket.readLine(); socket.close();Important: you cannot open a socket before the main event loop. In other words, you cannot open a socket in the app's constructor, but CAN in the initUI method.
When using GPRS connections, it is very important that you set a big timeout (20 seconds at least), otherwise the connection will be closed before the data is fully flushed (which only occurs after Socket.close).
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_OPEN_TIMEOUT
Default timeout value for socket creation
|
static int |
DEFAULT_READ_TIMEOUT
Default timeout value for read operations
|
static int |
DEFAULT_WRITE_TIMEOUT
Default timeout value for write operations
|
int |
readTimeout
Stores the timeout value for read operations.
|
int |
writeTimeout
Stores the timeout value for write operations.
|
skipBuffer
Modifier | Constructor and Description |
---|---|
protected |
Socket()
For internal use only
|
|
Socket(java.lang.String host,
int port)
Opens a socket with the given host, port and open timeout of 1500ms.
|
|
Socket(java.lang.String host,
int port,
int timeout)
Opens a socket with the given host, port, open timeout.
|
|
Socket(java.lang.String host,
int port,
int timeout,
boolean noLinger)
Opens a socket with the given host, port, timeout and linger option.
|
|
Socket(java.lang.String host,
int port,
int timeout,
java.lang.String params)
Opens a socket with the given parameters.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the socket.
|
protected void |
finalize() |
java.lang.String |
getHost()
Returns the remote host which this socket is connected to, passed in the constructor.
|
java.lang.Object |
getNativeSocket()
Used internally by the SSL classes.
|
int |
getPort()
Returns the remote port which this socket is connected to, passed in the constructor.
|
int |
readBytes(byte[] buf)
Reads bytes from the socket into a byte array, from offset 0 to
buf.length.
|
int |
readBytes(byte[] buf,
int start,
int count)
Reads bytes from the socket into a byte array.
|
java.lang.String |
readLine()
Reads a line of text from this socket.
|
int |
writeBytes(byte[] buf,
int start,
int count)
Writes to the socket.
|
asInputStream, asOutputStream, asStream, asStream, skipBytes, wrapInputStream, wrapInputStreamToStream, write, writeBytes, writeBytes, writeBytes
public int readTimeout
public int writeTimeout
public static final int DEFAULT_OPEN_TIMEOUT
public static final int DEFAULT_READ_TIMEOUT
public static final int DEFAULT_WRITE_TIMEOUT
protected Socket()
public Socket(java.lang.String host, int port) throws UnknownHostException, IOException
host
- the host name or IP address to connect toport
- the port number to connect toUnknownHostException
IOException
Socket(String, int, int, String)
public Socket(java.lang.String host, int port, int timeout) throws UnknownHostException, IOException
host
- the host name or IP address to connect toport
- the port number to connect totimeout
- the specified timeout, in milliseconds.UnknownHostException
IOException
Socket(String, int, int, String)
public Socket(java.lang.String host, int port, int timeout, java.lang.String params) throws UnknownHostException, IOException
host
- the host name or IP address to connect toport
- the port number to connect totimeout
- the specified timeout, in milliseconds.params
- the string specifying additional parameters to this socket.
Each parameter is specified in a 'key=value' form and separated
by a ';' from the next parameter. For example: 'p1=v1;p2=v2'.
On BlackBerry, the following parameters are valid: 'apn=[value]',
'apnuser=[value]', 'apnpass=[value]', 'directtcp=[true|false]' and
'nolinger=[true|false]'.
On Palm OS devices, the only valid parameter is 'nolinger=[true|
false]'. If true, the socket is closed immediately, and no ack
is waited from the server. Note that this must be done for the
very first socket creation per application.UnknownHostException
IOException
public Socket(java.lang.String host, int port, int timeout, boolean noLinger) throws UnknownHostException, IOException
host
- the host name or IP address to connect toport
- the port number to connect totimeout
- the specified timeout, in milliseconds.noLinger
- if true, the socket is closed immediately, and no ack is waited
from the server. Note that this must be done for the very first
socket creation per application.UnknownHostException
IOException
Socket(String, int, int, String)
public void close() throws IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
IOException
- If the socket is closed more than oncepublic int readBytes(byte[] buf, int start, int count) throws IOException
readBytes
in class Stream
buf
- the byte array to read data intostart
- the start position in the byte arraycount
- the number of bytes to readIOException
public int readBytes(byte[] buf) throws IOException
buf
- the byte array to read data intoIOException
readBytes(byte[], int, int)
public int writeBytes(byte[] buf, int start, int count) throws IOException
writeBytes
in class Stream
buf
- the byte array to write data fromstart
- the start position in the byte arraycount
- the number of bytes to writeIOException
public java.lang.String readLine() throws IOException
null
if nothing was read.IOException
LineReader
,
BufferedStream
protected void finalize()
finalize
in class java.lang.Object
public java.lang.Object getNativeSocket()
public java.lang.String getHost()
public int getPort()