J2ME UDP socket聊天程序编程,有用的信息就给分
最近想写一个聊天程序,就是在手机上发送一条字符串通过pc端服务器到另外一个手机上,想用socket网络编程来实现,但是有没有做过相关的项目,网上资料也很少,请各位帮帮出出注意,和思路,谢谢:-)
------解决方案--------------------
看看WTK里的networkDemo
------解决方案--------------------如果是聊天这块的话,建议你去看看XMPP协议,XMPP协议是基于Socket连接的,现在的IM(即时通讯)一般都用XMPP协议来实现。
------解决方案--------------------字符串怎么到pc上?在pc上做个web程序,通过手机访问它,相当于网上的聊天室。
------解决方案--------------------楼主好熟啊
咋这么低调哩
------解决方案--------------------去找一个J2ME的开源IM项目,Lampiro.不过里面代码我基本看不懂。
------解决方案--------------------不能做到主动push消息,除非客户端自己请求服务器才能知道是否有新消息
http://j2me.phpchinaz.cn/archives/51526
------解决方案--------------------即时通讯,以上问题都可以解决。
------解决方案--------------------我也在做这方面的研究,请加我qq:27277834 多多交流
------解决方案--------------------Server:
package demo1;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class SocketServer extends MIDlet implements CommandListener{
private ServerSocketConnection ssc = null;
private SocketConnection sc = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private TextField tfMsg = new TextField("输入聊天信息","",255,TextField.ANY);
private Command cmdSend = new Command("发送",Command.SCREEN,1);
private Form frmChat = new Form("聊天界面");
private Display display;
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
display.setCurrent(frmChat);
frmChat.addCommand(cmdSend);
frmChat.append(tfMsg);
frmChat.setCommandListener(this);
try{
ssc = (ServerSocketConnection)Connector.open("socket://:9999");
sc = (SocketConnection)ssc.acceptAndOpen();
dis = sc.openDataInputStream();
dos = sc.openDataOutputStream();
new ReceiveThread().start();
}catch(Exception ex){
ex.printStackTrace();
}
}
public void commandAction(Command c,Displayable d){
if(c==cmdSend){
try{
dos.writeUTF(tfMsg.getString());
}catch(Exception ex){}
}
}
class ReceiveThread extends Thread{
public void run(){
while(true){
try{
String msg = dis.readUTF();
frmChat.append(msg + "\n");
}catch(Exception ex){ex.printStackTrace();}
}
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
}
------解决方案--------------------Client:
package demo1;