Я пытаюсь получить данные от оборудования и создал класс, который будет делать сокет, заданный через атрибуты IP. Поскольку поток запускается один, все идет хорошо, но когда я запускаю второй поток с другим IP, оба потока получают один и тот же аргумент.
public static String addressFiorenza = "192.168.9.201"; //Fiorenza
public static String addressFriulmac = "192.168.10.191"; //Friulmac
public static int port = 9761;
public static void main(String[] args) throws Exception {
Thread friulmac = new threadForMachine(addressFriulmac, port);
friulmac.start();
Thread fiorenza = new threadForMachine(addressFiorenza, port);
fiorenza.start();}
Глупый вопрос, я предлагаю, но почему это так?
upd: Извините, вот тема ThreadForMachine:
public class threadForMachine extends Thread implements Runnable {
private static Socket s;
public static String address;
public static int port;
byte[] requestBothRegTime = {(byte) 0x10, (byte) 0x03, (byte) 0x00, (byte) 0x16, (byte) 0x00, (byte) 0x02, (byte) 0x26, (byte) 0x8E};
byte[] requestForStatus = {(byte) 0x10, (byte) 0x03, (byte) 0x00, (byte) 0x1A, (byte) 0x00, (byte) 0x01, (byte) 0xA6, (byte) 0x8C};
byte[] requestForOnTimes = {(byte) 0x10, (byte) 0x03, (byte) 0x00, (byte) 0x18, (byte) 0x00, (byte) 0x02, (byte) 0x47, (byte) 0x4D};
String inProgress = "3425680";
String inIdle = "1328200";
boolean isStarted;
public threadForMachine(String IPAddress, int port) {
this.address = IPAddress;
this.port = port;
}
public void run() {
System.out.print("Current status of" + address + " is: ");
try {
if (status() == true) {
System.out.println("ONLINE");
} else {
System.out.println("OFFLINE");
}
} catch (IOException ex) {
Logger.getLogger(threadForMachine.class.getName()).log(Level.SEVERE, null, ex);
}
do {
try {
getRequest(address, "Fiorenza");
} catch (IOException ex) {
Logger.getLogger(threadForMachine.class.getName()).log(Level.SEVERE, null, ex);
}
} while (true);
}
private void getRequest(String IP, String machineID) throws IOException {
if (status() == true && isStarted == false) {
isStarted = true;
System.out.println("Time is " + getUptime());
System.out.println("Times on " + getOnTimes());
System.out.println("ITS ALIVE!!!");
}
if (status() == false && isStarted == true) {
isStarted = false;
System.out.println("ITS DEAD!!!!");
}
}
public boolean status() throws IOException {
String status = workFlow(requestForStatus, address, port, 7);
if (status.equals(inProgress)) {
return true;
}
return false;
}
public int getUptime() throws IOException {
int upTime = Integer.parseInt(workFlow(requestBothRegTime, address, port, 7));
return upTime;
}
public int getOnTimes() throws IOException {
int onTimes = Integer.parseInt(workFlow(requestForOnTimes, address, port, 7));
return onTimes;
}
public static String workFlow(byte[] comm, String address, int port, int length) throws IOException {
String dataBlock = "";
try {
byte inp[] = new byte[10];
s = createSocket(address, port);
s.getOutputStream().write(comm);
s.setSoTimeout(1000);
Thread.sleep(250);
s.getInputStream().read(inp);
dataBlock = Integer.toString(fromHex(getTime(inp, length)));
getTime(inp, length);
s.close();
} catch (Exception e) {
System.out.println("init error: " + e.getMessage());
if (e.getMessage().contains("Read timed out")) {
System.out.println("Connection lost. Attempting to reconnect");
s.close();
}
}
return dataBlock;
}
public static Socket createSocket(String address, int port) throws Exception {
Socket socket = new Socket(address, port);
return socket;
}
}