張生工作室

          一切皆有可能

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            24 隨筆 :: 3 文章 :: 11 評(píng)論 :: 0 Trackbacks

          import java.io.DataInputStream;
          import java.io.DataOutputStream;
          import java.io.IOException;
          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.StringItem;
          import javax.microedition.lcdui.TextField;
          import javax.microedition.midlet.MIDlet;
          import javax.microedition.midlet.MIDletStateChangeException;


          public class Client extends MIDlet  implements CommandListener {
           
           

           private DataInputStream dis=null;
           private DataOutputStream dos=null;
           private ServerSocketConnection ssc=null;
           //private StreamConnection sc=null;
           private SocketConnection sc=null;
           private TextField msg=null;
           private Form frm=null;
           private Command cmdSend=null;
           private StringItem imsg=null;
           
           protected void startApp() throws MIDletStateChangeException {
            
            frm=new Form("客戶端");
            cmdSend=new Command("發(fā)送",Command.OK,1);
            msg=new TextField("請(qǐng)輸入你的信息","",255,TextField.ANY);
            frm.append(msg);
            frm.addCommand(cmdSend);
            frm.setCommandListener(this);
            Display.getDisplay(this).setCurrent(frm);
            
            
            try {
             sc=(SocketConnection)Connector.open("socket://127.0.0.1:9999");
             //sc=ssc.acceptAndOpen();
             dis=sc.openDataInputStream();
             dos=sc.openDataOutputStream();
             Chat chat=new Chat();
             chat.start();
             
            } catch (IOException e) {
            
             e.printStackTrace();
            }
            

           }
           

           public void commandAction(Command c, Displayable d) {
            if(c==cmdSend)
            {
             try {
              dos.writeUTF(msg.getString()+"\n");
             } catch (IOException e) {
              // TODO Auto-generated catch block
              System.out.println(e.getMessage()+"\n");
             }
             msg.setString("");
            }
            
            
           }

           protected void destroyApp(boolean b) throws MIDletStateChangeException {
            

           }

           protected void pauseApp() {
           

           }
           
           class Chat extends Thread
           {
            public void run()
            {
             while(true)
             {
              
               try {
                imsg=new StringItem(dis.readUTF(),"");
                frm.insert(1, imsg);
              
             } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }
             }
             
             
            }
           }

           
           

           

          }



          張生工作室
          posted on 2008-12-01 20:28 張生工作室 閱讀(251) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 麻江县| 靖江市| 揭西县| 柞水县| 凤凰县| 乌海市| 竹山县| 绍兴县| 黑水县| 襄汾县| 黎川县| 黎平县| 桂平市| 洛隆县| 石泉县| 开平市| 宁阳县| 沁阳市| 黑河市| 福建省| 饶阳县| 洛川县| 延安市| 巨野县| 方正县| 扎囊县| 高密市| 乌鲁木齐市| 武清区| 黄龙县| 长岭县| 佛坪县| 静安区| 临颍县| 易门县| 德令哈市| 土默特右旗| 安龙县| 桑日县| 临猗县| 罗源县|