最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
當前位置: 首頁 - 科技 - 知識百科 - 正文

python服務(wù)器與android客戶端socket通信實例

來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 14:40:41
文檔

python服務(wù)器與android客戶端socket通信實例

python服務(wù)器與android客戶端socket通信實例:本文實例講述了python服務(wù)器與android客戶端socket通信的方法。分享給大家供大家參考。具體實現(xiàn)方法如下: 首先,服務(wù)器端使用python完成,下面為python代碼: 代碼如下:#server.py import socket def getipaddrs(hostname):
推薦度:
導讀python服務(wù)器與android客戶端socket通信實例:本文實例講述了python服務(wù)器與android客戶端socket通信的方法。分享給大家供大家參考。具體實現(xiàn)方法如下: 首先,服務(wù)器端使用python完成,下面為python代碼: 代碼如下:#server.py import socket def getipaddrs(hostname):

本文實例講述了python服務(wù)器與android客戶端socket通信的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

首先,服務(wù)器端使用python完成,下面為python代碼:

代碼如下:

#server.py
import socket
def getipaddrs(hostname):#只是為了顯示IP,僅僅測試一下
result = socket.getaddrinfo(hostname, None, 0, socket.SOCK_STREAM)
return [x[4][0] for x in result]

host = ''#為空代表為本地host
hostname = socket.gethostname()
hostip = getipaddrs(hostname)
print('host ip', hostip)#應(yīng)該顯示為:127.0.1.1
port = 9999 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(4)
while True:
conn, addr = s.accept()
print('Connected by', addr)
data = conn.recv(1024)
if not data: break
conn.sendall(data)#把接收到數(shù)據(jù)原封不動的發(fā)送回去
print('Received', repr(data))
conn.close()

下面是Android代碼:

代碼如下:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class TcpClient extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
runTcpClient();
finish();
}

private static final int TCP_SERVER_PORT = 9999;//should be same to the server port
private void runTcpClient() {
try {
Socket s = new Socket("**.**.intel.com", TCP_SERVER_PORT);//注意host改成你服務(wù)器的hostname或IP地址
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//send output msg
String outMsg = "TCP connecting to " + TCP_SERVER_PORT + System.getProperty("line.separator");
out.write(outMsg);//發(fā)送數(shù)據(jù)
out.flush();
Log.i("TcpClient", "sent: " + outMsg);
//accept server response
String inMsg = in.readLine() + System.getProperty("line.separator");//得到服務(wù)器返回的數(shù)據(jù)
Log.i("TcpClient", "received: " + inMsg);
//close connection
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//replace runTcpClient() at onCreate with this method if you want to run tcp client as a service
private void runTcpClientAsService() {
Intent lIntent = new Intent(this.getApplicationContext(), TcpClientService.class);
this.startService(lIntent);
}
}


安卓代碼中要注意的就是服務(wù)器的地址要寫對,而且要保證服務(wù)器是可以被你的網(wǎng)段訪問的。

希望本文所述對大家的Python程序設(shè)計有所幫助。

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

python服務(wù)器與android客戶端socket通信實例

python服務(wù)器與android客戶端socket通信實例:本文實例講述了python服務(wù)器與android客戶端socket通信的方法。分享給大家供大家參考。具體實現(xiàn)方法如下: 首先,服務(wù)器端使用python完成,下面為python代碼: 代碼如下:#server.py import socket def getipaddrs(hostname):
推薦度:
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top