Android Socket programming attempt failed, ask for help

Source: Internet
Author: User

============ 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.