6.4. TCP Communication¶
6.4.1. Dependencies¶
TCP communication requires the NET
foundation library.
To use it, add the following dependency to your project’s module.ivy
file:
<dependency org="ej.api" name="net" rev="1.1.2" />
6.4.2. Replacing the javax.microedition.io.SocketConnection
class¶
Code using javax.microedition.io.SocketConnection
can use java.net.Socket
instead.
6.4.2.1. Code sample¶
The following snippet is extracted from the J2ME Javadoc.
SocketConnection sc = (SocketConnection)
Connector.open("socket://host.com:79");
sc.setSocketOption(SocketConnection.LINGER, 5);
InputStream is = sc.openInputStream();
OutputStream os = sc.openOutputStream();
os.write("\r\n".getBytes());
int ch = 0;
while(ch != -1) {
ch = is.read();
}
is.close();
os.close();
sc.close();
It can be adapted as follows:
Socket sc = new Socket("host.com", 79);
sc.setSoLinger(true, 5);
InputStream is = sc.getInputStream();
OutputStream os = sc.getOutputStream();
os.write("\r\n".getBytes());
int ch = 0;
while (ch != -1) {
ch = is.read();
}
is.close();
os.close();
sc.close();