DELL
2025-04-28 e6eb7fb0af366e370f125668d62e89eb0004f517
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/********************************* head of file Comm_Socket.java *********************************/
package sp_comm;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.Date;
 
import com.Com;
import com.ComFn;
 
import main.page_debug_inf;
/*************************************************************************************************/
public class Comm_Socket {
    public String mDevIp = "127.0.0.1";
    public int mPort = 1000;
    Socket mSocket = null;
    /*********************************************************************************************/
    public Comm_Socket(String ip_addr, int port) {
        mSocket = new Socket();
        mDevIp = ip_addr;
        mPort = port;
    }
    /*********************************************************************************************/
    public void socketClose() {
        try {
            mSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /*********************************************************************************************/
    static public boolean SocketUdpComm(int local_port, String dev_ip, int port, ByteBuffer bbf_tx, 
                                        ByteBuffer bbf_rx, page_debug_inf dt_debug_inf, boolean printdat) {
        boolean comm_res = false;
        DatagramSocket socket_udp = null;
        try {
            socket_udp = new DatagramSocket(local_port);
            byte[] dat_tx = new byte[bbf_tx.limit()];
            for(int n=0; n<dat_tx.length; n++) {
                dat_tx[n] = bbf_tx.get();
            }
            if(true == printdat) {
                dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) 
                                        + " IP:" + dev_ip + " port:" + port
                                        + " UDP TX: " + ComFn.bytesToHexString(dat_tx, dat_tx.length) + "\n");
            } else {
                dt_debug_inf.addDebugInf("******\n");
            }
            
            InetAddress address = InetAddress.getByName(dev_ip);
            DatagramPacket packet = new DatagramPacket(dat_tx, dat_tx.length, address, port);
            socket_udp.setSoTimeout(1000);
            socket_udp.send(packet);
            
            if(bbf_rx.capacity() > 0) {
                byte[] buffer = new byte[1024];
                DatagramPacket receiver = new DatagramPacket(buffer, buffer.length);
                socket_udp.receive(receiver);
                
                byte[] dat_rx = receiver.getData();
                int rx_len = receiver.getLength();
                bbf_rx.clear();
                bbf_rx.put(dat_rx, 0, rx_len);
                bbf_rx.flip();
                
                if(bbf_rx.limit() > 0) {
                    comm_res = true;
                }
                if(true == printdat) {
                    dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) 
                                            + " UDP RX: " + ComFn.bytesToHexString(dat_rx, rx_len) + "\n");
                } else {
                    dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) + " RX: ******\n");
                }
            }
        } catch (Exception e) {
            //e.printStackTrace();
            dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) + " " + e.toString() + "\n");
        } finally {
            socket_udp.close();
            socket_udp = null;
        }        
        return comm_res;
    }
    /*********************************************************************************************/
    public boolean SocketComm(ByteBuffer bbf_tx, ByteBuffer bbf_rx, 
                                page_debug_inf dt_debug_inf, boolean printdat) {
        boolean comm_res = false;
        bbf_rx.clear();
 
        InputStream s_in = null;
        OutputStream s_out = null;
        try {
            if(false == mSocket.isConnected()) {
                mSocket.connect(new InetSocketAddress(mDevIp, mPort), 1000);
                mSocket.setSoTimeout(500);
            }
            
            if(mSocket.isConnected()) {
                s_in = mSocket.getInputStream();
                s_out = mSocket.getOutputStream();
                
                byte[] dat_tx = new byte[bbf_tx.limit()];
                bbf_tx.position(0);
                for(int n=0; n<dat_tx.length; n++) {
                    dat_tx[n] = bbf_tx.get();
                }
                
                if(true == printdat) {
                    dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) 
                                            + " TCP TX: " + ComFn.bytesToHexString(dat_tx, dat_tx.length) + "\n");
                } else {
                    dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) + " TX: ******\n");
                }
                
                s_out.write(dat_tx);
                s_out.flush();
                /*
                for(int n=0; n<100; n++) {
                    Thread.sleep(10);
                    int len1 = s_in.available();
                    if(len1 <= 0) {
                        continue;
                    }
                    
                    Thread.sleep(10);
                    
                    if(s_in.available() == len1) {
                        break;
                    }
                }
                */
                byte[] dat_rx = new byte[3000];//new byte[s_in.available()];
                int read_len = s_in.read(dat_rx);
                bbf_rx.clear();
                bbf_rx.put(dat_rx, 0, read_len);
                bbf_rx.flip();
                
                if(bbf_rx.limit() > 0) {
                    comm_res = true;
                }
                if(true == printdat) {
                    dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) 
                                            + " TCP RX: " + ComFn.bytesToHexString(dat_rx, read_len) + "\n");
                } else {
                    dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) + " RX: ******\n");
                }
            }
        } catch (IOException e) {
            socketClose();
            dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) + " " + e.toString() + "\n");
            mSocket = new Socket();
        }
        
        return comm_res;
    }
    
    public Socket getSocketConnect() {
        if(false == mSocket.isConnected()) {
            try {
                mSocket.connect(new InetSocketAddress(mDevIp, mPort), 1000);
                mSocket.setSoTimeout(500);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return mSocket;
    }
    
    /*********************************************************************************************/
}
/********************************* end of file Comm_Socket.java **********************************/