import java.io.DataInputStream; import java.io.OutputStream; import java.net.Socket; public class CMD_Util { /** * * @param cmd_bbs * 待發送的指令 * @return 返回值 */ private String proxy_ip = "localhost"; private int proxy_port = 1001; public String sendCommand(byte[] cmd_bbs) { StringBuffer stb = new StringBuffer(); try { Socket socket = new Socket(proxy_ip, proxy_port); // // 获取输出流 // DataOutputStream dos = new DataOutputStream( // socket.getOutputStream()); OutputStream dos = socket.getOutputStream(); // 发送数据 // dos.writeBytes("00177E07"); dos.write(cmd_bbs); dos.flush(); DataInputStream dis = new DataInputStream(socket.getInputStream()); // System.out.println(dis.read()); byte[] bytes = new byte[256]; // System.out.println(dis.read(bytes)); dis.read(bytes); char temp_char; for (int i = 0; i < 256; i++) { Byte bt = Byte.valueOf(bytes[i]); if ((i < 255 && bt == 0) && (Byte.valueOf(bytes[i + 1]) == 0)) { break; } temp_char = (char) bytes[i]; stb.append(temp_char); } // System.out.println(stb.toString()+":"+stb.length()); socket.close(); } catch (Exception e) { e.printStackTrace(); stb = new StringBuffer(); stb.append("-1"); } return stb.toString(); } public String getProxy_ip() { return proxy_ip; } public void setProxy_ip(String proxy_ip) { this.proxy_ip = proxy_ip; } public int getProxy_port() { return proxy_port; } public void setProxy_port(int proxy_port) { this.proxy_port = proxy_port; } }
java搭建tcp客户端_java模拟tcp客户端
最新推荐文章于 2024-07-29 11:36:27 发布