/********************************* 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 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 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; } /*********************************************************************************************/ } /********************************* end of file Comm_Socket.java **********************************/