猜拳 源碼
http://www.eoeandroid.com/thread-114907-1-1.html
自己實現的Android曲線圖
http://www.eoeandroid.com/thread-207218-1-1.html
android遊戲物理引擎源碼
http://www.eoeandroid.com/thread-207445-1-1.html
socketServer類
package com.socket.server; import java.io.*;import java.net.*; import android.content.Context;import android.content.Intent; public class SocketServer { ServerSocket sever;Context context; public SocketServer(Context context, int port) {try {//執行個體化ServerSocket傳入連接埠號碼sever = new ServerSocket(port);this.context = context;} catch (IOException e) {e.printStackTrace();}} public void ControlActionAnswerMsg(String str) {final Intent intent = new Intent();intent.setAction("SOCKET_ACTION");intent.putExtra("message", str);context.sendBroadcast(intent);} public void beginListen() {//將socket監聽放入線程中,防止影響主線程操作new Thread() {public void run() {//因為是不斷監聽的過程,所以採用死迴圈while (true) {try {//這裡是做監聽操作final Socket socket = sever.accept(); BufferedReader in;try {//監聽到訊息後這裡讀取流資訊in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));//將流資訊放入PrintWriter中,然後讀出來即可PrintWriter out = new PrintWriter(socket.getOutputStream());while (!socket.isClosed()) {String str;//讀出字串str = in.readLine();out.println("Hello!world!! " + str);out.flush();if (str == null || str.equals("end"))break;System.out.println(str);ControlActionAnswerMsg(str);}socket.close();} catch (IOException e) {e.printStackTrace();}} catch (IOException e) {e.printStackTrace();}} }}.start();}}
ClientSocket類
package com.socket.pc;import java.io.*;import java.net.*;public class ClientSocket { static Socket client; public ClientSocket(String site, int port) { try { //執行個體化socket傳入ip和連接埠號碼,兩邊連接埠號碼要統一 client = new Socket(site, port); System.out.println("Client is created! site:" + site + " port:" + port); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String sendMsg(String msg) { try { // BufferedReader in = new BufferedReader(new InputStreamReader( // client.getInputStream())); //將字串轉成輸出資料流然後發送出去 PrintWriter out = new PrintWriter(client.getOutputStream()); out.println(msg); out.flush(); // return in.readLine(); } catch (IOException e) { e.printStackTrace(); } return ""; } public void closeSocket() { try { client.close(); } catch (IOException e) { e.printStackTrace(); } }}
服務端與用戶端源碼