Python server and android client socket communication instance, pythonandroid
This article describes how to communicate with the socket of the python server and the android client. 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:
Copy codeThe 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:
Copy codeThe 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.