www.pudn.com > j2me_QQ.rar > TalkSocket.java


import javax.microedition.lcdui.*; 
import javax.microedition.io.*; 
import javax.microedition.midlet.*; 
import java.io.*; 
public class TalkSocket extends MIDlet implements Runnable 
{ 
	Display display; 
	String serverURL=getAppProperty("MIDlet-Stock-URL"); 
	String message="短消息开始"; 
	Command exitCmd; 
	Command connectCmd; 
	Thread myThread; 
	InputStream is=null; 
	private TextBox inTextBox,OutTextBox; 
	StreamConnection con=null; 
	int data=0; 
	 
	 
	 
	 
	public TalkSocket() 
	{ 
	  display=Display.getDisplay(this); 
	  inTextBox=new TextBox("收消息:","",100000,TextField.ANY); 
	  OutTextBox=new TextBox("发消息:","",100,TextField.ANY); 
	} 
	 
	public void startApp() 
	{ 
	  display.setCurrent(OutTextBox); 
	  myThread=new Thread(this); 
	  myThread.start(); 
	} 
	 
	public void pauseApp() 
	{} 
	 
	public void destroyApp(boolean b) 
	{} 
	 
	public void run()  
	{ 
	  try 
	  { 
	    System.out.println("ccccc"); 
	    while(true) 
	    { 
	       
	         con=(StreamConnection)Connector.open(serverURL); 
	         is=con.openInputStream(); 
	         data=0; 
	         try 
	         { 
	           StringBuffer sb=new StringBuffer(); 
	           while((data=is.read())!=-1) 
	           { 
	             sb.append((char)data); 
	              
	           } 
	            message=message+"\n"+sb.toString(); 
	            OutTextBox.setString(message); 
	            display.setCurrent(OutTextBox); 
	          } 
	          catch(IOException e){}  
	   } 
	} 
        catch(IOException e){} 
      }	 
} 
/* 
   finally 
   { 
     if(is!=null) 
     { 
       is.close(); 
     } 
     if(con!=null) 
     { 
       con.close(); 
     } 
   } 
 
**/