Dtu set in a heartbeat packets fe, 785634123133393030303030303131016800, registered package set to send a "after" connect to the server, the server will often received the registration packet, heartbeat package has not received, this is why, obviously is a registered package without success,
The code is as follows:
The main program:
Public static void main (String [] args) {
Try {
//create a Socket on the server, namely the ServerSocket, binding need to monitor the port
ServerSocket ServerSocket=new ServerSocket (5002);
The info (serverSocket getLocalSocketAddress ());
The Socket Socket=null;
//record connection number of client server
Long count=0;
The info (" * * * * * * DTU server waiting ");
While (true) {//cycle to listen to a new client connection
//call the accept () method to listen and wait for the client connection in order to get the Socket instance
The socket=serverSocket. The accept ();
If (socket!=null) {
The info (" clientSocket: "+ socket);
InetAddress address=socket. GetInetAddress ();
System. The out. Println (" current client IP for: "+ address. GetHostAddress ());
//create a new thread
Thread the Thread=new Thread (new ServerThread (socket));
Thread. The start ();
count++;
The info (" count: "+ count);
}
}
//serverSocket. Close (); Has been circulating listening, don't have to close the connection
} the catch (IOException e) {
e.printStackTrace();
}
Thread:
Public class ServerThread implements Runnable {
Private static final Logger log=that the LogManager. GetLogger (" ServerThread ");
The Socket Socket=null;//associated with this thread Socket
Public ServerThread (Socket Socket) {
This. The socket=socket;
}
Public void the run () {
InputStream is=null;
BufferedReader br=null;
OutputStream OS=null;
Try {
//establish communication with the client to get the input stream, read the information provided by the client
Byte [] bufin=new byte [128].
Byte [] bufout=null;
Byte [] bufintemp=null;
Socket. GetInputStream (). Read (bufin);
Bufintemp=Util. BytesToBytes (bufin);
The info (" Receive - the text: "+ (Util. BytesToHexString (bufintemp)==null?" Null ": Util. BytesToHexString (bufintemp)));
If ((bufintemp [0]==(byte) 0 xfe) | | (bufintemp==null) {
Byte [] temp=new byte [8].
Bufout=null;
\ [0]=0 x0b (byte);
Temp [1]=0 x03 (byte);
\ [2]=0 x00 (byte);
\ [3]=0 x00 (byte);
\ [4]=0 x00 (byte);
\ [5]=0 x0d (byte);
\ [6]=0 x84 (byte);
\ [7]=0 xa5 (byte);
Bufout=temp;
} else if ((bufintemp [0]==(byte) 0 x7b) & amp; & (bufintemp [1]==(byte) 0 XFF)) {//registered
Bufout=null;
Byte [] temp=new byte [16].
System. Arraycopy (bufintemp, 0, temp, 0 (9);
Bufout=temp;
Bufout [1]=0 x81 (byte);
Bufout [2]=0 x00 (byte);
Bufout [3]=0 x10 (byte);
Bufout [15]=0 x7b (byte);
} else if ((bufintemp [0]==(byte) 0 x7b) & amp; & (bufintemp [1]==(byte) 0 x01)) {//send off data
Bufout=null;
Byte [] data={https://bbs.csdn.net/topics/(byte) 0 xaa, (byte) 0 XBB, (byte) 0 XCC};
Byte [] datalen=Util. GetBytes2 ((data. Length + 10));
Byte [] temp=new byte [data length + 16].
System. Arraycopy (bufintemp, 0, temp, 0 (9);
Bufout=temp;
Bufout [1]=0 x89 (byte);
Bufout [2]=datalen [0];
Bufout [3]=datalen [1].
System. Arraycopy (data, 0, bufout, 15, the data length);
Bufout [data. The length + 16-1]=0 x7b (byte);
} else if (bufintemp [0]==(byte) 0 x78) {
Bufout=null;
Byte [] temp=new byte [18].
System. Arraycopy (bufintemp, 0, temp, 0, 17).
}
The else {
Bufout=null;//can consider to send a character
}
Socket. ShutdownInput ();//close the input stream
//get the output stream, in response to the client's request
BufferedOutputStream bfo=new BufferedOutputStream (socket. GetOutputStream ());
Byte [] bufouttemp=new byte [8].//{(byte) 0 x0b, (byte) 0 x03, 0 x00 (byte), 0 x00 (byte), 0 x00 (byte), (byte) 0 x0d, 0 x84 (byte), (byte) 0 xa5};
Bufouttemp [0]=0 x0b (byte);
Bufouttemp [1]=0 x03 (byte);
Bufouttemp [2]=0 x00 (byte);
Bufouttemp [3]=0 x00 (byte);
Bufouttemp [4]=0 x00 (byte);
Bufouttemp [5]=0 x02 (byte);
Bufouttemp [6]=0 xc4 (byte);
Bufouttemp [7]=0 xa1 (byte);
//System. Arraycopy (bufout bufouttemp, 0, 0, 8).
The info (" Send - the text: "+ Util. BytesToHexString (bufouttemp));
Bfo. Write (bufouttemp);
Bfo. Flush ();
Bfo. Close ();
} the catch (IOException e) {
e.printStackTrace();
} the finally {
//closes the socket associated with the resource that is
Try {
If (OS!=null)
os.close();
If (br!=null)
Br. The close ();
If (is!=null)
Is the close ();
If (socket!=null)
Socket. The close ();
} the catch (IOException e) {
e.printStackTrace();
}
}
}
}
CodePudding user response:
Have encountered this problem?CodePudding user response:
After receipt of the registration package on the server, is there any response data in a timely manner to the dtu, test your server with TCP tools, it is recommended to use Mina framework development server program,