1

我知道在 Blackberry 中编程时可以打开与 URL 的连接,但是否可以在特定端口上打开连接?例如,我想向服务器的 echo 端口发送一些数据以检查它是否处于活动状态并测量 ping 时间。有任何想法吗 ?

4

2 回答 2

1

尝试这样的事情;

// Create ConnectionFactory
ConnectionFactory factory = new ConnectionFactory();

// use the factory to get a connection descriptor
ConnectionDescriptor conDescriptor = factory.getConnection("socket://www.abc.com:portnumber");

您可以在指定打开连接的 url 时指定端口号。

于 2012-10-22T13:06:11.153 回答
0

试试这个代码: -

String host  = "Your address" ;

new Thread()
{
    run()
    {
        try {
            SocketConnection connection = (SocketConnection)Connector.open("socket://" + host + ":80");
            OutputStream out = connection.openOutputStream();
            InputStream in = connection.openInputStream();
            // Standard HTTP GET request all in text
            // Only the required Host header, no body
            String request = "GET / HTTP/1.1\r\n" +
                "Host:" + host + "\r\n" +
                "\r\n" +
                "\r\n";
            out.write(request.getBytes());
            out.flush();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int firstByte = in.read();
            if (firstByte >= 0) {
                baos.write((byte)firstByte);
                int bytesAvailable = in.available();
                while(bytesAvailable > 0) {
                    byte[] buffer = new byte[bytesAvailable];
                    in.read(buffer);
                    baos.write(buffer);
                    bytesAvailable = in.available();
                }
            }
            baos.close();
            connection.close();
            final_OP(new String(baos.toByteArray()) );
        } catch (IOException ex) {
            final_OP(ex.getMessage());
        }
    }
}.start();

public void final_OP(final String message) {
    UiApplication.getUiApplication().invokeLater(new Runnable() {
        public void run() {
            Dialog.alert("Output" + message);
        }
    });
}    
于 2012-10-22T14:53:35.940 回答