Я где-то читал, что сначала нам нужно подключиться к IP-порту устройства. Я подключился через код с помощью веб-сокета. Это мой код. когда я запускаю это, я получаю исключение SocketTimedOut... Вот почему я увеличил время READ_TIMEOUT. Эта проблема все еще возникает? Что я сделал не так? Что мне следует сделать, чтобы подключиться и получить данные с БИОМЕТРИЧЕСКОГО устройства?
Я где-то читал, что сначала нам нужно подключиться к IP-порту устройства. Я подключился через код с помощью веб-сокета. Это мой код. когда я запускаю это, я получаю исключение SocketTimedOut... Вот почему я увеличил время READ_TIMEOUT. Эта проблема все еще возникает? Что я сделал не так? Что мне следует сделать, чтобы подключиться и получить данные с БИОМЕТРИЧЕСКОГО устройства? [code] public String getAttendanceData() { StringBuilder response = new StringBuilder(); Socket socket = null; DataOutputStream dataOutputStream = null; BufferedReader reader = null;
try { // Establish a socket connection to the device socket = new Socket(); socket.connect(new InetSocketAddress(DEVICE_IP, DEVICE_PORT), CONNECTION_TIMEOUT); socket.setSoTimeout(READ_TIMEOUT); // Set socket read timeout
System.out.println("Connection established to device at " + DEVICE_IP + ":" + DEVICE_PORT);
// Send authentication request with the username and password String authCommand = "AUTH:" + USERNAME + ":" + PASSWORD; dataOutputStream = new DataOutputStream(socket.getOutputStream()); dataOutputStream.write(authCommand.getBytes()); System.out.println("Sent authentication command to device: " + authCommand);
// Wait briefly before continuing, to ensure the device has time to process Thread.sleep(1000);
// Read the response from the device reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; while ((line = reader.readLine()) != null) { response.append(line).append("\n"); }
System.out.println("Received response from device."); } catch (SocketTimeoutException e) { System.err.println("Connection timed out: " + e.getMessage()); return "Error: Connection timed out while fetching attendance data."; } catch (SocketException e) { System.err.println("Connection reset: " + e.getMessage()); return "Error: Connection reset. The device might have closed the connection unexpectedly."; } catch (IOException e) { System.err.println("I/O error while communicating with the device: " + e.getMessage()); return "Error fetching attendance data: " + e.getMessage(); } catch (InterruptedException e) { e.printStackTrace(); } finally { try { if (reader != null) reader.close(); if (dataOutputStream != null) dataOutputStream.close(); if (socket != null && !socket.isClosed()) socket.close(); } catch (IOException e) { System.err.println("Error closing resources: " + e.getMessage()); } }
return response.toString(); } } [/code] Я пытаюсь получить данные о посещаемости с этого устройства