編輯:關於Android編程
在Android中通過Socket來來連接服務器,是一種比較簡單的網絡連接方式,雖然現在使用的不多,但某些場合還是很方便的,這裡總結一下基本的寫法。
客戶端:
try { Socket socket = new Socket(host, port); socket.setSoTimeout(6 * 1000); PrintWriter printWriter = new PrintWriter( socket.getOutputStream(), true); printWriter.println("socket test"); printWriter.flush(); BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream(), "GBK")); revMsg = reader.readLine(); if (TextUtils.isEmpty(revMsg)) { revMsg = "Server Error"; } printWriter.close(); reader.close(); socket.close(); return revMsg; } catch (Exception e) { e.printStackTrace(); revMsg = "Server Error"; }
import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(8888); while (true) { Socket socket = serverSocket.accept(); new ServerThread(socket); } } catch (Exception e) { e.printStackTrace(); } } }
import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; class ServerThread extends Thread { private Socket socket; private BufferedReader bufferedReader; private PrintWriter printWriter; public ServerThread(Socket socket) throws IOException { this.socket = socket; bufferedReader = new BufferedReader(new InputStreamReader( socket.getInputStream())); printWriter = new PrintWriter(socket.getOutputStream(), true); System.out.println("Client(" + getName() + ") connected..."); start(); } @Override public void run() { try { String line = bufferedReader.readLine(); if (!line.equals("exit")) { System.out.println("Client(" + getName() + ") say: " + line); WriteToTxt(line + "\r\n","/local/data.txt"); printWriter.println("Receive Success!"); } printWriter.close(); bufferedReader.close(); socket.close(); } catch (IOException e) { } } private void WriteToTxt(String str, String path) { try { FileWriter writer = new FileWriter(path, true); writer.write(str); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
Demo很簡單。
以上。
微信作為手機端通訊應用,很多人把它當做常用的通訊工具了,如果你還停留在QQ,還沒有開始使用微信的話,說明你就out了,微信功能強大,微信支付,微信叫滴滴,微
簡書App 是我很喜歡的一款軟件。今天就模仿了一下他的登錄框。先上圖:好了下面上代碼,自定義ImgEditText 繼承與EditText。重寫一些方法。package
今天開始陸續整理一下一些常規的Android常用開發實用程序。 第一季:Android播放動畫的方法示例 1. 通常動畫都是gif圖像,推薦使用easygifanimat
原文地址:Mina SSLFilter(Apahce Mina user guide Chapter11 SSL Filter)SslFilter過濾器是負責管理數據的加