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)
{
}
}
}
}
}
张生工作室