張生工作室

          一切皆有可能

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            24 隨筆 :: 3 文章 :: 11 評論 :: 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.io.StreamConnection;
          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 Server 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("服務(wù)器");
            cmdSend=new Command("發(fā)送",Command.OK,1);
            msg=new TextField("請輸入你的信息","",255,TextField.ANY);
            frm.append(msg);
            frm.addCommand(cmdSend);
            frm.setCommandListener(this);
            Display.getDisplay(this).setCurrent(frm);
            
            
            try {
             ssc=(ServerSocketConnection)Connector.open("socket://:9999");
             sc=(SocketConnection)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 張生工作室 閱讀(365) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 武夷山市| 大荔县| 阜新市| 泉州市| 盐源县| 陆河县| 长治县| 霍城县| 岳阳县| 密云县| 富裕县| 井冈山市| 观塘区| 海阳市| 马尔康县| 潼关县| 玛纳斯县| 长春市| 北票市| 东光县| 土默特左旗| 呼和浩特市| 阜宁县| 肃宁县| 喀喇| 名山县| 焦作市| 铜山县| 五峰| 平邑县| 客服| 永城市| 东丽区| 鹰潭市| 新平| 商水县| 江口县| 丹东市| 横峰县| 甘洛县| 昂仁县|