张生工作室

一切皆有可能

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  24 随笔 :: 3 文章 :: 11 评论 :: 0 Trackbacks

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class GroupServer extends MIDlet implements Runnable {
 
 private ServerSocketConnection ssc=null;
 private SocketConnection sc=null;
 private Vector v=new Vector();

 protected void startApp() throws MIDletStateChangeException {

    try {
   ssc=(ServerSocketConnection)Connector.open("socket://:9999");
    
   new Thread(this).start();
   
  } catch (IOException e) {
   
   e.printStackTrace();
  }
   

 }

 protected void destroyApp(boolean b) throws MIDletStateChangeException {


 }

 protected void pauseApp() {


 }

 public void run() {
  while(true)
  {
   
   try {
    sc=(SocketConnection)ssc.acceptAndOpen();
    ChatThread chat=new ChatThread(sc);
    v.addElement(chat);
    chat.start();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  
  
 }
 class ChatThread extends Thread
 {
  private SocketConnection sc=null;
  private DataOutputStream dos=null;
  private DataInputStream dis=null;
  public ChatThread(SocketConnection sc)
  {
   this.sc=sc;
   try
   {
       dis=sc.openDataInputStream();
       dos=sc.openDataOutputStream();
   }
   catch(Exception e)
   {
    
   }
   
  }
  public void run()
  {
   while(true)
   {
    try
    {
       // dis=sc.openDataInputStream();
       // dos=sc.openDataOutputStream();
        String str=dis.readUTF();
        for(int i=0;i<v.size();i++)
        {
         ChatThread chat= (ChatThread)v.elementAt(i);
        //chat.dos.writeUTF(chat.dis.readUTF());
         chat.dos.writeUTF(str);
        // System.out.println(str+"\n");
        
        }
    }
    catch(Exception e)
    {
     
    }
   }
  }
 }

 

}



张生工作室
posted on 2008-12-02 22:40 张生工作室 阅读(304) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: