java socket 接收byte数组

2017-02-20  本文已影响981人  LH_0811

public static String send8583(String send8583Str,String host,int port) throws Exception{
        
    //客户端请求与本机在20011端口建立TCP连接 
    Socket client = new Socket(host, port);  
        client.setSoTimeout(70000);             
        //获取Socket的输出流,用来发送数据到服务端                  
        PrintStream out = new PrintStream(client.getOutputStream());          
        //获取Socket的输入流,用来接收从服务端发送过来的数据 
        InputStream buf =  client.getInputStream();  
        String str = "mpos-"+send8583Str;  
        //发送数据到服务端   
        out.println(str);  
        try{  
            byte[] b = new byte[1024];
            int rc=0;
            int c = 0;
            while( (rc = buf.read(b, c, 1024) )>=0){
                c = buf.read(b, 0, rc);
            }
            String returnStr = byte2hex(b);
            String string = returnStr;
            String str16 = string.substring(0, 4);
            int leng = Integer.parseInt(str16,16);
            String result = string.substring(0, leng*2 + 4);
            return result;
            
        }catch(Exception e){ 
            e.printStackTrace();
            System.out.println("Time out, No response");  
        }
       return null;
    }


上一篇下一篇

猜你喜欢

热点阅读