Home > Mobile >  AndroidStudio using socket communication receives commands to the upper machine is null PC need to s
AndroidStudio using socket communication receives commands to the upper machine is null PC need to s

Time:09-29

Problem description:
App to send data back and forth, and the upper machine of the same local area net TCP socket communication, (PC code process function, etc. There is no problem, is a standard,)
Now in one link, the app sends a packet, PC will give a reply, but received in app side is null (log Attempt to read from a null array) will not according to the command into the next phase of the operation, I use the Internet instead of upper machine debugging assistant, see app received artificial hair again after the response, can be received, but PC can't be repeated twice, want to consult everybody a great god what's the problem, how to solve,,, for a long time, ideas are welcome to contact qq: 1206814310

/* * *
* to get the data thread
*/
Public Runnable Runnable=new Runnable () {
@ Override
Public void the run () {
Try {
While (true) {
{if (socket. IsConnected ())
if (! Socket. IsInputShutdown ()) {
Data=https://bbs.csdn.net/topics/new byte [527];//data up to 527
Int len.
If ((len=in. Read (data))!=1) {//obtain input stream data
INDATA=https://bbs.csdn.net/topics/new byte (len);
System. Arraycopy (INDATA data, 0, 0, len);
Wl. WriteLog (" receiving data "+ bytesToHexString (INDATA));
Retra=0;//: after receipt of the data does not need the retransmission
SharedPreferences=null;
SharedPreferences=getSharedPreferences (" msbDATA, "Context. MODE_PRIVATE);
SKey=AES. GetsKey (sharedPreferences, ID);
DataAnalysis DataAnalysis=new DataAnalysis ();
Byte [] IN_CMD_DATA=https://bbs.csdn.net/topics/dataanalysis.inDataDivide (INDATA, sKey);
Wl. WriteLog (dataanalysis getResult ());
Wl. WriteLog (" command word and data "+ bytesToHexString (IN_CMD_DATA));
Result=dataanalysis. GetResult ();
The switch (IN_CMD_DATA [0]) {
Case 0 x0f:,,,
Case 0 x8f:,,,
Default:
break;
}
}
}
} else {
//initSocket ();
}
}
} the catch (Exception e) {
The d (" MainActivity ", "end time receiving thread abnormal catch");
e.printStackTrace();
}
}
};


/* *
* send data
*/
Public void sendData publishes the event () {
Try {
If (in!=null)
in.close();
If (out!=null)
out.close();
If (socket!=null)
Socket. The close ();
The socket=null;
Out=null;
In=null;
The socket=new socket (HOST, PORT);
=in the socket. GetInputStream ();
Out=socket. GetOutputStream ();

The Message MSG=scHandler. ObtainMessage ();//start receiving data!!!!! Must add or second won't receive it,,,
MSG. What=0;//this thread is to start to accept data
ScHandler. SendMessage (MSG);

{} the catch (IOException ex)
The d (" MainActivity ", "end timing sendData publishes the event inside the new socket abnormality catch");
ex.printStackTrace();
}
If (socket!=null) {
{if (socket. IsConnected ())
if (! Socket. IsOutputShutdown ()) {
getData();//call for everydata
OUTDATA=https://bbs.csdn.net/topics/DataProcessing.outDataCombination (START, TYPE, ID, CTRL, L, CMD_DATA, CS, END);
Try {
Out. Write (OUTDATA);//send interface data
} the catch (IOException e) {
The d (" MainActivity ", "end of timing to send data write abnormal catch");
e.printStackTrace();
}
}
}
}
}


This is the code for sending and receiving data,
For each great god first aid! Thank you very much!

CodePudding user response:

To send data is the main thread or the child thread?
Is there a panic?
Suggest the debug step into view

CodePudding user response:

reference 1/f, wang can reply:
send data is the main thread or the child thread?
Is there a panic?
Suggest the debug step to see
send data is the main thread, by the way the problem has been resolved

CodePudding user response:

Then by the way solution, then no satisfaction stick

CodePudding user response:

reference 3 floor wang can reply:
just by the way solution, then there is no satisfactory junction post
what did you say?
  • Related