Socket发送Bytes信息到接口

来源:互联网 发布:vb api程序设计 编辑:程序博客网 时间:2024/05/16 03:13

import java.io.BufferedInputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.net.Socket;

import java.net.UnknownHostException;

import java.util.Random;

 

publicclass A {

    publicstatic Socket oSocket;

   

    publicstatic String sendMessage(String message) {

        String result = null;

        try {

            OutputStream outputStream = oSocket.getOutputStream();

            outputStream.write(hexStringToBytes(message));

            outputStream.flush();

            BufferedInputStream in = new BufferedInputStream(

                    oSocket.getInputStream());

            byte[] b =new byte[1024];

            in.read(b);

            result = bytesToHexString(b);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return result;

    }

    public String excuteXdrByMsi(int length)

    {

        StringBuffer buffer = new StringBuffer("0123456789");

        StringBuffer sb = new StringBuffer();

        Random r = new Random();

        int range = buffer.length();

        for (int i = 0; i < length; i ++)

        {

            sb.append(buffer.charAt(r.nextInt(range)));

        }

        return sb.toString();

    }

    /**

     * 描述信息

     *

     * @param args

     */

    publicstatic void main(String[] args) {

        try {

          A a=new A();

          StringBuffer str = new StringBuffer();

            str.append("7e5a");

            str.append("0014");

            str.append("23");

            str.append("40");

            str.append("00");

            str.append("03");

            str.append("0000");

            str.append("0000");

            str.append("00000000");

            str.append("00000000");

            str.append(a.excuteXdrByMsi(32));

            str.append(a.excuteXdrByMsi(8));

            oSocket =new Socket("111.111.111.111", 1111);

            sendMessage(String.valueOf(str));

        } catch (UnknownHostException e) {

            //TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            //TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

 

    publicstatic byte[] hexStringToBytes(String hexString)

    {

      if ((hexString ==null) || (hexString.equals(""))) {

        returnnull;

      }

      hexString = hexString.toUpperCase();

      int length = hexString.length() / 2;

      char[] hexChars = hexString.toCharArray();

      byte[] d =new byte[length];

      for (int i = 0; i < length; i++) {

        int pos = i * 2;

        d[i] = (byte)(charToByte(hexChars[pos]) << 4 |charToByte(hexChars[(pos + 1)]));

      }

      return d;

    }

    privatestatic byte charToByte(char c)

    {

        return (byte)"0123456789ABCDEF".indexOf(c);

    }

    publicstatic final String bytesToHexString(byte[] bArray) {

        StringBuffer sb = new StringBuffer(bArray.length);

        String sTemp;

        for (int i = 0; i < bArray.length; i++) {

         sTemp = Integer.toHexString(0xFF & bArray[i]);

         if (sTemp.length() < 2)

          sb.append(0);

         sb.append(sTemp.toUpperCase());

        }

        return sb.toString();

       }

 

}

 

0 0
原创粉丝点击