Socket
Overview
The Socket class allows you to open TCP/IP connections from your device. To be able to establish a connection with a particular server, both the server and the device must be connected to a common network (e.g. a local network that connects your computers and devices, or the Internet).
A Socket object denotes a client-server connection over a network using the TCP/IP protocol, where the device acts as a client, which connects to a specific server port.
The Socket class does not provide many methods besides the ones inherited from Stream. After creating a socket, you’ll usually just perform read and write operations, closing the socket after you’re done.
However, streams that handle remote connections are more likely to fail due to hardware and communication problems, so we shouldn’t handle socket operations the same way we do with file operations.
In TotalCross, socket’s read and write operations are blocking with a timeout – that means that socket methods will block the thread until the operation is completed, or the timeout for the operation is reached. If the operation is completed, the method returns immediately, regardless of the amount of time left. Otherwise, the method will just return the amount of data processed.
It’s important to notice that no exception is thrown if the method times out. This is just a way to prevent your thread from being blocked indefinitely because of communication problems. You may just keep executing the same method until it finishes processing all the data.
Constructors
You cannot open a socket before the main event loop. In other words, you cannot open a socket in the app’s constructor, but you can in the initUI( ) method.
The socket general behavior, including the timeout for read and write operations, are stored in the following public fields:
readTimeout
- The timeout value for read operations. Its default value is Socket.DEFAULT_READ_TIMEOUT
.writeTimeout
- The timeout value for write operations. Its default value is Socket.DEFAULT_WRITE_TIMEOUT
.
Usually you should not worry about the read and write timeouts. The default values will be fine in most cases. However, you may increase the timeout value if you experience problems with slow connections.
Reducing the timeout value is usually a bad idea. If your device has a high speed connection, the read and write methods should also be fast and return before the timeout is reached. However, reducing the timeout value won’t give you any benefit, and may even decrease your program performance.
Methods for read and write operations:
Methods:
Except for the method readLine( ), the methods above are just available for convenience and may be replaced by read/write calls inherited from Stream. This has a cost however - using these methods increases the number of method calls for each read/write operation by one (they actually just call the Stream methods). If your application makes heavy use of sockets, you may avoid using these methods to improve its performance.
References
For more details, check out totalcross.net.Socket JavaDoc.
Last updated