接着上一篇PLC与服务端交互,PLC的我不太懂,所以模拟了一个服务端:
public class PlcClient {
private Socket socket;
public PlcClient() throws Exception {
try{
socket = new Socket("localhost",8888);
}catch (Exception e){
e.printStackTrace();
throw e;
}
}
/**
* 客户端开始工作
*/
public void start() throws Exception {
try{
/**
* 用户输入(模拟PLC指令(二进制字节数组))
*/
Scanner sc = new Scanner(System.in);
OutputStream out = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(out,"UTF-8");
PrintWriter pw = new PrintWriter(osw,true);
while (true){
pw.println(sc.nextLine());
}
}catch (Exception e){
e.printStackTrace();
throw e;
}
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
try {
PlcClient plcClient = new PlcClient();
plcClient.start();
} catch (Exception e) {
System.out.println("客户端运行失败!");
}
}
}