代码(废话就不多说了直接上代码,不留坑每行都有注释)
package dao;
import com.alibaba.fastjson.JSON;
import pojo.DFBD;
import tools.GetTime;
import tools.HttpUtils;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
public class Client {
public static final String IP_ADDR = "111.111.228.11";
public static final int PORT = 10001;
public static final String url = "";
static String text = null;
public static void main(String[] args) throws IOException {
int count = 0;
System.out.println("客户端启动…");
Socket socket = null;
socket = new Socket(IP_ADDR, PORT);
while (true) {
try {
DataInputStream input = new DataInputStream(socket.getInputStream());
byte[] buffer;
buffer = new byte[input.available()];
if(buffer.length != 0){
System.out.println("length="+buffer.length);
DFBD dfbd = new DFBD();
input.read(buffer);
String three = new String(buffer);
String[] ret = three.split(",");
if (buffer.length == 19){
dfbd.setScanPerson("MES智能绑定");
for (String x : ret){
if (x.length() == 10){
dfbd.setMoldingBarCode(x);
System.out.println("参数"+x);
}else if (x.length() == 8){
dfbd.setQrcode(x);
System.out.println("参数"+x);
}else {
System.out.println("智能绑定参数抓取不全");
}
}
if (dfbd.getQrcode() != null && dfbd.getMoldingBarCode() != null){
Object json = JSON.toJSON(dfbd);
Object jsonObject= HttpUtils.HttpPostWithJson(url,json.toString());
System.out.println(jsonObject.toString());
}
}
count++;
System.out.println("第"+count+"个内容=" + three+",收取时间:"+GetTime.getTimeNow());
Thread.sleep(200);
}
} catch (Exception e) {
System.out.println("客户端异常:" + e.getMessage());
}finally {
}
}
}
}