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