2010-05-05 26 views

回答

0

查看Sun Developer Network的this article。它有一些簡約的例子,應該給你一些想法。

... 
SocketConnection client = (SocketConnection) Connector.open("socket://" + hostname + ":" + port); 
// set application-specific options on the socket. Call setSocketOption to set other options 
client.setSocketOption(DELAY, 0); 
client.setSocketOption(KEEPALIVE, 0); 
InputStream is = client.openInputStream(); 
OutputStream os = client.openOutputStream(); 
// send something to server 
os.write("some string".getBytes()); 
// read server response 
int c = 0; 
while((c = is.read()) != -1) { 
    // do something with the response 
} 
// close streams and connection 
is.close(); 
os.close(); 
client.close(); 
...