Transfer an object from java to C# -
i have communicate between java , c# need send object through socket server. here server portion written in java , client portion written c#. want send object server(java) , receive in client(c#). can't it.
localjobinfo.java :
package testsocket; import java.io.serializable; public class localjobinfo implements serializable{ private string id; private string message; public string getid() { return id; } public void setid(string id) { this.id = id; } public string getmessage() { return message; } public void setmessage(string message) { this.message = message; } } server (in java) :
list<localjobinfo> jobinfolist = new arraylist<localjobinfo>(); localjobinfo jobinfo = new localjobinfo(); jobinfo.setid("1"); jobinfo.setmessage("success"); jobinfolist.add(jobinfo); serversocket serversocket = new serversocket(4343, 10); socket socket = serversocket.accept(); inputstream = null; outputstream os = null; string received = null; try { = socket.getinputstream(); os = socket.getoutputstream(); byte[] lenbytes = new byte[4]; is.read(lenbytes, 0, 4); int len = (((lenbytes[3] & 0xff) << 24) | ((lenbytes[2] & 0xff) << 16) | ((lenbytes[1] & 0xff) << 8) | (lenbytes[0] & 0xff)); byte[] receivedbytes = new byte[len]; is.read(receivedbytes, 0, len); received = new string(receivedbytes, 0, len); system.out.println("server received: " + received); // sending byte[] tosendbytes = null; bytearrayoutputstream bos = new bytearrayoutputstream(); objectoutput out = null; try { out = new objectoutputstream(bos); out.writeobject(jobinfolist); out.flush(); tosendbytes = bos.tobytearray(); } { try { bos.close(); } catch (ioexception ex) { // ignore close exception } } int tosendlen = tosendbytes.length; byte[] tosendlenbytes = new byte[4]; tosendlenbytes[0] = (byte) (tosendlen & 0xff); tosendlenbytes[1] = (byte) ((tosendlen >> 8) & 0xff); tosendlenbytes[2] = (byte) ((tosendlen >> 16) & 0xff); tosendlenbytes[3] = (byte) ((tosendlen >> 24) & 0xff); os.write(tosendlenbytes); os.write(tosendbytes); try { thread.sleep(10000); } catch (exception ex) { } } catch (exception ex) { } socket.close(); serversocket.close(); client (in c#) :
string tosend = "hello!"; ipendpoint serveraddress = new ipendpoint(ipaddress.parse("127.0.0.1"), 4343); socket clientsocket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); clientsocket.connect(serveraddress); // sending int tosendlen = system.text.encoding.ascii.getbytecount(tosend); byte[] tosendbytes = system.text.encoding.ascii.getbytes(tosend); byte[] tosendlenbytes = system.bitconverter.getbytes(tosendlen); clientsocket.send(tosendlenbytes); clientsocket.send(tosendbytes); // receiving byte[] rcvlenbytes = new byte[4]; clientsocket.receive(rcvlenbytes); int rcvlen = system.bitconverter.toint32(rcvlenbytes, 0); byte[] rcvbytes = new byte[rcvlen]; clientsocket.receive(rcvbytes); memorystream memstream = new memorystream(); binaryformatter binform = new binaryformatter(); memstream.write(rcvbytes, 0, rcvbytes.length); memstream.seek(0, seekorigin.begin); object obj = (object)binform.deserialize(memstream); i got exception during deserialize byte[] in c# client. exception message : "the input stream not valid binary format. starting contents (in bytes) are: ac-ed-00-05-73-72-00-17-74-65-73-74-73-6f-63-6b-65 ..."
now question is, how can fix issue in order deserialize in c# code , object sent server in java ?
you need language agnostic serialisation framework. examples include google protocol buffers, asn.1, xsd.
basically need turn schema file (like gpb .proto, or , asn.1 .asn, or .xsd) both pojo , poco classes (for java , c# respectively). that's gpb compiler, or asn.1 compiler can you. end classes in both languages, , these classes serialise / deserialise / common wire format.
xsd more complex; there's xsd.exe microsoft turn .xsd file c#. i've no idea if there's similar java.
i think best place start google protocol buffers - it's free, , going. , supports both java , c#.
i wouldn't touch json schemas; code generation tools seem long way behind. there's plenty of validators tell if message have received, or send, conforms json schema, doesn't formulate message in first place.
Comments
Post a Comment