【问题标题】:Java Sockets and Blackberry programmingJava 套接字和黑莓编程
【发布时间】:2011-07-28 19:20:27
【问题描述】:

我是编程新手,我正在尝试构建一个黑莓 IRC 客户端,我让它连接到服务器,加入频道并说些什么,但是我怎样才能接收消息?我不知道如何循环等待消息,有人可以帮助我吗?这是我的代码:

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

我在 blackerry jre 上使用了套接字演示示例,谢谢

【问题讨论】:

    标签: java sockets client-server irc blackberry-eclipse-plugin


    【解决方案1】:

    在您的代码中,您有一个 OutputStreamWriter _out 要写入服务器,传入连接 _in (InputStream) 未使用。你应该期待那里的任何传入数据...... 我能想到的最简单的例子是这样的:

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

    在实践中,最好使用 BufferedReader。此外,如果您正在构建一个聊天应用程序,创建一个新线程来处理传入数据并创建另一个线程来处理传出数据可能会有所帮助。

    【讨论】:

      【解决方案2】:

      获得输入流阅读器句柄后,您可以循环直到连接关闭

      BufferedReader reader = new BufferedReader(_in);
      while(true) {
          String line = reader.readLine();
          // do something...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-12
        • 2013-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多