2011-07-28 7 views
2

私はプログラミングに慣れていないので、ブラックベリーIRCクライアントを構築しようとしています。サーバーに接続し、チャンネルに参加して何かを言わせましたが、メッセージを受け取るにはどうしたらいいですか?私はメッセージを待つループを作る方法を知らない、誰かが私を助けることができる?ここに私のコードは次のとおりです。あなたがサーバーに書き込むためのOutputStreamWriterの_outを持って、私はblackerry JRE上のソケットのデモサンプルを使用JavaソケットとBlackberryプログラミング

package com.rim.samples.device.socketdemo; 

import java.io.*; 
import javax.microedition.io.*; 
import net.rim.device.api.ui.*; 

public class ConnectThread extends Thread 
{ 
    private InputStream _in; 
    private OutputStreamWriter _out;   
    private SocketDemoScreen _screen; 

    // Constructor 
    public ConnectThread() 
    { 
     _screen = ((SocketDemo)UiApplication.getUiApplication()).getScreen(); 
    } 

    public void run() 
    {   
     StreamConnection connection = null;  
     String user = "Cegooow"; 
     String channel = "#oi"; 

     try 
     { 
      _screen.updateDisplay("Opening Connection..."); 
      String url = "socket://" + _screen.getHostFieldText() + ":6667" + (_screen.isDirectTCP() ? ";deviceside=true" : "");          
      connection = (StreamConnection)Connector.open(url); 
      _screen.updateDisplay("Connection open"); 

      _in = connection.openInputStream(); 

      _out = new OutputStreamWriter(connection.openOutputStream());    
      StringBuffer s = new StringBuffer(); 

      _out.write("NICK " + _screen.getNickText() + "\r\n"); 
      _out.write("USER " + user + "8 * : Java Bot\r\n"); 
      _out.write("JOIN " + channel + "\r\n"); 
      _out.write("PRIVMSG " + channel + " " + _screen.getMessageFieldText() + "\r\n"); 
      _screen.updateDisplay("Done!"); 
     } 
     catch(IOException e) 
     { 
      System.err.println(e.toString()); 
     } 
     finally 
     {    
      _screen.setThreadRunning(false); 

      try 
      {    
       _in.close();    
      } 
      catch(IOException ioe) 
      {     
      } 
      try 
      {  
       _out.close();    
      } 
      catch(IOException ioe) 
      {    
      } 
      try 
      {    
       connection.close(); 
      } 
      catch(IOException ioe) 
      {     
      } 
     } 
    } 
} 

あなたのコードに感謝

答えて

0

、着信接続_in(InputStreamが)未使用です。 BufferedReaderのを使う方が良いでしょう、実際には

// process the inputstream after writing to _out - in single threaded app 
Reader reader = new InputStreamReader(_in); 
int data; 
while((data = reader.read()) != -1){ 
    System.out.println((char) data); // do something with the char 
} 
reader.close(); 

:あなたはそこにすべての着信データを期待するべきである...私は考えることができる 最も簡単な例は次のようになります。また、チャットアプリケーションを構築する場合は、着信データを処理するための新しいスレッドと、送信データのための別のスレッドを作成すると便利です。

0

接続が

BufferedReader reader = new BufferedReader(_in); 
while(true) { 
    String line = reader.readLine(); 
    // do something... 
} 
を閉じるまで、あなたの入力ストリームリーダハンドルはあなたができるループを取得したら、