問題如下:
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=https://bbs.csdn.net/topics/ {(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();
}
}
}
}
uj5u.com熱心網友回復:
有遇到這個問題的嗎?uj5u.com熱心網友回復:
服務器端收到注冊包后,有沒有及時回應資料至dtu,用tcp工具測驗下你的服務端。推薦使用Mina框架開發服務端程式。轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/105314.html
標籤:網絡通信
上一篇:RT63365E+RT5392L+RT63087N 有什么好韌體刷嗎?
下一篇:win2008r2 虛擬機 開放445埠協議出入站規則后,telnet 127或local能通,telnet 本機IP +埠不通
