Your First Groovy
Your First Groovy //hello.groovy println “hello, world” for (arg in this.args ) { println “Argument:” + arg; } // this is a comment /* a block comment, commenting out an alternative to above: this.args.each{ arg -> println “hello, ${arg}”} */ To run it from command line groovy hello.groovy MyName yourName HisName Overview Groovy classes compile […]
install Groovy in RedHat as4
官方网是这样写的:1,first, Download a binary distribution of Groovy and unpack it into some file on your local file system 2,set your GROOVY_HOME environment variable to the directory you unpacked the distribution 3,add GROOVY_HOME/bin to your PATH environment variable 4,set your JAVA_HOME environment variable to point to your JDK. On OS X this is /Library/Java/Home, on other […]
mysqlbinlog 資料庫處理二進制日誌檔案的實用工具
當然,系統剛弄好是沒有開啟 mysqlbinlog 的功能,至少在 CentOS4.4 上面我還要去開啟,不過 ubuntu 系統預設就已經開啟了,不過沒關係,只要利用下面方法就可以達到了 修改 my.cnf [ CentOS: /etc/my.cnf Ubuntu: /etc/mysql/my.cnf ],加入下面語法 # Replication Master Server (default)# binary logging is required for replicationlog-BIN=mysql-BIN 不過話說當你開啟這個功能之後,你會發現在 /var/lib/mysql/ 底下多出很多檔案 -rw-rw—- 1 mysql mysql 33164904 1月 17 15:44 mysql-bin.000001-rw-rw—- 1 mysql mysql 4007 1月 17 15:50 mysql-bin.000002-rw-rw—- 1 mysql mysql 70288989 1月 29 22:38 […]
Timer Server
import java.io.InterruptedIOException;import javax.microedition.io.Connector;import javax.microedition.io.Datagram;import javax.microedition.io.DatagramConnection;import javax.microedition.lcdui.Alert;import javax.microedition.lcdui.AlertType;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.StringItem;import javax.microedition.lcdui.TextField;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;public class TimeMIDlet extends MIDlet implements CommandListener, Runnable { private Display display; private Form addressForm; private Form connectForm; private Form displayForm; private Command backCommand; private Command exitCommand; private Command okCommand; private StringItem messageLabel; private TextField serverName; protected void startApp() throws MIDletStateChangeException { if (display == null) { initialize(); display.setCurrent(addressForm); } } protected void pauseApp() { } protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { } public void commandAction(Command cmd, Displayable d) { if (cmd == okCommand) { Thread t = new Thread(this); t.start(); } else if (cmd == backCommand) { display.setCurrent(addressForm); } else if (cmd == exitCommand) { try { destroyApp(true); } catch (MIDletStateChangeException ex) { } notifyDestroyed(); } } public void run() { DatagramConnection conn = null; display.setCurrent(connectForm); try { // Build the name string for the Connector open method String server = serverName.getString(); String name = “datagram://” + server + “:” + 13; conn = (DatagramConnection)Connector.open(name, Connector.READ_WRITE, false); // Build and send an empty datagram Datagram dg = conn.newDatagram(10); dg.setData(“Hello”.getBytes(), 0, 5); conn.send(dg); // Receive the reply Datagram rdg = conn.newDatagram(512); conn.receive(rdg); messageLabel.setText(new String(rdg.getData(), 0, rdg.getLength())); display.setCurrent(displayForm); } catch (InterruptedIOException iex) { display.callSerially(new Runnable() { public void run() { Alert alert = new Alert(“No Reply”, “No reply was received.\n” + “Please check the server address and try again.”, null, AlertType.ERROR); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert, addressForm); } }); return; } catch (Exception ex) { display.callSerially(new Runnable() { public void run() { Alert alert = new Alert(“Invalid Address”, “The supplied address is invalid\n” + “Please correct it and try again.”, null, AlertType.ERROR); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert, addressForm); } }); return; } catch (Error err) { System.out.println(err); err.printStackTrace(); } } private void initialize() { display = Display.getDisplay(this); // Commands exitCommand = new Command(“Exit”, Command.EXIT, 0); okCommand = new Command(“OK”, Command.OK, 0); backCommand = new Command(“Back”, Command.BACK, 0); // The address form addressForm = new Form(“Network Time”); serverName = new TextField(“Time Server name:”, “tock.usno.navy.mil”, 256, TextField.ANY); addressForm.append(serverName); addressForm.addCommand(okCommand); addressForm.addCommand(exitCommand); addressForm.setCommandListener(this); // The connect form connectForm = new Form(“Sending”); messageLabel = new StringItem(null, “Sending the datagram…\nPlease wait.”); connectForm.append(messageLabel); connectForm.addCommand(backCommand); connectForm.setCommandListener(this); // The display form displayForm = new Form(“Server Reply”); messageLabel = new StringItem(null, null); displayForm.append(messageLabel); displayForm.addCommand(backCommand); displayForm.setCommandListener(this); }}
J2ME Datagram Receiver
import java.io.IOException;import javax.microedition.io.Connector;import javax.microedition.io.Datagram;import javax.microedition.io.DatagramConnection;public class DatagramReceiver { public static void main(String[] args) { if (args.length != 1) { System.out.println(“Usage: DatagramReceiver port”); System.exit(1); } try { DatagramConnection receiver = (DatagramConnection)Connector.open(“datagram://:” + args[0]); byte[] buffer = new byte[256]; Datagram dgram = receiver.newDatagram(buffer, buffer.length); for ($$) { dgram.setLength(buffer.length); receiver.receive(dgram); int length = dgram.getLength(); System.out.println(“Datagram received. Length is ” + length); // Show the content of the datagram. for (int i = 0; i < length; i++) { System.out.print(buffer[i] + ” “); } System.out.println(); // Send it back… receiver.send(dgram); } } catch (IOException ex) { System.out.println(“IOException: ” + ex); } }}
J2ME DatagramSender
import java.io.IOException;import javax.microedition.io.Connector;import javax.microedition.io.Datagram;import javax.microedition.io.DatagramConnection;public class DatagramSender { public static void main(String[] args) { if (args.length != 2) { System.out.println(“Usage: DatagramSender port length”); System.exit(1); } try { DatagramConnection sender = (DatagramConnection)Connector.open(“datagram://localhost:” + args[0]); int length = Integer.parseInt(args[1]); byte[] buffer = new byte[length]; for (int i = 0; i < length; i++) { buffer[i] = (byte)(‘0’ + (i % 10)); } Datagram dgram = sender.newDatagram(buffer, buffer.length); sender.send(dgram); // Wait for the packet to be returned for (int i = 0; i < length; i++) { buffer[i] = (byte)0; } sender.receive(dgram); length = dgram.getLength(); System.out.println(“Received return packet, length is ” + length); // Show the content of the datagram. for (int i = 0; i < length; i++) { System.out.print(buffer[i] + ” “); } System.out.println(); } catch (IOException ex) { System.out.println(“IOException: ” + ex); } }}
Demonstrates the functionality of DatagramConnection framework.
/*** Chapter 5 Sample Code for Datagram functionality ***/import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.io.*;import java.util.*;public class DatagramTest extends MIDlet { // Port 9001 is used for datagram communication static final int receiveport = 9001; Receive receiveThread = new Receive(); public DatagramTest() { } public void startApp() { // Start the listening thread receiveThread.start(); // Send message Hello World! sendMessage(“Hello World!”); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } // This function sends a datagram message on port 9001. void sendMessage(String msg) { String destAddr = “datagram://localhost:” + receiveport; DatagramConnection dc = null; Datagram dgram; byte[] bMsg = msg.getBytes(); try { dc = (DatagramConnection)Connector.open(destAddr); // Create a datagram socket and send dgram= dc.newDatagram(bMsg,bMsg.length,destAddr); dc.send(dgram); System.out.println(“Sending Packet:” + msg); dc.close(); } catch (Exception e) { System.out.println(“Exception Connecting: ” + e.getMessage()); } finally { if (dc != null) { try { dc.close(); } catch (Exception e) { System.out.println(“Exception Closing: ” + e.getMessage()); } } } } // This function is a listener. It waits to receive datagram packets on 9001 port class Receive extends Thread { public void run() { doReceive(); } void doReceive() { DatagramConnection dc = null; Datagram dgram; try { // Open Server side datagram connection dc = (DatagramConnection)Connector.open(“datagram://:”+receiveport); String receivedMsg; while (true) { dgram = dc.newDatagram(dc.getMaximumLength()); try { dc.receive(dgram); } catch (Exception e) { System.out.println(“Exception in receiving message:” + e.getMessage()); } receivedMsg = new String(dgram.getData(), 0,dgram.getLength()); System.out.println(“Received Message: ” + receivedMsg); try { Thread.sleep(500); } catch (Exception e) { System.out.println(“Exception doReceive(): ” + e.getMessage()); } } } catch (Exception e) { System.out.println(“Exception doReceive(): ” + e.getMessage()); } finally { if (dc != null) { try { dc.close(); } catch (Exception e) { System.out.println(“Exception Closing: ” + e.getMessage()); } } } } } }
j2me:Socket connection
/*J2ME: The Complete ReferenceJames KeoghPublisher: McGraw-HillISBN 0072227109*/// jad file (Please verify the jar size first)/*MIDlet-Name: socketconnectionMIDlet-Version: 1.0MIDlet-Vendor: MyCompanyMIDlet-Jar-URL: socketconnection.jarMIDlet-1: socketconnection, , socketconnectionMicroEdition-Configuration: CLDC-1.0MicroEdition-Profile: MIDP-1.0MIDlet-JAR-SIZE: 100*/import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.io.*;import javax.microedition.io.*;public class socketconnection extends MIDlet implements CommandListener { private Command exit, start; private Display display; private Form form; public socketconnection () { display = Display.getDisplay(this); exit = new Command(“Exit”, Command.EXIT, 1); start = new Command(“Start”, Command.EXIT, 1); form = new Form(“Read Write Socket”); form.addCommand(exit); form.addCommand(start); form.setCommandListener(this); } public void startApp() throws MIDletStateChangeException { display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command command, Displayable displayable) { if (command == exit) { destroyApp(false); notifyDestroyed(); } else if (command == start) { try { StreamConnection connection = (StreamConnection) Connector.open(“socket://www.myserver.com:80”); PrintStream output = new PrintStream(connection.openOutputStream() ); output.println( “GET /my.html HTTP/0.9\n\n” ); output.flush(); InputStream in = connection.openInputStream(); int ch; while( ( ch = in.read() ) != –1 ) { System.out.print( (char) ch ); } in.close(); output.close(); connection.close(); } catch( ConnectionNotFoundException error ) { Alert alert = new Alert( “Error”, “Cannot access socket.”, null, null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.ERROR); display.setCurrent(alert); } catch( IOException error ) { Alert alert = new Alert(“Error”, error.toString(), null, null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.ERROR); display.setCurrent(alert); } } }}
Multi-Player MIDP Game Programming
Multi-Player MIDP Game Programming This tutorial explains how to write multi-player games for MIDP phones. It describes the technologies that are available to support multi-player games, and shows what kinds of games are possible with these technologies. Click here to view “Multi-Player MIDP Game Programming”
Developing Applications with the Java APIs for Bluetooth (JSR-82)
Developing Applications with the Java APIs for Bluetooth (JSR-82) This paper covers the Java API for Bluetooth (JSR-82) with respect to Sony Ericsson devices. It starts by introducing the Bluetooth technology, followed by the Java APIs for Bluetooth, and how to use them. Currently, these APIs are currently available in the Sony Ericsson P900/P908 handsets. […]