2

i'm new to programming and I'm trying to build a blackberry IRC Client, I made it connect to a server, join a channel and say something, but how can I receive messages ? I don't know how to make a loop to wait for messages, can somebody help me ? here is my code:

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

I used the sockets demo sample on blackerry jre, thanks

2 Answers2

0

In your code you have an OutputStreamWriter _out to write to the Server, the incoming connection _in (InputStream) is unused. You should expect any incoming data there... The simplest example I can think of would be like this:

// 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();

In practice, it would be better to use a BufferedReader. Also, if you are building a chat application it might be beneficial to create a new thread to process incoming data and another thread for outgoing data.

Drupad Panchal
  • 383
  • 3
  • 10
0

Once you get your input stream reader handle you can loop until connection closes

BufferedReader reader = new BufferedReader(_in);
while(true) {
    String line = reader.readLine();
    // do something...
}
Sid Malani
  • 2,078
  • 1
  • 13
  • 13