张生工作室

一切皆有可能

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

2008年12月1日 #

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 @ 2008-12-02 22:40 张生工作室 阅读(304) | 评论 (0)编辑 收藏

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("发送",Command.OK,1);
  msg=new TextField("请输入你的信息","",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 @ 2008-12-01 20:28 张生工作室 阅读(238) | 评论 (0)编辑 收藏

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("服务器");
  cmdSend=new Command("发送",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 @ 2008-12-01 20:28 张生工作室 阅读(357) | 评论 (0)编辑 收藏