当前位置:编程学习 > JAVA >>

j2me实现简单socket聊天

发此贴主要想结交java爱好者  j2se j2me j2ee   都可以

在此献丑了



server端
package demo;

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 server extends MIDlet implements CommandListener{
    private Display display;
    private Form form = new Form("服务器聊天");
    private Command cmd = new Command("发送",Command.SCREEN,1);
    private TextField tf_msg = new TextField("请输入:","",50,TextField.ANY);
    
    private ServerSocketConnection ssc = null;
    private SocketConnection sc = null;
    private DataInputStream dis = null;
    private DataOutputStream dos = null;
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

    }

    protected void pauseApp() {

    }

    protected void startApp() throws MIDletStateChangeException {
        display = Display.getDisplay(this);
        display.setCurrent(form);
        form.addCommand(cmd);
        form.append(tf_msg);
        form.setCommandListener(this);
        
        try{
            ssc = (ServerSocketConnection)Connector.open("socket://:8888");
            sc = (SocketConnection)ssc.acceptAndOpen();
            dos = sc.openDataOutputStream();
            dis = sc.openDataInputStream();
            new ChatThread().start();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public void commandAction(Command c, Displayable d) {
        if(c == cmd){
            try{
                String str = tf_msg.getString();
                dos.writeUTF(str);
                form.append("server say:" + str + " ");
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }
    
    class ChatThread extends Thread{
        public void run(){
            while(true){
                try{
                    
                    String str = dis.readUTF();
                    form.append("client say:" + str + " ");
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        }
    }

}



client端

package demo;

import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.microedition.io.Connector;
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 client extends MIDlet implements CommandListener{
    private Display display;
    private Form form = new Form("服务器聊天");
    private Command cmd = new Command("发送",Command.SCREEN,1);
    private TextField tf_msg = new TextField("请输入:","",50,TextField.ANY);
    
    private SocketConnection sc = null;
    private DataInputStream dis = null;
    private DataOutputStream dos = null;
    
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

    }

    protected void pauseApp() {

    }

    protected void startApp() throws MIDletStateChangeException {
        display = Display.getDisplay(this);
        display.setCurrent(form);
        form.addCommand(cmd);
        form.append(tf_msg);
        form.setCommandListener(this);
        
        try{
            
            sc = (SocketConnection)Connector.open("socket://127.0.0.1:8888");
            dos = sc.openDataOutputStream();
            dis = sc.openDataInputStream();
            new ChatThread().start();
     &n
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,