1

I have a socket sever is written by java, it always listens on specific port. I wrote a client in Java and it could connect to the server but when I created a client in C#, I could not connect to Server.

I just want to send a short value to start a java server.

I guess the problem come from the endianess (little-endian, big-endian) and I have several days to research but can not solve this issue.

I post more information about my problem

I use java.io.* and java.net.*. My java code

out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
public static final short CLIENT = 0;
out.writeShort(CLIENT);
out.flush();

and with c#, I use System.Net.Sockets. My C# code is below:

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
...
short sClient = 0;
if (BitConverter.IsLittleEndian) sClient = IPAddress.HostToNetworkOrder(sClient);

NetworkStream ns = new NetworkStream(socket);
BinaryWriter bw = new BinaryWriter(ns);
bw.Write(sClient);

The java code can connect to the java server but the c# code can not :(. The output error from java server is "Exception while getting socket streams"

Anyone can help me about this problem, thanks in advance

1
  • 1
    to rule out endianness, just print out the short you receive and the short you print. If for instance the short you send is 0x1234 and the one you receive is 0x3412 then hooray it's an endian issue. If you receive nothing or something completely different, it's something else. Debugging is nothing more than step by step ruling things out until you find the problem Commented Jan 15, 2010 at 8:39

2 Answers 2

1

If endianness is your problem you should have a look at the following two links which show you how to convert between Host and Network Byte order

Network to Host order

Host to network order

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I already tried Host to Network but can not solve this problem. In Java, my code like that: out.writeShort(CLIENT); (out is ObjectOutputStream, CLIENT is short) and my code in C#: short sClient = 0; if (BitConverter.IsLittleEndian) sClient = IPAddress.HostToNetworkOrder(sClient); with the java code, the java server will return a packet but with the c# code, the java server always returns "Exception while getting socket streams" Hope anyone can help me, thanks in advance.
1

A server is a server, so you're going to need to be more specific. There could be a million reasons they aren't communicating correctly. Can you ping the machine the service is on? Are they running on the same machine or might there be firewall issues? Are you debugging each to see if the request is timing out or being actively denied? Are they connecting but the encoding is wrong?

Also, what protocols are you using? If you're sending binary data in a custom protocol format, endianess might be a worry, but most protocols expect network byte order. If there is an endianess problem, that's something you'd need to verify in a debugger. What calsses are you using in Java and .NET? Can you post some sample code?

More information is necessary to solve this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.