java socket 短连接 请求 并获取返回

package com.test;

import java.io.DataInputStream;

public class socketDemo {
    public static void main(String[] args) {
        send();
    }

  

public static String send() {
        Socket socket = null;
        String info = "";
        OutputStream out=null;
        InputStream in=null;
        try {
            // socket用于连接某个服务器
            socket = new Socket("localhost", 12351);
            out = socket.getOutputStream();
            in = socket.getInputStream();
            String reqbody = "390000000046464646";//发送内容
            byte[] msgByteArray = hexString2bytes(reqbody);
            out.write(msgByteArray);
            out.flush();
            
            //获取返回
            byte[] datalength = new byte[2];
            in.read(datalength, 0, datalength.length);
            String datLen = bytesToHexString(datalength);
            int headLen = Integer.valueOf(headLen, 16).intValue();
            byte[] message = new byte[headLen];
            DataInputStream dis = new DataInputStream(in);
            dis.readFully(message);
            info = bytesToHexString(message);
            System.out.println("收到返回:" + info);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } // 这时候可以关闭socket连接
            }
            
        }
        return info;

    }

    public static String bytesToHexString(byte[] bytes) {
        StringBuilder stringBuilder = new StringBuilder();
        if ((bytes == null) || (bytes.length <= 0)) {
            return "";
        }
        for (byte b : bytes) {
            int v = b & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString().toUpperCase();
    }

    public static byte[] hexString2bytes(String asciiStr) {
        String hexStr;
        int len = asciiStr.length();
        if (len % 2 != 0) {
            hexStr = "0" + asciiStr;
        } else {
            hexStr = asciiStr;
        }
        byte[] bytes = new byte[hexStr.length() / 2];
        for (int i = 0; i < bytes.length; i++) {
            try {
                bytes[i] = (byte) (0xff & Integer.parseInt(
                        hexStr.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return bytes;
    }
}
 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值