Python server and Android client socket communication instance _python

Source: Internet
Author: User
Tags server port

The example in this article describes how the Python server communicates with the Android client socket. Share to everyone for your reference. The implementation methods are as follows:

First, the server side is done with Python, and here's the Python code:

Copy Code code as follows:
#server. py
Import socket
def GETIPADDRS (hostname): #只是为了显示IP, just test
result = Socket.getaddrinfo (hostname, None, 0, socket. SOCK_STREAM)
return [x[4][0] for x on 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 ()

Here's the Android code:

Copy Code code as follows:
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 the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Runtcpclient ();
Finish ();
}

private static final int tcp_server_port = 9999;//should is same to the SERVER PORT
private void Runtcpclient () {
try {
Socket s = new socket ("**.**.intel.com", tcp_server_port);/note Host changes to your server's hostname or IP address
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);//Send data
Out.flush ();
LOG.I ("TcpClient", "Sent:" + outmsg);
Accept Server Response
String inmsg = in.readline () + system.getproperty ("Line.separator");//Get the data returned by the server
LOG.I ("TcpClient", "Received:" + inmsg);
Close connection
S.close ();
catch (Unknownhostexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
Replace Runtcpclient () at onCreate and if you are want to run TCP client as a service
private void Runtcpclientasservice () {
Intent lintent = new Intent (This.getapplicationcontext (), tcpclientservice.class);
This.startservice (lintent);
}
}

The Android code should pay attention to the server's address to write right, and to ensure that the server can be accessed by your network segment.

I hope this article will help you with your Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.