cainwise 发表于 2013-1-28 19:41:41

QQDemo_mobile版本一

服务端:
package com;import javax.microedition.io.*;import java.io.*;import javax.microedition.io.ServerSocketConnection;import javax.microedition.lcdui.*;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;public class ServerSocket extends MIDlet implements Runnable, CommandListener {private Form form;private StringItem stri;private Command exitCmd,sendCmd;private TextField tf;Sends send;public ServerSocket() {form = new Form("serverSocket");stri = new StringItem("接收信息:","");tf = new TextField("发送信息:","",8000,0);exitCmd = new Command("exit",Command.EXIT,1);sendCmd = new Command("send",Command.SCREEN,1);form.append(stri);form.append(tf);form.addCommand(exitCmd);form.addCommand(sendCmd);form.setCommandListener(this);}protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}protected void pauseApp() {}protected void startApp() throws MIDletStateChangeException {Display.getDisplay(this).setCurrent(form);Thread th = new Thread(this);th.start();}public void run() {ServerSocketConnection scn = null;SocketConnection sn = null;InputStream is = null;OutputStream os = null; try {scn= (ServerSocketConnection)Connector.open("socket://:6000");sn = (SocketConnection) scn.acceptAndOpen();is = sn.openInputStream();os = sn.openOutputStream();send = new Sends(os);while(true){StringBuffer sb = new StringBuffer();int count = 0;while((count=is.read())!='\n'&&count!=-1){sb.append((char)count);}this.stri.setText(sb.toString());}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {is.close();os.close();sn.close();scn.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public void commandAction(Command cmd, Displayable dis) {if(cmd==exitCmd){try {destroyApp(false);notifyDestroyed();} catch (MIDletStateChangeException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(cmd==sendCmd){this.send.send(this.tf.getString());}}} 
客户端:
package com;
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ClinetSocket extends MIDlet implements Runnable, CommandListener {
 
 private Form form;
 private StringItem stri;
 private Command exitCmd,sendCmd;
 private TextField tf;
 Sends send;
 
 public ClinetSocket() {
  form = new Form("ClinetrSocket");
  stri = new StringItem("接收信息:","");
  tf = new TextField("发送信息:","",8000,0);
  exitCmd = new Command("exit",Command.EXIT,1);
  sendCmd = new Command("send",Command.SCREEN,1);
  form.append(stri);
  form.append(tf);
  form.addCommand(exitCmd);
  form.addCommand(sendCmd);
  form.setCommandListener(this);
 }
 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
 }
 protected void pauseApp() {
 }
 protected void startApp() throws MIDletStateChangeException {
   Display.getDisplay(this).setCurrent(form);
   Thread th = new Thread(this);
   th.start();
 }
 public void run() {
  SocketConnection sn = null;
  InputStream is = null;
  OutputStream os = null;
   try {
   sn = (SocketConnection)Connector.open("socket://localhost:6000");
   is = sn.openInputStream();
   os = sn.openOutputStream();
   
   send = new Sends(os);
   
   
   while(true){
    StringBuffer sb = new StringBuffer();
    int count = 0;
    while((count=is.read())!='\n'&&count!=-1){
     sb.append((char)count);
    }
    this.stri.setText(sb.toString());
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
  
   try {
    is.close();
    os.close();
    sn.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
 public void commandAction(Command cmd, Displayable dis) {
   if(cmd==exitCmd){
    try {
     destroyApp(false);
     notifyDestroyed();
    } catch (MIDletStateChangeException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   
   if(cmd==sendCmd){
    this.send.send(this.tf.getString());
   }
 }
}
发送处理:
package com;
import java.io.IOException;
import java.io.OutputStream;
public class Sends extends Thread {
 private OutputStream os;
 private String message;
 public  Sends(OutputStream os){
  this.os=os;
  this.start();
 }
 
 public synchronized void send(String msg){
  this.message=msg;
  notify();
 }
 
 public synchronized void run() {
  while(true){
   if(this.message==null){
    try {
     this.wait();
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   
   if(this.message==null){
    break;
   }
   
   try {
    os.write(this.message.getBytes());
    os.write("\r\n".getBytes());
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
   this.message=null;
  }
 }
 
}
欢迎大家来加添新功能!!!!
页: [1]
查看完整版本: QQDemo_mobile版本一