package kr.co.icontrols.ilp300_700.ifconverter;

import android.util.Log;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import kr.co.icontrols.ilp300_700.global.Global;

public class IFConvertClient extends Thread {
    String TAG = "IFConvertClient";

    byte[] hSendMsg;
    String strTargetIP;
    int nTargetPort;

    public IFConvertClient(byte[] Msg, String TargetIP, int TargetPort) {
        hSendMsg = Msg;
        strTargetIP = TargetIP;
        nTargetPort = TargetPort;

        Log.d(TAG, "[IFConvertClient] Msg destination [" + strTargetIP + ":" + nTargetPort + "]");
        Log.d(TAG, "[IFConvertClient] Msg [" + Global.printHexs(hSendMsg) + "]");
    }

    public void run() {
        Socket SendSock;
        try {
            SendSock = new Socket(strTargetIP, nTargetPort);
            SendSock.setSoLinger(true, 1000);
            DataOutputStream OutStm = new DataOutputStream(SendSock.getOutputStream());
            byte[] OutBuffer = hSendMsg;
            OutStm.write(OutBuffer);
            OutStm.flush();
            SendSock.close();
        } catch (UnknownHostException e) {
            Log.e(TAG, "[UnknownHostException] IFConvertClient.run()");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(TAG, "[IOException] IFConvertClient.run()");
            e.printStackTrace();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] IFConvertClient.run()");
            e.printStackTrace();
        }
    }


}