import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Date;
import org.apache.commons.httpclient.util.DateUtil;
public class CallSocket {
public int execShell(String ip, int port, String sendbuf,
StringBuilder recvbuf) {
PrintWriter os = null;
BufferedReader is = null;
Socket socket = new Socket();
try {
// 发出客户请求
//socket = new Socket(ip, port);
System.out.println("====socket start==="+DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss|SSS "));
socket.connect(new InetSocketAddress(ip, port), 1000);//设置连接请求超时时间10 s
System.out.println("====socket connect==="+DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss|SSS "));
socket.setSoTimeout(10000);//设置超时时间10秒
// 由系统标准输入设备构造BufferedReader对象
os = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"utf-8"),true);
// 由Socket对象得到输出流,并构造PrintWriter对象
is = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8"));
int sendlen = sendbuf.length();
os.print(sendbuf);
// 刷新输出流,使Server马上收到该字符串
os.flush();
char[] cbuf = new char[128];
// 从Server读入一字符串
int nbyte = is.read(cbuf);
System.out.println(cbuf);
// if (nbyte != 2 || !"00".equals(String.valueOf(cbuf, 0, nbyte))) {
// return -2;
// }
recvbuf.append(cbuf);
if (os != null)
os.close(); // 关闭Socket输出流
if (is != null)
is.close(); // 关闭Socket输入流
if (socket != null)
socket.close(); // 关闭Socket
os = null;
is = null;
socket = null;
System.out.println("====socket end==="+DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss|SSS "));
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
if (os != null)
os.close(); // 关闭Socket输出流
if (is != null)
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 关闭Socket输入流
if (socket != null)
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 关闭Socket
os = null;
is = null;
socket = null;
}
return 0;
}
public String execShell(String ip, int port, String sendbuf) {
System.out.println("====socket start==="+DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss|SSS "));
PrintWriter os = null;
BufferedReader is = null;
Socket socket = new Socket();
String xml="";
try {
socket.connect(new InetSocketAddress(ip, port), 5000);//设置连接请求超时时间10 s
System.out.println("====socket connect==="+DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss|SSS "));
socket.setSoTimeout(10000);//设置读操作超时时间30 s
// 由系统标准输入设备构造BufferedReader对象
os = new PrintWriter(socket.getOutputStream());
// 由Socket对象得到输出流,并构造PrintWriter对象
is = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
// 由Socket对象得到输入流,并构造相应的BufferedReader对象
os.print(sendbuf);
// 刷新输出流,使Server马上收到该字符串
os.flush();
char[] cbuf = new char[1024];
// 从Server读入一字符串
is.read(cbuf);
xml=new String(cbuf);
if (os != null)
os.close(); // 关闭Socket输出流
if (is != null)
is.close(); // 关闭Socket输入流
if (socket != null)
socket.close(); // 关闭Socket
os = null;
is = null;
socket = null;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null)
os.close(); // 关闭Socket输出流
if (is != null)
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 关闭Socket输入流
if (socket != null)
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 关闭Socket
os = null;
is = null;
socket = null;
}
return xml;
}
private byte asc_to_bcd(byte asc) {
byte bcd;
if ((asc >= '0') && (asc <= '9'))
bcd = (byte) (asc - '0');
else if ((asc >= 'A') && (asc <= 'F'))
bcd = (byte) (asc - 'A' + 10);
else if ((asc >= 'a') && (asc <= 'f'))
bcd = (byte) (asc - 'a' + 10);
else
bcd = (byte) (asc - 48);
return bcd;
}
private byte[] ASCII_To_BCD(byte[] ascii, int asc_len) {
byte[] bcd = new byte[asc_len / 2];
int j = 0;
for (int i = 0; i < (asc_len + 1) / 2; i++) {
bcd[i] = asc_to_bcd(ascii[j++]);
bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc_to_bcd(ascii[j++])) + (bcd[i] << 4));
}
return bcd;
}
public static void main(String[] args) {
CallSocket callSocket=new CallSocket();
String str = "0158<?xml version=\"1.0\" encoding=\"UTF-8\"?><request><trans_code>RPN</trans_code><new_pin>111111</new_pin><merNo>22</merNo><pan>6988100000000002311</pan></request>";
callSocket.execShell("192.168.10.73", 9999, str);
//callSocket.execShell("localhost", 7810, str);
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Date;
import org.apache.commons.httpclient.util.DateUtil;
public class CallSocket {
public int execShell(String ip, int port, String sendbuf,
StringBuilder recvbuf) {
PrintWriter os = null;
BufferedReader is = null;
Socket socket = new Socket();
try {
// 发出客户请求
//socket = new Socket(ip, port);
System.out.println("====socket start==="+DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss|SSS "));
socket.connect(new InetSocketAddress(ip, port), 1000);//设置连接请求超时时间10 s
System.out.println("====socket connect==="+DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss|SSS "));
socket.setSoTimeout(10000);//设置超时时间10秒
// 由系统标准输入设备构造BufferedReader对象
os = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"utf-8"),true);
// 由Socket对象得到输出流,并构造PrintWriter对象
is = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8"));
int sendlen = sendbuf.length();
os.print(sendbuf);
// 刷新输出流,使Server马上收到该字符串
os.flush();
char[] cbuf = new char[128];
// 从Server读入一字符串
int nbyte = is.read(cbuf);
System.out.println(cbuf);
// if (nbyte != 2 || !"00".equals(String.valueOf(cbuf, 0, nbyte))) {
// return -2;
// }
recvbuf.append(cbuf);
if (os != null)
os.close(); // 关闭Socket输出流
if (is != null)
is.close(); // 关闭Socket输入流
if (socket != null)
socket.close(); // 关闭Socket
os = null;
is = null;
socket = null;
System.out.println("====socket end==="+DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss|SSS "));
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
if (os != null)
os.close(); // 关闭Socket输出流
if (is != null)
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 关闭Socket输入流
if (socket != null)
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 关闭Socket
os = null;
is = null;
socket = null;
}
return 0;
}
public String execShell(String ip, int port, String sendbuf) {
System.out.println("====socket start==="+DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss|SSS "));
PrintWriter os = null;
BufferedReader is = null;
Socket socket = new Socket();
String xml="";
try {
socket.connect(new InetSocketAddress(ip, port), 5000);//设置连接请求超时时间10 s
System.out.println("====socket connect==="+DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss|SSS "));
socket.setSoTimeout(10000);//设置读操作超时时间30 s
// 由系统标准输入设备构造BufferedReader对象
os = new PrintWriter(socket.getOutputStream());
// 由Socket对象得到输出流,并构造PrintWriter对象
is = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
// 由Socket对象得到输入流,并构造相应的BufferedReader对象
os.print(sendbuf);
// 刷新输出流,使Server马上收到该字符串
os.flush();
char[] cbuf = new char[1024];
// 从Server读入一字符串
is.read(cbuf);
xml=new String(cbuf);
if (os != null)
os.close(); // 关闭Socket输出流
if (is != null)
is.close(); // 关闭Socket输入流
if (socket != null)
socket.close(); // 关闭Socket
os = null;
is = null;
socket = null;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null)
os.close(); // 关闭Socket输出流
if (is != null)
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 关闭Socket输入流
if (socket != null)
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 关闭Socket
os = null;
is = null;
socket = null;
}
return xml;
}
private byte asc_to_bcd(byte asc) {
byte bcd;
if ((asc >= '0') && (asc <= '9'))
bcd = (byte) (asc - '0');
else if ((asc >= 'A') && (asc <= 'F'))
bcd = (byte) (asc - 'A' + 10);
else if ((asc >= 'a') && (asc <= 'f'))
bcd = (byte) (asc - 'a' + 10);
else
bcd = (byte) (asc - 48);
return bcd;
}
private byte[] ASCII_To_BCD(byte[] ascii, int asc_len) {
byte[] bcd = new byte[asc_len / 2];
int j = 0;
for (int i = 0; i < (asc_len + 1) / 2; i++) {
bcd[i] = asc_to_bcd(ascii[j++]);
bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc_to_bcd(ascii[j++])) + (bcd[i] << 4));
}
return bcd;
}
public static void main(String[] args) {
CallSocket callSocket=new CallSocket();
String str = "0158<?xml version=\"1.0\" encoding=\"UTF-8\"?><request><trans_code>RPN</trans_code><new_pin>111111</new_pin><merNo>22</merNo><pan>6988100000000002311</pan></request>";
callSocket.execShell("192.168.10.73", 9999, str);
//callSocket.execShell("localhost", 7810, str);
}
}