2012年5月17日 星期四

Ubuntu 安裝 JAVA6

安裝Java
關閉所有視窗,同時按下Ctrl + Alt + T 打開終端畫面,
接著安裝JDK,下列的方式是安裝1.6.x版本。

Installing the JDK
下載  jdk-6uXX-linux-x64.bin
$ sudo apt-get purge openjdk*
$ chmod +x jdk-6u32-linux-x64.bin  <--將bin檔設定可執行
$ ./jdk-6u32-linux-x64.bin
$ sudo mv jdk1.6.0_32 /usr/lib/jvm/
Install new java source in system:
$ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.0_32/bin/javac 1
$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_32/bin/java 1
Choose default java:
$ sudo update-alternatives --config javac   <--設定預設Javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).

  Selection    Path                                  Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-oracle/bin/javac   1         auto mode
  1            /usr/lib/jvm/java-7-oracle/bin/javac   1         manual mode
  2            /usr/lib/jvm/jdk1.6.0_32/bin/javac     1         manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/jdk1.6.0_32/bin/javac to provide /usr/bin/javac (javac) in manual mode.

$ sudo update-alternatives --config java  <--設定預設Java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                 Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-oracle/bin/java   1         auto mode
  1            /usr/lib/jvm/java-7-oracle/bin/java   1         manual mode
  2            /usr/lib/jvm/jdk1.6.0_32/bin/java     1         manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/jdk1.6.0_32/bin/java to provide /usr/bin/java (java) in manual mode.

安裝完成後執行java -version指令看到如下訊息出現表示安裝完成。
$ java -version
java version "1.6.0_32"
Java(TM) SE Runtime Environment (build 1.6.0_32-b05)
Java HotSpot(TM) 64-Bit Server VM (build 20.7-b02, mixed mode)




參考:http://cheng-min-i-taiwan.blogspot.com/2012/05/ubuntu-1204-lts-android-sdk.html

Auto mount partitions using Storage Device Manager (pysdm)


sudo apt-get install pysdm
sudo pysdm
 select the partition you want to be automatically mounded on startup and click the "Assistant" button.



check the box next to "The file system is mounted at boot time" (it's probably already be checked),

- make sure the "Mount file system in read-only mode" is unchecked (this is automatically checked for NTFS partitions),

check the "Allow any user to mount the file system" box,

check the "Allow a user to mount and unmount the filesystem" box,


Once you're done, click OK and "Apply" (important - if you don't click the "Apply" button on the Storage Device Manager window, the settings won't be applied). Follow the same steps for each partition (be it NTFS, EXT3/4, etc.) and click "Apply" when done.


If you've messed up something, you can restore the original fstab file which we've backed up above, using this command:
sudo cp /etc/fstab.old /etc/fstab
參考:http://www.webupd8.org/2011/11/how-to-mount-partitions-automatically.html

2012年5月11日 星期五

about broadcast(C#)

接收:
Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 100);
sock.EnableBroadcast = true;
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
sock.ReceiveTimeout = 1000;
IPEndPoint iep = new IPEndPoint(IPAddress.Any/*IPAddress.Parse("192.168.1.220")*/, 8027);
sock.Bind(iep);

EndPoint ep = (EndPoint)iep;
Console.WriteLine("Ready to receive..." + sock.LocalEndPoint.ToString());

while (runTD)
{
Console.WriteLine("Waiting for data..");

try
{
byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref ep);
//int recv = sock.Receive(data);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
//dosomething
Console.WriteLine(stringData.Trim());
}
catch
{
Console.WriteLine("timeout!!");
}
}
sock.Close();


發送:
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.EnableBroadcast = true;
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);

IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 8028);

string str = tb_send.Text.ToString();
byte[] data = Encoding.ASCII.GetBytes(str);

sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 100);
sock.SendTo(data, iep1);

sock.Close();

注意:若收不到請檢察防火牆。


demo download : https://docs.google.com/open?id=0B2Mp2cW7CWo5bEo5NWp4WGNSZ1U

2012年5月10日 星期四

JSON in JAVA

JSON(JavaScript Object Notation)
json jar download : https://docs.google.com/open?id=0B2Mp2cW7CWo5V183dUpsLUVIQUU
json api doc : http://www.json.org/java/index.html
參考資料 :
http://werdna1222coldcodes.blogspot.com/2009/01/json-in-java.html

about broadcast (java、android)


(廣播) broadcast 和群播( multicast ) 兩者皆基於UDP的協議 。

廣播(broadcast)
Code寫法與單播一樣,區別在於目的地址不同。單播使用的是特定ip地址,而廣播顧名思義就是要向其他的客戶端發送消息,所以使用的是255.255.255.255(ipv4)。
JAVA:
public static final String broadip = "255.255.255.255";
public static final int broadport = 8027;
DatagramSocket socket= new DatagramSocket( broadport  );
address = InetAddress.getByName(broadip);
socket.setBroadcast(true);
socket Saddress = new InetSocketAddress(broadip, broadport);
socket .bind(Saddress);//接收時需要bind


byte[] SendBuf = InMsg.getBytes();//String InMsg為要傳送的字串
int length = InMsg.getBytes().length;
packet = new DatagramPacket(SendBuf, length,address,broadport);
socket  .send(packet);


群播(multicast)
稍微則稍微複雜一點。 Java提供了一個MulticastSocket的類來進行multicast,該類繼承於DatagramSocket。
多播地址一般為224.0.0.0 - 239.255.255.255(ipv4)。
JAVA:
InetAddress address = InetAddress.getByName("224.0.0.0");
if (!address.isMulticastAddress()) {
throw new Exception("this is not multi address");
}
packet = new DatagramPacket(byteToSend, byteToSend.length,address, 9999);
與廣播不同,它使用的是一個 群播 組。接收方需要加入這個組以告知對該組上的數據有興趣。所以接收方有一個joinGroup的操作,
JAVA:
MulticastSocket socket = new MulticastSocket();
socket.joinGroup(address);


demo download : https://docs.google.com/open?id=0B2Mp2cW7CWo5am9HSlozanZVc1k

Output To Console In Windows Forms Application



using System.Runtime.InteropServices;


public class Win32

{ /// /// Allocates a new console for current process. ///

[DllImport("kernel32.dll")]

public static extern Boolean AllocConsole(); /// /// Frees the console. ///

[DllImport("kernel32.dll")]

public static extern Boolean FreeConsole();

}




public Form1()

{

InitializeComponent(); 
Win32.AllocConsole();

}

在需要的地方插入下Code: