安卓基于socket的服务器通信为什么不可以 PC服务器端和android手机客户端socket通信问题
\u5b89\u5353\u57fa\u4e8esocket\u7684\u670d\u52a1\u5668\u901a\u4fe1\u4e3a\u4ec0\u4e48\u4e0d\u53ef\u4ee5\u8bd5\u4e00\u4e0b\u4e0d\u7528BufferedRead\uff0c\u800c\u662f\u76f4\u63a5\u4ecesocket\u7684InputStream\u8bfb\u6570\u636e\u3002
\u8fd9\u4e2a\u5e94\u5f53\u53ef\u4ee5\u7684
\u3000\u30001. PC\u670d\u52a1\u5668\u542f\u7528ServerSocket
\u3000\u3000\u4e24\u4e2a\u901a\u4fe1\u5b9e\u4f53\u5728\u5efa\u7acb\u865a\u62df\u94fe\u8def\u4e4b\u524d\uff0c\u9700\u8981\u6709\u4e00\u65b9\u5148\u51c6\u5907\u597d\uff0c\u4e3b\u52a8\u63a5\u53d7\u6765\u81ea\u5176\u4ed6\u901a\u4fe1\u5b9e\u4f53\u7684\u8fde\u63a5\u8bf7\u6c42\u3002
\u3000\u3000\u4f7f\u7528ServerSocket\u5bf9\u8c61\u76d1\u542c\u6765\u81ea\u5ba2\u6237\u7aef\u7684Socket\u8fde\u63a5\u3002
\u3000\u3000//\u521b\u5efaServerSocket\u5bf9\u8c61
ServerSocket ss = new ServerSocket(30000);
//\u76d1\u542c\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42
while(true){
Socket s = ss.accept();
\u2026
}
\u3000\u3000\u5982\u679c\u6ca1\u6709\u8fde\u63a5\uff0c\u5219\u5c06\u4e00\u76f4\u5904\u4e8e\u7b49\u5f85\u72b6\u6001\u3002\u5f53\u63a5\u6536\u5230\u8fde\u63a5\u8bf7\u6c42\u540e\uff0c\u83b7\u53d6\u6d88\u606f\u5230\u8f93\u5165\u6d41\uff0c\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\u3002
\u3000\u3000//\u63a5\u6536\u5ba2\u6237\u7aef\u6d88\u606f
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str;
BufferedWriter bw = new BufferedWriter(new FileWriter("D:/ApInfo"+ (i++) +".txt"));
while ((str = in.readLine()) != null) {
System.out.println(str);
bw.write(str);
bw.newLine();
}
\u3000\u30002. Android\u7ec8\u7aef\u4f7f\u7528Socket\u901a\u4fe1
\u3000\u3000\u5ba2\u6237\u7aef\u4f7f\u7528Socket\u7684\u6784\u9020\u5668\u8fde\u63a5\u670d\u52a1\u5668\uff0c\u6307\u5b9a\u670d\u52a1\u5668IP\u548c\u7aef\u53e3\u53f7\u5c31\u53ef\u4ee5\u4e86\u3002
\u3000\u3000Socket s = new Socket(\u201c192.168.1.100\u201d, 30000);
\u3000\u3000\u8fd9\u6837\u670d\u52a1\u5668\u7aef\u7684accept()\u65b9\u6cd5\u5c31\u5f97\u5230\u54cd\u5e94\uff0c\u4ece\u800c\u5411\u4e0b\u6267\u884c\uff0c\u670d\u52a1\u5668\u7aef\u548c\u5ba2\u6237\u7aef\u5c31\u5f62\u6210\u4e86\u4e00\u5bf9\u4e92\u76f8\u8fde\u63a5\u7684Socket\u3002\u518d\u8fdb\u884c\u901a\u4fe1\u65f6\u5c31\u6ca1\u6709\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u4e4b\u5206\u4e86\uff0c\u90fd\u662f\u901a\u8fc7\u8f93\u5165\u8f93\u51fa\u6d41\u8fdb\u884c\u901a\u4fe1\u3002
\u3000\u3000\u8be6\u7ec6\u6b65\u9aa4\uff1a
\u3000\u3000\u91c7\u7528Handler\u548cTimerTask\u6765\u5b9a\u65f6\u626b\u63cfAP\u4fe1\u606f\u5e76\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u7aef\u3002TimerTask\u89c4\u5b9a\u4e86\u5230\u8fbe\u6307\u5b9a\u7684\u65f6\u95f4\u6240\u8981\u8fdb\u884c\u7684\u4efb\u52a1\u3002
\u3000\u3000TimerTask task = new TimerTask(){
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
\u3000\u3000handler\u4f20\u9012message\u5185\u5bb9\uff1a
\u3000\u3000Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
// \u6267\u884c\u5b9a\u65f6\u5668\u65f6\u95f4\u5230\u4e86\u4e4b\u540e\u7531handler\u4f20\u9012\u7684\u4efb\u52a1
break;
}
super.handleMessage(msg);
}
}
\u3000\u3000\u63a5\u4e0b\u6765\u626b\u63cfAP\u4fe1\u606f\u5e76\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u7aef\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u4fdd\u5b58\u3002
\u3000\u3000WifiManager wifiManager=(WifiManager) getSystemService(WIFI_SERVICE);
wifiManager.startScan();
mWifiList = wifiManager.getScanResults();
\u3000\u3000\u7531WifiManager\u8bf4\u660e\u53ef\u77e5\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5df2\u914d\u7f6e\u7684\u7f51\u7edc\uff0c\u5f53\u524d\u8fde\u63a5\u7684\u7f51\u7edc\u53caAP\u4fe1\u606f\u7684\u626b\u63cf\u7b49\u60c5\u51b5\u3002
\u3000\u3000\u5411\u670d\u52a1\u5668\u53d1\u9001\u6d88\u606f\uff1a
\u3000\u3000socket = new Socket("192.168.1.211",30000);
//\u5411\u670d\u52a1\u5668\u7aef\u53d1\u9001\u6d88\u606f
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
out.println(message);
\u3000\u3000\u5176\u4e2dmessage\u4e3a\u83b7\u53d6\u7684AP\u4fe1\u606f\uff0c\u6d4b\u8bd5\u6536\u5230\u7684\u4fe1\u606f\u683c\u5f0f\u4e3a\uff1a
\u3000\u3000SSID: ICIS_LAB, BSSID: 1c:af:f7:9a:65:e4, capabilities: [WPA-PSK-TKIP+CCMP], level: -80,frequency: 2437
两个通信实体在建立虚拟链路之前,需要有一方先准备好,主动接受来自其他通信实体的连接请求。
使用ServerSocket对象监听来自客户端的Socket连接。
//创建ServerSocket对象
ServerSocket ss = new ServerSocket(30000);
//监听来自客户端的请求
while(true){
Socket s = ss.accept();
…
}
如果没有连接,则将一直处于等待状态。当接收到连接请求后,获取消息到输入流,并保存到文件。
//接收客户端消息
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str;
BufferedWriter bw = new BufferedWriter(new FileWriter("D:/ApInfo"+ (i++) +".txt"));
while ((str = in.readLine()) != null) {
System.out.println(str);
bw.write(str);
bw.newLine();
}
2. Android终端使用Socket通信
客户端使用Socket的构造器连接服务器,指定服务器IP和端口号就可以了。
Socket s = new Socket(“192.168.1.100”, 30000);
这样服务器端的accept()方法就得到响应,从而向下执行,服务器端和客户端就形成了一对互相连接的Socket。再进行通信时就没有服务器和客户端之分了,都是通过输入输出流进行通信。
详细步骤:
采用Handler和TimerTask来定时扫描AP信息并发送给服务器端。TimerTask规定了到达指定的时间所要进行的任务。
TimerTask task = new TimerTask(){
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
handler传递message内容:
Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
// 执行定时器时间到了之后由handler传递的任务
break;
}
super.handleMessage(msg);
}
}
接下来扫描AP信息并发送给服务器端,然后将结果保存。
WifiManager wifiManager=(WifiManager) getSystemService(WIFI_SERVICE);
wifiManager.startScan();
mWifiList = wifiManager.getScanResults();
由WifiManager说明可知,它可以用于处理已配置的网络,当前连接的网络及AP信息的扫描等情况。
向服务器发送消息:
socket = new Socket("192.168.1.211",30000);
//向服务器端发送消息
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
out.println(message);
其中message为获取的AP信息,测试收到的信息格式为:
SSID: ICIS_LAB, BSSID: 1c:af:f7:9a:65:e4, capabilities: [WPA-PSK-TKIP+CCMP], level: -80,frequency: 2437
绛旓細涓昏鐭ヨ瘑鐐瑰涓嬶細1.寮鍙戠幆澧冿紝AndroidStudio銆乪clipse.濡備綍鎼缓Android寮鍙戠幆澧冨彲浠ュ幓鐧惧害銆2.鏁版嵁缁撴瀯锛孉pp鐨勬煇浜涘姛鑳芥秹鍙婂埌鍋氱畻娉曪紝鎵浠ヨ鏈変竴瀹氱殑鏁板鍩虹 3.AndroidSDK锛屼細API鎺ュ彛寮鍙戯紝鍖呮嫭鑷寮鍙慉PI鐨勮兘鍔涘拰璋冪敤绗笁鍙慉PI鐨勭粡楠屻4.鐔熸倝tcp銆両P锛socket绛夌綉缁滃崗璁 5.濡傛灉娑夊強鍒鏈嶅姟鍣锛屼綘杩橀渶瑕佷簡瑙...
绛旓細涓嶅湪鍚岀綉娈电殑璇濓紝鐩殑鏈嶅姟鍣鍦板潃鍐欏叕缃慖P鍦板潃鍟婏紝浣嗚鍙敤鐨勭鍙f墠琛岋紝UDP TCP閮借鐨
绛旓細Hardcoder Client 绔笌 Server 绔噰鐢ㄧ殑鏄 LocalSocket 鐨閫氫俊鏂瑰紡锛岀敱浜 Hardcoder 閲囩敤 Native 瀹炵幇锛屽洜鑰屽湪 C 灞備娇鐢 Linux 鐨 socket 鎺ュ彛瀹炵幇浜嗕竴濂 LocalSocket 鏈哄埗浣滀负 Client 绔笌 Server 绔箣闂寸殑閫氫俊鏂瑰紡銆侶ardcoder 閫氫俊妗嗘灦鏈変互涓嬬壒鐐癸細Hardcoder 閫傜敤鍦烘櫙鍜屾晥鏋 Hardcoder 妗嗘灦鏈夋晥鎻愬崌浜嗗井淇...
绛旓細瀹夊崜璁惧鎯宠閫氳繃shad杩涜鐧诲綍锛屽叾瀹炴湁涓ょ鍙鐨勬柟妗堛傞鍏堬紝浣犲彲浠ラ夋嫨鍦ㄤ綘鐨勪釜浜虹數鑴戜笂鎼缓涓涓鏈嶅姟鍣锛屾墜鏈轰笌鎵嬫満涔嬮棿鐨勯氫俊浼氶氳繃杩欎釜鏈嶅姟鍣ㄤ綔涓轰腑杞傚叿浣撴楠ゅ涓嬶細1. 鍦≒C涓婅缃細鍒涘缓涓涓猄erverSocket锛岀洃鍚5648绔彛锛屼緥濡傦細serverSocket = new ServerSocket(5648);2. 寤虹珛杩炴帴锛氬湪鎵嬫満涓婏紝閫氳繃...
绛旓細鎴戞渶杩戞鍦ㄥ仛android鏂归潰鐨勯氫俊锛岀湡鐨勬兂鎶婂畬鏁寸殑浠g爜閮界粰浣狅紝鍙槸娌″姙娉曪紝鍏徃鏈哄瘑銆傘 缁欎綘鎴戠殑socket杩炴帴绫诲惂銆傘傘 package sean.UnknownHostException; import sean.Sysout; import sean.business.BusinessCenter; import sean.business.LoginManager; import sean.format.MyType; import sean.io.Reader;...
绛旓細鏈満浣犲彲浠ュ啓鎴 "127.0.0.1"鎴栬"localhost",濡傛灉鏄瀹夊崜鏈満灏辨槸"10.0.2.2"绔彛闅忎究浣犲啓锛屽彧瑕佺郴缁熶笉鍐茬獊灏辫銆
绛旓細搴旇鍙互瀹炵幇鐨勶紝涓昏鏄煡閬撳鏂瑰尯鍙
绛旓細鎴戣鐨勮繖浜涗笓涓氬悕璇嶏紝涔熻浣犱竴鏃跺崐浼氬苟涓嶈兘鐞嗚В 鍊煎緱搴嗗垢鐨勬槸锛屽綋浣犱簡瑙e埌杩欓噷锛屼綘璧风爜鑳芥兂鏄庣櫧锛屼负浠涔堟湁鐨勪汉璇达紝Android 绋嬪簭锛屼篃鍙互鍋鏈嶅姟鍣浜嗭紝鍥犱负Socket 鎻愪緵浜嗗叾浠栫绯荤粺璁块棶Android绋嬪簭鐨勮兘鍔涳紝鑳借璇锋眰璁块棶鐨勭▼搴忥紝鍙互绉颁綔鏈嶅姟绔傛兂娣卞叆浜嗚ВSocket鐨璁捐鍘熺悊銆佽璁℃湰鎰忥紝闇瑕佹繁鍘氱殑璁$畻鏈虹綉缁...
绛旓細鏈閲嶈鐨処M鍔熻兘 杩欎釜灏辩湅浣犻渶瑕佺殑鍔熻兘瑕佸瀹屽杽浜 寰堢畝鍗曠殑璇濆彲浠ョ洿鎺ョ敤socket 浣犺鍙傚姞姣旇禌鐨勮瘽澶ф涓嶄細鏄偅涔堢畝鍗曠殑 浣犲彲浠ヤ娇鐢▁mpp鍗忚 鐢ㄥ紑婧愮殑妗嗘灦鐨勮瘽 灏辩敤a *** ack( *** ack鐨android鐗) 鎼厤鐨勬湇鍔″櫒浣犵洿鎺ョ綉涓婃悳绱 xmpp *** ack鍏抽敭瀛 搴旇灏辫兘鎵惧埌 鍙嶆瑕佸仛濂戒竴涓狪M杞欢 鏄渶瑕佽姳涓瀹氱簿鍔涘拰鏃...
绛旓細杩欐槸android浠g爜,android绯荤粺涓嶅厑璁稿湪涓荤嚎绋嬩腑鎵цio鎿嶄綔鍜岀綉缁滄搷浣,鍥犳鍦╝ctivity涓墽琛屾湁鍏socket鐨io鎿嶄綔,涔熷睘浜庝笉鍚堟硶,鍙互鎶妔ocket璇诲啓鎿嶄綔鏀惧埌涓涓嚎绋嬩腑,寮傛鎵ц