python服务器与android客户端socket通信实例

yipeiwu_com4年前服务器

本文实例讲述了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程序设计有所帮助。

相关文章

PHP编写文件多服务器同步程序

本文实例为大家分享了PHP文件多服务器同步工具,具体内容如下 <?php header('Content-type:text/html;charset=utf-8');...

用Python实现一个简单的多线程TCP服务器的教程

用Python实现一个简单的多线程TCP服务器的教程

 最近看《python核心编程》,书中实现了一个简单的1对1的TCPserver,但是在实际使用中1对1的形势明显是不行的,所以研究了一下如何在server端通过启动不同的线程...

PHP实现微信图片上传到服务器的方法示例

本文实例讲述了PHP实现微信图片上传到服务器的方法。分享给大家供大家参考,具体如下: $pic_img=trim( $postObj->PicUrl); if($type=="...

php将远程图片保存到本地服务器的实现代码

php如何将远程图片本地化,本文分享了实现代码 <?php //站点根目录 $cfg_basedir = dirname(__FILE__); //停建目录属性...

微信公众平台开发-微信服务器IP接口实例(含源码)

微信公众平台开发-微信服务器IP接口实例(含源码)

学习了access_token的获取及应用后,正式的使用access_token调用下其他微信公众平台的接口,加深认识与使用方法。 一、获取微信服务器IP地址实例 (一)接口介绍 如果公...