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

📄 neteg.java

📁 midlet code to find a remote server and get the time
💻 JAVA
字号:


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.util.*;
import java.io.*;


public class NetEg extends MIDlet {

    private static Command httpTestCommand = new Command("HttpConn", Command.SCREEN, 1);
    private static Command datagramSendCommand = new Command("Send Datagram", Command.SCREEN, 1);
    private static Command datagramReceiveCommand = new Command("Receive Datagram", Command.SCREEN, 1);
    private static Command socketClientCommand = new Command("Socket Client", Command.SCREEN, 1);
    private static Command socketServerCommand = new Command("Socket Server", Command.SCREEN, 1);
	private Command commandType;

    private List list = new List("Http Test", List.IMPLICIT);

	/**
	Used to create instances of the MIDlet
	*/
	public NetEg()
	{
		CommandListener listener = initListeners();
		Display.getDisplay(this).setCurrent(list);
        list.addCommand(httpTestCommand);
        list.addCommand(datagramSendCommand);
        list.addCommand(datagramReceiveCommand);
        list.addCommand(socketClientCommand);
        list.addCommand(socketServerCommand);
        list.setCommandListener(listener);
 	}

	/**
	Used to create instances of Thread to run the actual command
	*/
	public NetEg(Command cmd)
	{
		commandType = cmd;
	}

	public void startApp()
	{
	}

	public void pauseApp()
	{
	}

	public void destroyApp(boolean unc)
	{
	}


	private void appendData(String data)
	{
		list.append(data, null);
	}

	/**
	Create a listener command handler
	*/
	private CommandListener initListeners()
    {
        return new CommandListener()
        {
            public void commandAction(Command command, Displayable displayable)
            {
				Thread t = new Thread(new CommandHandler(command));
				t.start();
            }
        };
    }

    class CommandHandler implements Runnable
    {
		private Command commandType;

		public CommandHandler(Command type)
		{
			commandType = type;
		}


		private void httpTest()
		{

			HttpConnection c = null;
			InputStream is = null;
			StringBuffer sb = new StringBuffer();
			try
			{
				c = (HttpConnection)Connector.open("http://www.gearworks.com", Connector.READ_WRITE, true);
				c.setRequestMethod(HttpConnection.GET); //default
				is = c.openInputStream(); // transition to connected!
				int ch = 0;
				for(int ccnt=0; ccnt < 150; ccnt++) //get the title.
				{
					ch = is.read();
					System.out.print((char)ch);
					if (ch == -1)
					{
						break;
					}
					sb.append((char)ch);
				}
			}
			catch (IOException x)
			{
				x.printStackTrace();
			}
			finally
			{
				try
				{
					is.close();
					c.close();
				} catch (IOException x)
				{
					x.printStackTrace();
				}
			}
			appendData(sb.toString());
		}

		private void sendDatagram()
		{
			try
			{
				DatagramConnection dgc = (DatagramConnection) Connector.open("datagram://localhost:9001");
				try
				{
					byte[] payload = "Hello from a Datagram".getBytes();
					Datagram datagram = dgc.newDatagram(payload, payload.length);
					dgc.send(datagram);
				} finally
				{
					dgc.close();
				}
			} catch (IOException x)
			{
				x.printStackTrace();
			}
		}

		private void receiveDatagram()
		{
			try
			{
				DatagramConnection dgc = (DatagramConnection) Connector.open("datagram://:9001");
				try
				{
					int size = 100;
					Datagram datagram = dgc.newDatagram(size);
					dgc.receive(datagram);
					appendData(new String(datagram.getData()).trim());
				} finally
				{
					dgc.close();
				}
			} catch (IOException x)
			{
				x.printStackTrace();
			}
		}

		private void clientSocket()
		{
			try
			{
				SocketConnection sc = (SocketConnection) Connector.open("socket://localhost:9002");
				OutputStream os = null;
				try
				{
					os = sc.openOutputStream();
					byte[] data = "Hello from a socket!".getBytes();
					os.write(data);
				} finally
				{
					sc.close();
					os.close();
				}
			} catch (IOException x)
			{
				x.printStackTrace();
			}
		}

		private void serverSocket()
		{
			try
			{
				ServerSocketConnection ssc = (ServerSocketConnection) Connector.open("socket://:9002");
				StreamConnection sc = null;
				InputStream is = null;
				try
				{
				    sc = ssc.acceptAndOpen();
					is = sc.openInputStream();
					System.out.println("connection opened!");
					int ch = 0;
					StringBuffer sb = new StringBuffer();
					while ((ch = is.read()) != -1)
					{
						sb.append((char)ch);
					}
					appendData(sb.toString());
				} finally
				{
					ssc.close();
					sc.close();
					is.close();
				}
			} catch (IOException x)
			{
				x.printStackTrace();
			}
		}

		public void run()
		{
			System.out.println("Running Command");

			if (commandType == httpTestCommand)
			{
				httpTest();
			} else if  (commandType == datagramSendCommand)
			{
				sendDatagram();
			} else if  (commandType == datagramReceiveCommand)
			{
				receiveDatagram();
			} else if (commandType == socketClientCommand)
			{
				clientSocket();
			} else if (commandType == socketServerCommand)
			{
				serverSocket();
			}
			else
			{
				throw new IllegalStateException("No command specified to run.");
			}
		}
	}
}

⌨️ 快捷键说明

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