2012-10-22 53 views
0

我知道您可以在Blackberry中編程時打開與URL的連接,但是可以在特定端口上打開連接嗎?例如,我想將一些數據發送到服務器的回顯端口,以檢查它是否處於活動狀態並測量ping時間。有任何想法嗎 ?在Blackberry上的特定端口上打開http連接

+1

只需用一個URL像 「H T T [號碼:// blahblah: /路徑」 –

回答

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來打開連接時,可以指定端口號。

1

試試這個代碼: -

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); 
     } 
    }); 
}