j2me Socket编程常用服务器和客户端编写结构

程序比较乱,望能给予参考。更复杂的写法,自己开发

服务器端代码:

public class SocketServer1 extends MIDlet {
 private ServerSocketConnection ssc;
 private Display display;
 private Form myForm;
 private Command setUpCommand;
 private boolean isConnect=true;

 public SocketServer1() {
  display = Display.getDisplay(this);
  myForm = new Form("显示客户端信息");
  setUpCommand = new Command("启动服务器", Command.OK, 1);
  //打开一个连接端口
  try {
   ssc = (ServerSocketConnection) Connector.open("socket://:5003");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 protected void startApp() throws MIDletStateChangeException {
  display.setCurrent(myForm);
  myForm.addCommand(setUpCommand);
  myForm.setCommandListener(new CommandListener() {
   public void commandAction(Command c, Displayable arg1) {
    if(c==setUpCommand){
     new ReceiveRequestThread().start();
    }
   }
  });

 }

 class ReceiveRequestThread extends Thread {
  public void run() {
   while (isConnect) {
    try {
     System.out.println("连接请求");
     SocketConnection sc = (SocketConnection) ssc
       .acceptAndOpen();
     System.out.println("请求成功");
     //启动服务器代理
     new ServerAgent(sc).start();
     
    } catch (IOException e) {
     System.out.println("断开连接");
     isConnect=false;
    }
   }
  }
 }
 class ServerAgent extends Thread{
  SocketConnection sc;
  public DataInputStream din;
  public DataOutputStream dout;
  boolean flag = false;
  public ServerAgent(SocketConnection _sc){
   
   sc=_sc;
   try {
    din=sc.openDataInputStream();
    dout=sc.openDataOutputStream();
    
    dout.writeUTF("欢迎你的连接");
   } catch (IOException e) {
    System.out.println("断开连接");
    isConnect=false;
   }
   flag=true;
   
  }
  public void run() {
   while(flag){
    try {
     String msg = din.readUTF();
     if(msg.startsWith("<#LOGIN#>")){
      myForm.append(msg.substring(9)+"\n");
     }
    } catch (IOException e) {
     System.out.println("断开连接");
    }
   }
  }
 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub

 }

 protected void pauseApp() {
  // TODO Auto-generated method stub

 }

}

客户端:

public class SocketClient1 extends MIDlet {

 private Display display;
 private Form myForm;
 private Command SendCommand;
 private TextField tfName;
 private SocketConnection sc;
 private DataOutputStream dos;
 private DataInputStream dis;
 private int count=1;

 public SocketClient1() {
  display = Display.getDisplay(this);
  myForm = new Form("显示客户端信息");
  SendCommand = new Command("发送", Command.OK, 1);
  tfName = new TextField("姓名:", "", 10, TextField.ANY);

 }

 protected void startApp() throws MIDletStateChangeException {
  display.setCurrent(myForm);
  myForm.append(tfName);
  
  myForm.addCommand(SendCommand);

  try {
   sc = (SocketConnection) Connector.open("socket://127.0.0.1:5003");
   dos = sc.openDataOutputStream();
   dis= sc.openDataInputStream();
   System.out.println("AAAAAAAAAAAAAAAAA");
   myForm.setTitle(dis.readUTF());
  } catch (IOException e) {
   e.printStackTrace();
  }
  myForm.setCommandListener(new CommandListener() {

   public void commandAction(Command c, Displayable arg1) {
    if (c == SendCommand) {
     String msg = tfName.getString();
     try {
      dos.writeUTF("<#LOGIN#>"+msg);
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }

   }
  });

 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub

 }

 protected void pauseApp() {

 }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值