Android uses Socket for communication
The client can usually use the Socket constructor to link to the specified server.
The following shows how to link the Android client to the server and use the server to send a message to Android:
1) server side
2) Android end
1) server side
A small problem encountered during the compilation process: java.net. BindException: Address already in use: JVM_Bind. To analyze the causes of this problem, there are generally the following:
①) Cause: the port is in use.
Solution: Change the port.
②) Cause: the variable definition location problem (I don't know if it is the problem, maybe it is, when I started to see this problem, I changed a few ports and still did not solve it, then I defined the variable outside and solved it)
Solution: define variables externally.
Package dujun. king. javaee; import java. io. outputStream; import java.net. serverSocket; import java.net. socket; public class CServer {public static void main (String [] args) {// create a ServerSocket for listening to client Socket connection requests // local IP Address: 169.254.49.31ServerSocket ss = null; socket s = null; OutputStream OS = null; // uses a loop to continuously accept requests from the client while (true) {try {System. out. println (wait for the client link ....); ss = new ServerSocket (9527); // whenever the client Socket is received The server also generates a Sockets = ss. accept (); System. out. println (the client is connected ....); OS = s. getOutputStream (); OS. write (hello, I receive a message !. GetBytes (UTF-8); // close the output stream and ETOs. close (); s. close (); ss. close ();} catch (Exception e) {e. printStackTrace ();}}}}
2) Android end
A small problem encountered during the compilation process was that (I used a real machine) could not connect to the server, and I thought it was not an IP address problem, I have changed several of them, but it is useless (including the ip address 113.132.174.158 after the Internet connection, and the ip address 169.254.49.31 when the Internet connection is unavailable ).
Solution: Add a simulator without real-machine simulation. (Use: 169.254.49.31 test successful, for example)
A) xml layout code:
B) MainActivity. java code:
Package client. dujun. king. tcp; import java. io. bufferedReader; import java. io. inputStreamReader; import java.net. socket; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. widget. editText; public class MainActivity extends Activity {EditText show; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); show = (EditText) findViewById (R. id. show); new Thread () {@ Override public void run () {try {// establish Socket socket Socket = new Socket (169.254.49.31, 9527) connected to the remote server ); // package the input stream corresponding to the Socket into BufferedReader br = new BufferedReader (new InputStreamReader (socket. getInputStream (); // perform common I/O operations String line = br. readLine (); show. setText (data from the server: + line); br. close (); socket. close ();} catch (Exception e) {e. printStackTrace ();}}}. start () ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}
C) AndroidMainiFest. xml permission setting code: