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