Themselves in the newly opened thread creates a SOCKET
Then opened a thread receives data
Why only occasional cases in order to receive the data server sends it
Please check prawn helped write checks where there is not a problem
public SocketConnect (Context context) {
init (context);
. new Thread (runnableSocket) start ();
new Thread (runnableBlock, "TCP_Recv") start ();.
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Runnable runnableSocket = new Runnable () {
@ Override
public void run () {
try {
Log.v (TAG, "Start new Socket");
tcpSocket = new Socket (serverIp, SEVER_PORT);
Log.v (TAG, "Socket create success");
} Catch (UnknownHostException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
};
/ ** Receive data ** /
private Runnable runnableBlock = new Runnable () {
@ Override
public void run () {
while (isRun) {
if (tcpSocket == null) {
return;
}
receivePacket ();
}
}
};
/ ** Receive packets ** /
private void receivePacket () {
if (tcpSocket == null)
return;
BufferedReader reader = null;
try {
reader = new BufferedReader (new InputStreamReader (
tcpSocket.getInputStream ()));
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
String txt = null;
try {
txt = reader.readLine ();
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
if (txt! = null)
Log.v (TAG, "txt =" + txt);
}
Reply:
Log.v (TAG, "11111111111");
dataInputStream = new DataInputStream (tcpSocket.getInputStream ());
String data = dataInputStream.readLine ();
Log.v (TAG, "data" + data);
Add a log found to be parked in the 1111111
Reply:
Seems to be the problem
DataInputStreamWhat method prawn processing tcpSocket.getInputStream it
Reply:
String data = dataInputStream.readLine (); This place blocks, waiting for the server response line of text,
The text must have line breaks, otherwise the code will not move.
Recommend reading the stream of bytes. No need to consider whether there is a line break.
http://blog.csdn.net/birdsaction/article/details/7083031
No comments:
Post a Comment