============ Problem Description ============
Server side: Add 1 to the int from the client and pass it back.
public class Server extends thread{@Overridepublic void Run () {//TODO auto-generated method Stubtry{serversocket Serverso Cket=new ServerSocket (10000); System.out.println ("Start Server"); while (true) {Socket socket=serversocket.accept (); System.out.println ("Get Socket"); int i=socket.getinputstream (). read (); I++;socket.getoutputstream (). write (i); Socket.close ();}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} public static void Main (String args[]) {new Server (). Start ();}}
Client: The activity layout, a button, pressed on to connect the server and send an int number past, a textview to show the number of the received plus 1
textview= (TextView) Findviewbyid (r.id.tv); button= (Button) Findviewbyid (R.ID.BTN); Button.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubtry{socket socket=new Socket ("113.76.162.91", 10000); System.out.println ("Build Socket"); Socket.getoutputstream (). Write (12); System.out.println ("Build Socket"); int i=socket.getinputstream (). read (); Socket.close (); Textview.settext ( String.valueof (i));} catch (Unknownhostexception e) {//TODO auto-generated catch BlockSystem.out.println ("Unknow Host");} catch (IOException e) {//TODO auto-generated catch blockSystem.out.println ("IO Wrong");}});
If my code does not involve Android, that is, direct 2 *.java can run normally, my computer's IP address is not mistaken, Android also added the Internet access:
<uses-permission android:name= "Android.permission.INTERNET" ></uses-permission>
Just don't know how to shoot, that IP address I tried the 127.0.0.1 and 10.0.2.2, I used the real machine debugging
============ Solution 1============
Your client has a problem and cannot update textview.settext (string.valueof (i)) within Onclicklistener;
============ Solution 2============
Socket in the sub-thread.
Android Socket programming attempt failed, ask for help