用了一款gprs dtu,发现始终发送注册包
问题如下:
dtu中设置心跳包fe,注册包785634123133393030303030303131016800,设置为“与服务器连接后发送一次”,服务端就频繁收到这个注册包,心跳包始终没有收到。这是何原因,明显是注册包没有注册成功。
代码如下:
主程序:
public static void main(String[] args) {
try {
//创建一个服务器端的Socket,即ServerSocket,绑定需要监听的端口
ServerSocket serverSocket = new ServerSocket(5002);
log.info(serverSocket.getLocalSocketAddress());
Socket socket = null;
//记录连接过服务器的客户端数量
long count = 0;
log.info("***DTU server waiting***");
while(true){//循环侦听新的客户端的连接
//调用accept()方法侦听,等待客户端的连接以获取Socket实例
socket = serverSocket.accept();
if (socket!=null) {
log.info("clientSocket:"+socket);
InetAddress address = socket.getInetAddress();
System.out.println("当前客户端的IP为:"+address.getHostAddress());
//创建新线程
Thread thread = new Thread(new ServerThread(socket));
thread.start();
count++;
log.info("count:"+count);
}
}
//serverSocket.close();一直循环监听,不用关闭连接
} catch (IOException e) {
e.printStackTrace();
}
线程:
public class ServerThread implements Runnable {
private static final Logger log = LogManager.getLogger("ServerThread");
Socket socket = null;//和本线程相关的Socket
public ServerThread(Socket socket) {
this.socket = socket;
}
public void run(){
InputStream is = null;
BufferedReader br = null;
OutputStream os = null;
try {
//与客户端建立通信,获取输入流,读取取客户端提供的信息
byte[] bufin = new byte[128];
byte[] bufout = null;
byte[] bufintemp = null;
socket.getInputStream().read(bufin);
bufintemp=Util.bytesToBytes(bufin);
log.info("Receive-text:"+(Util.bytesToHexString(bufintemp)==null?"null":Util.bytesToHexString(bufintemp)));
if ((bufintemp[0]==(byte)0xfe)||(bufintemp==null)) {
byte[] temp = new byte[8];
bufout = null;
temp[0]=(byte) 0x0b;
temp[1]=(byte) 0x03;
temp[2]=(byte) 0x00;
temp[3]=(byte) 0x00;
temp[4]=(byte) 0x00;
temp[5]=(byte) 0x0d;
temp[6]=(byte) 0x84;
temp[7]=(byte) 0xa5;
bufout=temp;
}else if((bufintemp[0]==(byte)0x7b)&&(bufintemp[1]==(byte)0xff)) {//注册
bufout = null;
byte[] temp = new byte[16];
System.arraycopy(bufintemp,0,temp,0,15);
bufout=temp;
bufout[1]=(byte) 0x81;
bufout[2]=(byte) 0x00;
bufout[3]=(byte) 0x10;
bufout[15]=(byte) 0x7b;
}else if((bufintemp[0]==(byte)0x7b)&&(bufintemp[1]==(byte)0x01)) {//发关数据
bufout = null;
byte[] data= {(byte) 0xaa,(byte) 0xbb,(byte) 0xcc};
byte[] datalen=Util.getBytes2((data.length+10));
byte[] temp = new byte[data.length+16];
System.arraycopy(bufintemp,0,temp,0,15);
bufout=temp;
bufout[1]=(byte) 0x89;
bufout[2]=datalen[0];
bufout[3]=datalen[1];
System.arraycopy(data,0,bufout,15,data.length);
bufout[data.length+16-1]=(byte) 0x7b;
}else if(bufintemp[0]==(byte)0x78) {
bufout = null;
byte[] temp = new byte[18];
System.arraycopy(bufintemp,0,temp,0,18);
}
else {
bufout = null;//可考虑发送一个字符
}
socket.shutdownInput();//关闭输入流
//获取输出流,响应客户端的请求
BufferedOutputStream bfo = new BufferedOutputStream(socket.getOutputStream());
byte[] bufouttemp = new byte[8];//{(byte) 0x0b,(byte) 0x03,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x0d,(byte) 0x84,(byte) 0xA5};
bufouttemp[0]=(byte)0x0b;
bufouttemp[1]=(byte)0x03;
bufouttemp[2]=(byte)0x00;
bufouttemp[3]=(byte)0x00;
bufouttemp[4]=(byte)0x00;
bufouttemp[5]=(byte)0x02;
bufouttemp[6]=(byte)0xc4;
bufouttemp[7]=(byte)0xa1;
// System.arraycopy(bufouttemp,0,bufout,0,8);
log.info("Send-text:"+Util.bytesToHexString(bufouttemp));
bfo.write(bufouttemp);
bfo.flush();
bfo.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭资源即相关socket
try {
if(os!=null)
os.close();
if(br!=null)
br.close();
if(is!=null)
is.close();
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}