Tcpserver.java
The server's two sockets are not closed: S,ss. Two are open all the time, then the data is displayed.
classtcpserver{ Public Static voidMain (string[] args)throwsioexception{//set up a service-side socket service and listen for portsServerSocket SS =NewServerSocket (30000);//using loops to continuously accept requests from clients while(true) { //Get the Client object (content, port, IP attribute) from the link through the Accept methodSocket s =ss.accept (); /*Receive phone data*/ //IP:String IP =s.getinetaddress (). gethostaddress (); SYSTEM.OUT.PRINTLN (IP+"... Connection Successful " ); //content: Gets the data sent by the client, then uses the client object sInputStream in =S.getinputstream (); byte[] buf =New byte[1024]; intlen=In.read (BUF); String content=NewString (buf,0, Len); System.out.println ("Content:" +content); /*send back phone data*/outputstream OS=S.getoutputstream (); Os.write ("Welcome back to learn Android, you have received the blessing of bubbles!" \ n ". GetBytes ("Utf-8")); //s.close ();//shut down the client, the server can control the customer//ss.close ();//close the server, optional operation }}}View Code
Android Backend Code:
Packagecom.simpleclient;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream;ImportJava.io.OutputStreamWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.EditText; Public classMainactivityextendsActivity {EditText show; String content; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Show=(EditText) Findviewbyid (r.id.show); NewThread () {@Override Public voidrun () {Try { //establishing a socket to connect to a remote serverSocket socket =NewSocket ("192.168.1.108", 30000);//①String linewrite = "tcp:mobilephone/3.1415926"; //sending data to the serverBufferedWriter Bufwriter =NewBufferedWriter (NewOutputStreamWriter (Socket.getoutputstream ())); Bufwriter.write (linewrite);//sending data to the serverBufwriter.newline (); Bufwriter.flush (); //wraps the input stream corresponding to the socket into a bufferedreaderBufferedReader br =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); //perform normal IO operationsString line =Br.readline (); Show.settext ("Read data from the server:" +Line ); //close the input stream, socketBr.close (); Socket.close (); } Catch(IOException e) {e.printstacktrace (); }}}.start (); } }
Front Code:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical " android:layout_width=" Fill_parent " Android:layout_height= "Fill_parent" > <EditText android:id= "@+id/show" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" Android:cursorvisible= "false" android:editable= "false" android:ems= "10" > <requestfocus/> </EditText> </LinearLayout>
View Code
Permission code:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.simpleclient"Android:versioncode= "1"Android:versionname= "1.0" > <uses-SDK Android:minsdkversion= "14"android:targetsdkversion= "/>" <Application Android:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <Activity Android:name=". Mainactivity "Android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MA In "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter& Gt </activity> </application></manifest>
View Code
In general, the implementation of the server and client-to-one effect, but Chinese garbled phenomenon, and not rigorous, no class package
Android TCP Communication