0

I have C# program that makes a tcp connection with another c# program. In the c# program to send an message I did this :

private TcpClient client;

private void SendPulse()
{
    byte[] send_Buffer;
    port = 11000;

    while (true)
    {
        lock (locked)
        {
            try
            {
                BlockID = 1003;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (BinaryWriter w = new BinaryWriter(ms))
                    {
                        NetworkStream stream = client.GetStream();
                        BlockID = 1003;
                        LengthMessage = 84;

                        // Header : 
                        w.Write(BeginMessage);
                        w.Write(BlockID);
                        w.Write(LengthMessage);
                        w.Write(RadarID);
                        w.Write(Time);
                        w.Write(ModeSystem);
                        w.Write(Icd_primary_var);
                        w.Write(Icd_secondary_ver);

                        // Data :
                        w.Write(StatusSystem);
                        send_Buffer = ms.ToArray();

                        stream.Write(send_Buffer, 0, send_Buffer.Length);

                        Thread.Sleep(3000); // Send pulse every 3 seconds.
                    }
                }
            }
            catch
            {

            }
        }
    }
}

The idea is to write in binarywriter and than convert the memory we wrote on to byte array and to send it.

Now I have Java programming. I want to do it too, I have connection with C# but I dont know how to send it. I did DataOutputStream but it send every parameter alone, I want all in 1 array of bytes exactly like in the c# code.

Thanks for helpers.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
RonYamin
  • 77
  • 2
  • 11
  • As I've said in other posts to other folks, rather than communicating via old/proprietary messaging formats, why not use JSON or XML for these communications? – ControlAltDel Nov 05 '14 at 19:23

1 Answers1

0

If you want to use DataOutputStream, you can wrap it around a BufferedOutputStream and flush() it when you are done.

Or you can use an NIO ByteBuffer and write it to the socket.

To make the message easier to decode I would add the length to the start, unless you know it every message will be that length.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Ok, So as you said I used ByteArrayOutputStream, but it doesn't have writeDouble func, only int. What can I do ? – RonYamin Nov 05 '14 at 20:10
  • You still need DataOutputStream. Like I said, I would use BufferOutputStream to do what you want. – Peter Lawrey Nov 05 '14 at 20:47
  • Would you help me to solve it ? please I'm really don't know what to do. – RonYamin Nov 05 '14 at 20:57
  • Wrap the socket output stream with a buffered output stream, wrap that with a data output stream. Write what you want and flush () to send. Note: java defaults to big endian and I assume C# uses little endian. – Peter Lawrey Nov 05 '14 at 21:03
  • I write : out.writedouble --- 8 bytes. and in the c# i read 8 bytes by readDouble function of binaryReader and it wrong reading. What to do ? what the hell ? :( – RonYamin Nov 05 '14 at 21:15
  • Like I said, the endianess is not the same. http://en.m.wikipedia.org/wiki/Endianness – Peter Lawrey Nov 05 '14 at 21:26
  • The solution is usually to use ByteBuffer to swap the byte order. Using NIO is quite a bit harder. – Peter Lawrey Nov 05 '14 at 21:27
  • I tried to use byteBuffer but without success would you help me with my code ? please dude .. – RonYamin Nov 05 '14 at 21:30