/** * Java interacts with c * @param param send information * @return return information */public static map<string, string> infexchange (Object param) {map& Lt String, string> returnmsg = null; Socket socket = Null;dataoutputstream OutputStream = Null;datainputstream InputStream = null; String host = Null;int Port = 0;try{//Read address and port number host = Global.getconfig ("Socket.host");p ort = integer.valueof ( Global.getconfig ("Socket.port"));//Create socketsocket = new Socket (host, port); Socket.setsotimeout (20000);//timeout set to 20s The object is converted to Jsonobjectmapper mapper = new Objectmapper (); String json = mapper.writevalueasstring (param);//Front plus 4-bit length String sendmsg = Stringutils.padleft (string.valueof ( Json.getbytes ("GBK"). Length), ' 0 ', 4 + json; System.out.println ("delivery paper:" + sendmsg);//Send outputstream = new DataOutputStream (Socket.getoutputstream ()); O Utputstream.write (Sendmsg.getbytes ("GBK")); Outputstream.flush ();//Receive return InputStream = new DataInputStream ( Socket.getinputstream ()); byte[] B = new Byte[4];int count = 1;//reads only two times, reads the message length for the first time, reads the complete message for the second time (inputstream.read(b)!=-1) {if (count==1) {int len = integer.valueof (new String (b)); b = new byte[len];count++; Thread.Sleep (200);} Else{break;}} String response = new String (b, "GBK"); SYSTEM.OUT.PRINTLN ("return message:" + response);//Convert the JSON back to map format if (response!=null &&! "". Equals (response)) {returnmsg = Mapper.readvalue (response, Map.class);}} catch (Exception e) {e.printstacktrace ();} finally{//Close Stream try {inputstream.close (); Outputstream.close (); Socket.close ();} catch (IOException e) { E.printstacktrace ();}} return returnmsg;}
Java interacts with C