⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 communication_midlet.java

📁 用Eclipse实现的J2me程序,实现了页面的跳转等
💻 JAVA
字号:
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.OutputStreamWriter;
import java.io.*;
import java.util.*;
import java.lang.Thread;
/*
 * Created on Jul 13, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author blrc
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Communication_MIDlet extends MIDlet implements CommandListener {
    private static final Command SEND_COMMAND = new Command("Send",Command.OK,0);
    private static final Command STOP_SOCKET_COMMAND = new Command("Stop socket",Command.STOP,0);
        
    final int MAX_LENGTH = 128;
    
    private Display display;
	
	List list;
	boolean start;
	
	public Communication_MIDlet() {
		super();
		display = Display.getDisplay(this);		
		start = false;
		list = new List("Socket and Http test",List.IMPLICIT);
		list.append("start a socket connection",null);
		list.append("start a http connection",null);
		list.append("start a socket server",null);
		
		list.setCommandListener(this);		
	}

	/* (non-Javadoc)
	 * @see javax.microedition.midlet.MIDlet#startApp()
	 */
	protected void startApp() throws MIDletStateChangeException {
		if (!start)
		{
			start = true;
			display.setCurrent(list);
		}

	}

	/* (non-Javadoc)
	 * @see javax.microedition.midlet.MIDlet#pauseApp()
	 */
	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
	 */
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
	 */
	public void commandAction(Command c, Displayable d)
	{		
		if (c == List.SELECT_COMMAND)
		{
			switch (list.getSelectedIndex())
			{
			  case 0:
			  	SocketThread st = new SocketThread();
				st.start(); 	
			  	break;
			  case 1:
			  	HttpThread ht = new HttpThread();
			  	ht.start();
			  	break;
			  case 2:
			  	MySocketServer ss = new MySocketServer();
			  	ss.start();
			    break;
			}
			return;
		}


	}
}

class SocketThread extends Thread
{
	 final int MAX_LENGTH = 128;
	String info;
	byte[] buf = new byte[MAX_LENGTH];
	public SocketThread()
	{
		super();		
	}
	
	public void run()
	{
		try
		{
		   //SocketConnection sc = (SocketConnection)Connector.open("socket://135.252.6.33:8189");
		   SocketConnection sc = (SocketConnection)Connector.open("socket://localhost:7000");
           sc.setSocketOption(SocketConnection.LINGER, 5);

           InputStream is  = sc.openInputStream();
           OutputStream os = sc.openOutputStream();
        
           int i = 0; int j = 0;
           while (i >= 0)
           {     	
              while (j < 100000) j++;
              j = 0;           	  
              info = "This is No." + i + "\n";
              os.write(info.getBytes());
              int total = 0; 
              int count = is.read(buf, 0, is.available());   
              String reply = new String(buf,0,count);  
              System.out.println("reply:" + reply);   
              if (i == 10) i = 0;
           	  else i++;
            }
            is.close();
            os.close();
            sc.close();
		 }
		 catch (Exception e)
		 {
			System.out.println("Error:"+e);
		 }
		
	}
}

class HttpThread extends Thread
{
	private String url;
	final int MAX_LENGTH = 128;
    byte[] buf = new byte[MAX_LENGTH];
	public HttpThread()
	{
		super();
		url = "http://localhost:8080/helloapp/MyServlet";
	}
	public void run()
	{
		/**************************************************************
		    HttpConnection hc = null;
	        DataInputStream dis = null;	        
	       
	        try {
	        	hc = (HttpConnection)Connector.open(url);
	        	dis = new DataInputStream(hc.openInputStream());
	        	String content = "";
	        	int ic;
	        	while ((ic = dis.read()) != -1)
	        	{
	        		content += (char)ic;
	        	}
	        	System.out.println("Http receive:" + content);
	           
	        } catch (Exception e) 
			{
	           System.out.println("http throw an Exception:" + e.getMessage());
	        }   
	        *****************************************************/
		HttpConnection hc = null;
		InputStream is = null;
		OutputStream os = null;
		int rc;
		
		try
		{
			hc = (HttpConnection)Connector.open(url);
			hc.setRequestMethod(HttpConnection.POST);
			hc.setRequestProperty("Content-Type", "binary/data");
			os = hc.openOutputStream();
			os.write("SHINEAGLE".getBytes());
			os.flush();
			os.close();
			
			if ((rc = hc.getResponseCode()) != HttpConnection.HTTP_OK) 
			{
                throw new IOException("HTTP response code: " + rc);
            }
			is = hc.openInputStream();
			int count = is.read(buf, 0, is.available());
			String reply = new String(buf,0,count); 
			System.out.println("reply:" + reply);
			
			is.close();	        
	        hc.close();
		}
		catch (Exception e)
		{
			System.out.println("Error:"+e);
		}		

	}
}
class MySocketServer extends Thread
{
	public MySocketServer()
	{
		super();
	}
	
	public void run()
	{
		try
		{
	  		int i = 1;
	  		ServerSocketConnection scn = (ServerSocketConnection)Connector.open("socket://:7000");
	  		while (true)
	  		{
	  			 SocketConnection sc = (SocketConnection) scn.acceptAndOpen();
	  			 System.out.println("Spawning  " + i);
	  			 Thread t = new ThreadedEchoHandler(sc,i);
				 t.start();
				 i++;
	  			 
	  		}
		}
	  	catch (Exception e)
		{
	  		e.printStackTrace();
		}
	}
}
class ThreadedEchoHandler extends Thread
{
	private SocketConnection sc;
	final int MAX_LENGTH = 128;
    byte[] buf = new byte[MAX_LENGTH];
	private int counter;
	public ThreadedEchoHandler(SocketConnection i, int c)
	{
		sc = i;
		try
		{
			sc.setSocketOption(SocketConnection.DELAY, 0);
			sc.setSocketOption(SocketConnection.LINGER, 0);
			sc.setSocketOption(SocketConnection.KEEPALIVE, 0);
			sc.setSocketOption(SocketConnection.RCVBUF, 128);
			sc.setSocketOption(SocketConnection.SNDBUF, 128);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		counter = c;
	}

	public void run()
	{
		try
		{
			InputStream is  = sc.openInputStream();
	        OutputStreamWriter os = new OutputStreamWriter(sc.openOutputStream());

			os.write("Hello!Enter BYE to exit.");
			os.flush();

			boolean done = false;
			while (!done)
			{
				int count = is.read(buf, 0, is.available());
				String str = new String(buf,0,count);
				if (str == null)
				{
					done = true;
				}
				else
				{
					os.write("Echo (" + counter + "):" + str);
					os.flush();
					if (str.trim().equals("BYE"))
					{
						done = true;
					}
			    }
			}
			is.close();
			os.close();
			sc.close();
		 }
		 catch (Exception e)
		{
			 e.printStackTrace();
		}
	}
}



⌨️ 快捷键说明

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