Created
September 15, 2015 06:02
-
-
Save 0532/313313d485309db9059c to your computer and use it in GitHub Desktop.
socket client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tmp; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.OutputStream; | |
| import java.io.UnsupportedEncodingException; | |
| import java.net.InetAddress; | |
| import java.net.InetSocketAddress; | |
| import java.net.Socket; | |
| /** | |
| * 第三方服务器 工商E线通 client | |
| * Date: 14-11-27 | |
| */ | |
| public class TpsSocketClient { | |
| private String ip; | |
| private int port; | |
| private int timeout = 30000; //超时时间:ms 连接超时与读超时统一 | |
| public TpsSocketClient(String ip, int port) { | |
| this.ip = ip; | |
| this.port = port; | |
| } | |
| public byte[] call(byte[] sendbuf) throws Exception { | |
| byte[] recvbuf = null; | |
| InetAddress addr = InetAddress.getByName(ip); | |
| Socket socket = new Socket(); | |
| try { | |
| socket.connect(new InetSocketAddress(addr, port), timeout); | |
| //socket.setSendBufferSize(100); | |
| socket.setSoTimeout(timeout); | |
| OutputStream os = socket.getOutputStream(); | |
| os.write(sendbuf); | |
| os.flush(); | |
| InputStream is = socket.getInputStream(); | |
| recvbuf = new byte[8]; | |
| int readNum = is.read(recvbuf); | |
| if (readNum == -1) { | |
| throw new RuntimeException("服务器连接已关闭!"); | |
| } | |
| if (readNum < 8) { | |
| throw new RuntimeException("读取报文头长度部分错误..."); | |
| } | |
| int msgLen = Integer.parseInt(new String(recvbuf).trim()); | |
| recvbuf = new byte[msgLen - 8]; | |
| //TODO | |
| Thread.sleep(500); | |
| readNum = is.read(recvbuf); //阻塞读 | |
| if (readNum != msgLen - 8) { | |
| throw new RuntimeException("报文长度错误,报文头指示长度:[" + msgLen + "], 实际获取长度:[" + readNum +"]"); | |
| } | |
| } finally { | |
| try { | |
| socket.close(); | |
| } catch (IOException e) { | |
| // | |
| } | |
| } | |
| //System.out.println("---:" + new String(recvbuf,"GBK")); | |
| return recvbuf; | |
| } | |
| public static void main(String... argv) throws UnsupportedEncodingException { | |
| TpsSocketClient mock = new TpsSocketClient("127.0.0.1", 60001); | |
| //1070:入资登记预交易 | |
| String msg = "" + | |
| "1070" + //交易码 | |
| "02" + //银行代码 2 CHAR 中行代码统一使用01 | |
| "1111111" + //柜员号 7 CHAR 右补空格 | |
| "22222" + //机构号 5 CHAR 右补空格 | |
| "3333" + //地区码 4 CHAR 右补空格 | |
| "44" + //工商局编号 2 CHAR | |
| "12345678901234567890123456789012"; //预登记号 32 CHAR 右补空格 | |
| String strLen = null; | |
| strLen = "" + (msg.getBytes("GBK").length + 4); | |
| String lpad = ""; | |
| for (int i = 0; i < 4 - strLen.length(); i++) { | |
| lpad += "0"; | |
| } | |
| strLen = lpad + strLen; | |
| byte[] recvbuf = new byte[0]; | |
| try { | |
| recvbuf = mock.call((strLen + msg).getBytes("GBK")); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| System.out.printf("服务器返回:%s\n", new String(recvbuf, "GBK")); | |
| } | |
| public void setTimeout(int timeout) { | |
| this.timeout = timeout; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment