AutoCloseable ꡬνν κ°μ²΄λ§ κ°λ₯
private void connectSendReceive(Packet packet) {
LogWrapper.d(TAG, LogLogic.TCP, "connectSendReceive " + mHost + ":" + mPort);
SocketAddress socketAddress = new InetSocketAddress(mHost, mPort);
try (Socket socket = new Socket()) {
socket.setSoTimeout(SOCKET_READ_TIMEOUT);
socket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT);
LogWrapper.d(TAG, LogLogic.TCP, "after connect");
try (BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream());
BufferedInputStream is = new BufferedInputStream(socket.getInputStream())) {
os.write(packet.packet, 0, packet.packet.length);
os.flush();
LogWrapper.d(TAG, LogLogic.TCP, "after write");
byte[] buffer = new byte[MAX_READ_SIZE];
int readSize = is.read(buffer, 0, buffer.length);
LogWrapper.d(TAG, LogLogic.TCP, "after read, size:" + readSize);
if (readSize > 0)
LogWrapper.d(TAG, LogLogic.TCP, new String(Arrays.copyOf(buffer, readSize)));
mEventListener.onFinished(packet, readSize > 0);
} catch (Exception e) {
LogWrapper.w(TAG, LogLogic.TCP, "sendAndReceive", e);
mEventListener.onFinished(packet, false);
}
} catch (Exception e) {
LogWrapper.w(TAG, LogLogic.TCP, "connect", e);
mEventListener.onFinished(packet, false);
}
}