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()
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);
}
}
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程序设计有所帮助。
上一篇: 常见SEO中十大难题解决方法
下一篇: 《数据挖掘:R语言实战》神经网络完整代码
推荐阅读
-
python网络编程之UDP通信实例(含服务器端、客户端、UDP广播例子)
-
C#使用Socket实现服务器与多个客户端通信(简单的聊天系统)
-
Python警察与小偷的实现之一客户端与服务端通信实例
-
java Tcp通信客户端与服务器端实例
-
android基于socket的局域网内服务器与客户端加密通信
-
python网络编程之UDP通信实例(含服务器端、客户端、UDP广播例子)
-
Android BLE与终端通信(四)——实现服务器与客户端即时通讯功能
-
网络套接字socket,利用UDP协议实现服务器与客户端通信
-
Android通过Socket与服务器之间进行通信的示例
-
Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)