Socket communication between Android phone client and PC

Source: Internet
Author: User

These two days do a cell phone and computer communication using the socket for network communication applet, first:




A lot of problems have been found in the process of writing the program ...

First, the computer and mobile phone IP must be in the same network segment, that is, the first three segments of IP must be the same.

The first computer and mobile phone are attached to the teacher's office wireless, but not even. Toss for a long time only to find that although all are connected to the same router, network segment is not the same. (the teacher said it should be the same, I do not know how.) Later modified the IP address of the phone to connect. about how to modify the phone IP, Baidu a bit OK.


Second, ReadLine () read data blocking problem

Finally connected, but can't read the data. After debug discovery, the code behind ReadLine is no longer executed. Internet search for a bit readline, only to find that it was a hole. About the knowledge look here

Be careful readline. Workaround: Add a newline character before each data is sent.

<span style= "FONT-SIZE:14PX;" >out.write (data + ' \ n '). GetBytes ("GBK"));</span>

Third, the PC-side character encoding format is GBK, and the mobile phone side of the format is UTF-8. garbled when sending and receiving messages.

Workaround: Specify the encoding format of the data when writing data to the stream.

<span style= "FONT-SIZE:14PX;" >writer.write (Me.getbytes ("Utf-8"));</span>

Four, this is a problem only discovered today. When I execute in a newly created thread
<span style= "FONT-SIZE:14PX;" >textview.append ("server:" + B + ' \ n ');</span>
thrownJava.lang.RuntimeException:Can ' t create handler inside thread this has a not exception.
Searching the web, the UI operation must be performed in the UI thread. If the data obtained by other threads is to change the UI interface, you must send a message to the UI thread with handler that you want the UI thread to tell.

Workaround: Use a Handler object in the main thread to process the message, and use the handler object to send the message to the main thread in the threads that you created.


Main thread:



New Thread:



Toss such a meal, finally can run normally!

Project Code:
Project Code


Socket communication between Android phone client and PC

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.