This article describes how to implement the socket communication between the python Server and the android client. The example details how to implement the Python Server and the corresponding Android client, for more information about how to communicate with the android client socket, see the following example. Share it with you for your reference. The specific implementation method is as follows:
First, use python on the server side. The following is the python code:
The code is as follows:
# Server. py
Import socket
Def getipaddrs (hostname): # to display the IP address, just test it.
Result = socket. getaddrinfo (hostname, None, 0, socket. SOCK_STREAM)
Return [x [4] [0] for x in result]
Host = ''# if it is null, it indicates a local host.
Hostname = socket. gethostname ()
Hostip = getipaddrs (hostname)
Print ('host IP', hostip) # it should be shown as 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 ('connectedby', addr)
Data = conn. recv (1024)
If not data: break
Conn. sendall (data) # send the received data back intact
Print ('received', repr (data ))
Conn. close ()
The following is the Android code:
The code is 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 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; // shocould be same to the server port
Private void runTcpClient (){
Try {
Socket s = new Socket ("**. ** .intel.com", TCP_SERVER_PORT); // change the host to the hostname or IP address of your server.
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 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 );
}
}
In Android code, you must note that the server address must be correct and that the server can be accessed by your network segment.
I hope this article will help you with Python programming.