Java如何按16进制发送和接收TCP指令
更新时间:2023年09月28日 10:23:24 作者:qq_38310536
这篇文章主要介绍了Java如何按16进制发送和接收TCP指令问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
Java按16进制发送和接收TCP指令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import org.apache.http.util.TextUtils; public class TCPSocket{ public static void main(String args[]){ String cmdInfor= "00 00 00 00 00 06 01 01 00 01 00 20" ; send(cmdInfor); } public static String send(String cmdInfor){ String strReturn = null ; try { //要连接的服务端IP地址 String host = "192.168.1.2" ; //要连接的服务端对应的监听端口 int port = 502 ; //将十六进制的字符串转换成字节数组 byte [] cmdInfor2 = hexStrToBinaryStr(cmdInfor); //1.建立客户端socket连接,指定服务器位置及端口 Socket clientSocket = new Socket(host,port); //2.得到socket读写流 OutputStream os=clientSocket.getOutputStream(); PrintWriter pw= new PrintWriter(os); //输入流 InputStream is=clientSocket.getInputStream(); //3.利用流按照一定的操作,对socket进行读写操作 os.write(cmdInfor2); os.flush(); clientSocket.shutdownOutput(); //接收服务器的响应 int line = 0 ; byte [] buf = new byte [is.available()]; //接收收到的数据 while ((line=is.read(buf))!=- 1 ){ //将字节数组转换成十六进制的字符串 strReturn= BinaryToHexString(buf); } //4.关闭资源 is.close(); pw.close(); os.close(); clientSocket.close(); } catch (Exception e){ e.printStackTrace(); } return strReturn; } /** * 将十六进制的字符串转换成字节数组 * * @param hexString * @return */ public static byte [] hexStrToBinaryStr(String hexString) { if (TextUtils.isEmpty(hexString)) { return null ; } hexString = hexString.replaceAll( " " , "" ); int len = hexString.length(); int index = 0 ; byte [] bytes = new byte [len / 2 ]; while (index < len) { String sub = hexString.substring(index, index + 2 ); bytes[index/ 2 ] = ( byte )Integer.parseInt(sub, 16 ); index += 2 ; } return bytes; } /** * 将字节数组转换成十六进制的字符串 * * @return */ public static String BinaryToHexString( byte [] bytes) { String hexStr = "0123456789ABCDEF" ; String result = "" ; String hex = "" ; for ( byte b : bytes) { hex = String.valueOf(hexStr.charAt((b & 0xF0 ) >> 4 )); hex += String.valueOf(hexStr.charAt(b & 0x0F )); result += hex + " " ; } return result; } } |
Java使用TCP协议发送和接收数据
TCP
TCP是面向连接的可靠传输协议
Java使用TCP发送数据代码
三步:
- (1) 创建客户端的Socket对象Socket
- (2) 获取输出流,写数据
- (3) 释放资源
1 2 3 4 5 6 7 8 9 10 11 12 | import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; public class TCPTest01 { public static void main(String[] args) throws IOException { Socket s = new Socket(InetAddress.getByName( "uos-pc" ), 10086 ); OutputStream os = s.getOutputStream(); os.write( "FTP" .getBytes()); s.close(); } } |
Java使用TCP接收数据代码
四步
- (1) 创建服务器Socket对象ServerSocket
- (2) 监听客户端链接,返回Socket对象
- (3) 获取输入流,读数据,控制台显示
- (4) 释放资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class TCPTest02 { public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket( 10086 ); Socket s = ss.accept(); InputStream is = s.getInputStream(); byte [] b = new byte [ 1024 ]; int len = is.read(b); String data = new String(b, 0 , len); System.out.println(data); s.close(); ss.close(); } } |
执行
分别启动TCPTest01.java和TCPTest02.java,可在控制台看到输出TCP。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
springboot之security FilterSecurityInterceptor的使用要点记录
这篇文章主要介绍了springboot之security FilterSecurityInterceptor的使用要点记录,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-12-12
最新评论