一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - python服務(wù)器與android客戶端socket通信實例

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

2020-05-12 09:57腳本之家 Python

這篇文章主要介紹了python服務(wù)器與android客戶端socket通信的實現(xiàn)方法,實例形式詳細講述了Python的服務(wù)器端實現(xiàn)原理與方法,以及對應(yīng)的Android客戶端實現(xiàn)方法,需要的朋友可以參考下

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

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

復(fù)制代碼 代碼如下:
#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代碼:

 

復(fù)制代碼 代碼如下:
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è)計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产综合色在线视频区色吧图片 | 亚洲精品 欧美 | 免费成年网 | 亚洲精品午夜级久久久久 | 果冻传媒在线播放1 | 九色PORNY真实丨国产大胸 | 蜜桃88av| 99这里只有精品在线 | 97青草香蕉依人在线播放 | 精品国产品香蕉在线观看75 | 小sao货ji巴cao死你视频 | 精品91一区二区三区 | 日本中文字幕一区二区高清在线 | 国产精品天天在线 | 久久综合色超碰人人 | 精品国产欧美一区二区三区成人 | 海派甜心完整版在线观看 | daring国家队在线观看樱花动漫 | 九九精品热 | 欧美xxxxxbb| 成人免费在线视频 | 男人的天堂在线观看免费 | 亚洲天堂岛国片 | 久久视频这只精品99re6 | 亚洲欧美一区二区三区不卡 | 男人使劲躁女人小视频 | 小苹果日本在线观看 | 国产精品久久久久久福利 | 丁香六月色婷婷综合网 | 私人家庭影院5577 | gayrb漫画免费入口 | 14一15sexvideo日本 | 亚洲国产婷婷俺也色综合 | 久久全国免费观看视频 | 欧洲喷浆乌克兰 | 亚洲爱视频 | 不良研究所地址一 | 扒开双腿疯狂进出爽爽动态图 | 午夜爱| blackedhd 18sex| 免费大秀视频在线播放 |