Wednesday, April 9, 2008

Byte Ordering between java and C#

In many scenarios, there will be a server which is written in one language(c# or java) and there will be a client written in another language(c# or java). The client-server communication may be carried out via binary-stream requests and responses. In my case the server was java and i had 2 clients one in java and another in c#. When i constructed a binary request and from java client and sent to server, i got a proper response. But when i sent the same request from c# client, i got an internal server error. I was stuck for a day, then only i figured out very basic mistake of mine. By default c# handles data in little-endian format and java operates on big-endian format. When i sent request from c# client, it was in little-endian format but java server assumed it as big-endian format and while decoding from binary format, it gets invalid data, bcoz of that it was throwing internal server error. once i convert the request stream in to big-endian format manually, everything started working as expected. So when ever we want to make a binary communication between server and client of different languages we need to take care of the endianess .