關於Android應用與Framework的socket通訊,相信關心這個問題的朋友們已經看過《android使用socket使底層和framework通訊》這篇文章,美中不足的是做者只貼出一些關鍵的代碼片斷而並無放出源碼。我這裏仍是以一個能實際運行的例子爲基礎來說,這樣也方便你們學習。 html
首先看一下效果,以下圖。我填寫姓名"Potter",選擇性別"Mr"而後點擊發送,底層socket收到消息後將消息直接返回給我,我將返回的結果(Mr.Potter)直接顯示在Result。 java
編寫socket服務端代碼,生成可執行腳本htfsk。 android
#define SOCKET_NAME "htfsk" #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/wait.h> #include <sys/un.h> #include <cutils/sockets.h> #include <utils/Log.h> #include <android/log.h> int main(){ char log[200]; int connect_number = 6; int fdListen = -1, new_fd = -1; int ret; struct sockaddr_un peeraddr; socklen_t socklen = sizeof (peeraddr); int numbytes ; char buff[256]; //這一步很關鍵,就是獲取init.rc中配置的名爲 "htfsk" 的socket fdListen = android_get_control_socket(SOCKET_NAME); if (fdListen < 0) { sprintf(log,"Failed to get socket '" SOCKET_NAME "' errno:%d", errno); __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log); exit(-1); } //開始監聽 ret = listen(fdListen, connect_number); sprintf(log,"Listen result %d",ret); __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log); if (ret < 0) { perror("listen"); exit(-1); } //等待Socket客戶端發啓鏈接請求 new_fd = accept(fdListen, (struct sockaddr *) &peeraddr, &socklen); sprintf(log,"Accept_fd %d",new_fd); __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log); if (new_fd < 0 ) { sprintf(log,"%d",errno); __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log); perror("accept error"); exit(-1); } while(1){ //循環等待Socket客戶端發來消息 __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI","Waiting for receive"); if((numbytes = recv(new_fd,buff,sizeof(buff),0))==-1){ sprintf(log,"%d",errno); __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log); perror("recv"); continue; } //發送消息回執給Socket客戶端 if(send(new_fd,buff,strlen(buff),0)==-1) { perror("send"); close(new_fd); exit(0); } } close(new_fd); close(fdListen); return 0; }
三、編寫客戶端java代碼。核心代碼以下: 異步
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import android.net.LocalSocket; import android.net.LocalSocketAddress; import android.util.Log; /** * Socket客戶端 * * @author lai_zs * @date:2012-3-17 下午12:15:09 */ public class SocketClient { private final String SOCKET_NAME = "htfsk"; private LocalSocket client; private LocalSocketAddress address; private boolean isConnected = false; private int connetTime = 1; public SocketClient() { client = new LocalSocket(); address = new LocalSocketAddress(SOCKET_NAME, LocalSocketAddress.Namespace.RESERVED); new ConnectSocketThread().start(); } /** * 發送消息 * @param msg * @return 返回Socket服務端的消息回執 */ public String sendMsg(String msg) { if (!isConnected) { return "Connect fail"; } 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 "Nothing return"; } /** * 異步鏈接Socket,若是鏈接不上會嘗試重複鏈接十次 * * @author Administrator * */ private class ConnectSocketThread extends Thread { @Override public void run() { while (!isConnected && connetTime <= 10) { try { sleep(1000); Log.i("SocketClient","Try to connect socket;ConnectTime:"+connetTime); client.connect(address); isConnected = true; } catch (Exception e) { connetTime++; isConnected = false; Log.i("SocketClient","Connect fail"); } } } } /** * 關閉Socket */ public void closeSocket() { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } }
其它相關文章鏈接彙總: socket
android native c java進行本地socket通訊
http://blog.csdn.net/weidawei0609/article/details/7327626
android使用socket使底層和framework通訊
http://fanwei51880.blog.163.com/blog/static/32406740201011150240981/
Android應用與framework的socket通訊實例 -- 推薦
http://blog.csdn.net/goleftgoright/article/details/7406292
【android】Socket簡單用法
http://www.cnblogs.com/harrisonpc/archive/2011/03/31/2001565.html ide