Socket发送Bytes信息到接口

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 {

    public static Socket oSocket;

   

    public static 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

     */

    public static 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();

        }

    }

 

    public static byte[] hexStringToBytes(String hexString)

    {

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

        return null;

      }

      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;

    }

    private static byte charToByte(char c)

    {

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

    }

    public static 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();

       }

 

}

 

展开阅读全文

没有更多推荐了,返回首页