Java 套接字和 Blackberry 编程
我是编程新手,我正在尝试构建一个黑莓 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 上使用了套接字演示示例,谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的代码中,您有一个 OutputStreamWriter _out 来写入服务器,传入连接 _in (InputStream) 未使用。您应该期待那里有任何传入数据......
我能想到的最简单的例子是这样的:
在实践中,最好使用 BufferedReader。此外,如果您正在构建聊天应用程序,创建一个新线程来处理传入数据和另一个线程来处理传出数据可能会很有帮助。
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:
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.
一旦获得输入流读取器句柄,您就可以循环直到连接关闭
Once you get your input stream reader handle you can loop until connection closes