https://ferozedaud.blogspot.com/2009/11/howto-serialize-data-from-object-from.html
The Java implementation was relying on ByteBuffer class to serialize the object in LittleEndian format that the .NET code understands.
However, it turns out that using a ByteBuffer is not necessary. Java's native format is BigEndian. The network byte order is also BigEndian. However, the X86 platform (and .NET) are LittleEndian. So, we only need a way to convert from BigEndian to LittleEndian and vice versa on .NET.
In .NET, the IPAddress.HostToNetworkOrder and IPAddress.NetworkToHostOrder methods are provided for doing this conversion.
On the java application, we can forego the ByteBuffer and use DataInputStream/DataOutputStream directly. This makes the code more concise and easy to understand.
With these changes, the .NET application looks as follows: (I am only including changes to the Read() method, the other stuff is the same.
static void Read(TcpClient client) { Console.WriteLine("Got connection: {0}", DateTime.Now); NetworkStream ns = client.GetStream(); BinaryReader reader = new BinaryReader(ns); Employee emp = new Employee(); // first read the Id emp.Id = IPAddress.NetworkToHostOrder(reader.ReadInt32()); // length of first name in bytes. int length = IPAddress.NetworkToHostOrder(reader.ReadInt32()); // read the name bytes into the byte array. // recall that java side is writing two bytes for every character. byte[] nameArray = reader.ReadBytes(length); emp.FirstName = Encoding.UTF8.GetString(nameArray); // last name length = IPAddress.NetworkToHostOrder(reader.ReadInt32()); nameArray = reader.ReadBytes(length); emp.LastName = Encoding.UTF8.GetString(nameArray); // salary emp.Salary = IPAddress.NetworkToHostOrder(reader.ReadInt32()); Console.WriteLine(emp.Id); Console.WriteLine(emp.FirstName); Console.WriteLine(emp.LastName); Console.WriteLine(emp.Salary); System.Threading.Thread.Sleep(5); Console.WriteLine("Writing data..."); // now reflect back the same structure. BinaryWriter bw = new BinaryWriter(ns); bw.Write(IPAddress.HostToNetworkOrder(emp.Id)); byte [] data = Encoding.UTF8.GetBytes(emp.FirstName); bw.Write(IPAddress.HostToNetworkOrder(data.Length)); bw.Write(data); data = Encoding.UTF8.GetBytes(emp.LastName); bw.Write(IPAddress.HostToNetworkOrder(data.Length)); bw.Write(data); bw.Write(IPAddress.HostToNetworkOrder(emp.Salary)); Console.WriteLine("Writing data...DONE"); client.Client.Shutdown(SocketShutdown.Both); ns.Close(); }
As you can see, we still use BinaryReader/BinaryWriter. However we just call NetworkToHostOrder or HostToNetworkOrder before writing the bytes on the wire.
On the java application, there are changes in the Main() method and the serialize() method. We directly work with DataInputStream/DataOutputStream which are similar to .NET's BinaryReader/BinaryWriter.
public static void main(String [] args) { int written = 0; EmployeeData emp = new EmployeeData(); emp.setFirstName("John"); emp.setLastName("Smith"); emp.setId(1); emp.setSalary(2); // Create the encoder and decoder for targetEncoding Charset charset = Charset.forName("UTF-8"); CharsetDecoder decoder = charset.newDecoder(); CharsetEncoder encoder = charset.newEncoder(); try { Socket client = new Socket("localhost", 8080); OutputStream oStream = client.getOutputStream(); InputStream iStream = client.getInputStream(); DataOutputStream dos = new DataOutputStream(oStream); serializeToStream(dos, emp, encoder); DataInputStream dis = new DataInputStream(iStream); // now client echoes back the data. EmployeeData rEmp = new EmployeeData(); rEmp.setId(dis.readInt()); int length = dis.readInt(); System.out.println("FName Length: " + length); byte [] stringBuffer = new byte[length]; dis.read(stringBuffer); rEmp.setFirstName(decoder.decode(ByteBuffer.wrap(stringBuffer)).toString()); length = dis.readInt(); System.out.println("LName Length: " + length); stringBuffer = new byte[length]; dis.read(stringBuffer); rEmp.setLastName(decoder.decode(ByteBuffer.wrap(stringBuffer)).toString()); rEmp.setSalary(dis.readInt()); System.out.println("ID: " + rEmp.getId()); System.out.println("First: " + rEmp.getFirstName()); System.out.println("Last: " + rEmp.getLastName()); System.out.println("Salary: " + rEmp.getSalary()); System.out.flush(); client.close(); } catch(Exception e) { e.printStackTrace(System.err); } finally { System.out.println("Written bytes: " + written); } } private static void serializeToStream(DataOutputStream os, EmployeeData emp, CharsetEncoder encoder) throws IOException { // id os.writeInt(emp.getId()); CharBuffer nameBuffer = CharBuffer.wrap(emp.getFirstName().toCharArray()); ByteBuffer nbBuffer = null; // length of first name try { nbBuffer = encoder.encode(nameBuffer); } catch(CharacterCodingException e) { throw new ArithmeticException(); } System.out.println(String.format("String [%1$s] #bytes = %2$s", emp.getFirstName(), nbBuffer.limit())); os.writeInt(nbBuffer.limit()); os.write(nbBuffer.array()); // put lastname nameBuffer = CharBuffer.wrap(emp.getLastName().toCharArray()); nbBuffer = null; // length of first name try { nbBuffer = encoder.encode(nameBuffer); } catch(CharacterCodingException e) { throw new ArithmeticException(); } System.out.println(String.format("String [%1$s] #bytes = %2$s", emp.getLastName(), nbBuffer.limit())); os.writeInt(nbBuffer.limit()); os.write(nbBuffer.array()); // salary os.writeInt(emp.getSalary()); }
We do not need to use ByteBuffer anymore - we were only using it because it supports LittleEndian format. The java app always expects to read/write bytes from the wire in BigEndian format (which is java's native format, as well as the Network order for all communications). The .NET application does the conversion from BigEndian to LittleEndian and back.
this was helpful, thanks; the only issue I noticed was lines 79 and 80, in the java app, I think it should be the array length, not the limit.
ReplyDeleteAnon - thanks for your comments. The ByteBuffer was returned from a call to CharSetEncoder::Encode(), therefore, shouldn't limit() be same as length() of the array?
ReplyDeleteHi Feroze, I have posted some code for a c# client/server but do not know enough Java to write the Java part.
ReplyDeleteI don't know whether you are interested in this still.
http://www.codeproject.com/Messages/4089961/Re-Typed-Socket-Communication-of-Objects-Between-J.aspx