欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-12 19:06:14
本文实例讲述了python服务器与android客户端socket通信的方法。分享给大家供大家参考。具体实现方法如下: 首先,服务器端使用python完成,下面为pyth...

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