mainform.java

来自「实现客户端和服务器之间的收发短信。可以有多个用户登陆。比较简单」· Java 代码 · 共 78 行

JAVA
78
字号
package cardserver;

/**
 * <p>Title: CardServer</p>
 *
 * <p>Description: lizhenpeng</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: LP&P</p>
 *
 * @author lipeng
 * @version 1.0
 */
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.util.*;

public class MainForm
  extends Form implements CommandListener,Runnable
{
  ServerSocketConnection server;
  SocketConnection clientSocket;
  Vector userList = new Vector();
  private Thread thread = null;
  boolean isRunning = false;
  static public MainForm inst;
  public MainForm()
  {
    super("服务器");
    this.append("服务器正在运行");
    try
    {
      server=(ServerSocketConnection)Connector.open("socket://:8070");
      isRunning=true;
      thread=new Thread(this);
      thread.start();
    }
    catch(Exception e)
    {
    }
    inst = this;
  }

  private void init()
    throws Exception
  {
    setCommandListener(this);
    addCommand(new Command("Exit",Command.EXIT,1));
  }

  public void commandAction(Command command,Displayable displayable)
  {
    if(command.getCommandType()==Command.EXIT)
    {
      CardServerMIDlet.quitApp();
    }
  }
  public void run()
  {
    while(isRunning)
    {
      try
      {
        clientSocket=(SocketConnection)server.acceptAndOpen();
        ClientConnection client = new ClientConnection(clientSocket);
        userList.addElement(client);
        new Thread(client).start();
      }
      catch(Exception e)
      {

      }
    }
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?