最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2014年11月12日 09:08:21   投稿:shichen2014  
這篇文章主要介紹了python服務器與android客戶端socket通信的實現(xiàn)方法,實例形式詳細講述了Python的服務器端實現(xiàn)原理與方法,以及對應的Android客戶端實現(xiàn)方法,需要的朋友可以參考下

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

首先,服務器端使用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)#把接收到數(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改成你服務器的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");//得到服務器返回的數(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ǎng)段訪問的。

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

相關文章

最新評論

平定县| 海淀区| 许昌县| 吉木乃县| 茶陵县| 固镇县| 广饶县| 花垣县| 福州市| 牟定县| 广南县| 巴东县| 上饶市| 滨州市| 正蓝旗| 昌都县| 桦川县| 沙坪坝区| 汾阳市| 壤塘县| 抚远县| 靖江市| 克拉玛依市| 鲜城| 兴国县| 碌曲县| 和林格尔县| 温宿县| 天气| 长宁区| 靖江市| 莱阳市| 蓝山县| 惠来县| 泌阳县| 全南县| 交城县| 尖扎县| 平远县| 株洲市| 江山市|