国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python服務器與android客戶端socket通信實例

2019-11-25 18:06:00
字體:
來源:轉載
供稿:網友

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

首先,服務器端使用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)#應該顯示為: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)#把接收到數據原封不動的發送回去 
    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改成你服務器的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);//發送數據 
            out.flush(); 
            Log.i("TcpClient", "sent: " + outMsg); 
            //accept server response 
            String inMsg = in.readLine() + System.getProperty("line.separator");//得到服務器返回的數據 
            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); 
    } 
}

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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黔江区| 鄂尔多斯市| 遵义市| 莱阳市| 益阳市| 行唐县| 安仁县| 普兰县| 焦作市| 岑巩县| 昌图县| 如皋市| 霞浦县| 鲁山县| 呼伦贝尔市| 定结县| 分宜县| 太仓市| 梁河县| 木兰县| 义乌市| 筠连县| 青河县| 平远县| 湟源县| 宁都县| 九龙城区| 齐齐哈尔市| 庆城县| 正定县| 河北省| 阿拉善盟| 苏尼特左旗| 永安市| 襄垣县| 赤壁市| 连南| 昌图县| 鄂伦春自治旗| 澄城县| 乌恰县|