2

I want to send a structure which contains several double data type elements using network socket. Receivers can be of different architecture. What will be better optimized approach to do this except converting it to a string?

peterh
  • 11,875
  • 18
  • 85
  • 108
Pratik
  • 21
  • 2
  • You open a socket, call `connect()` or `bind()` depending on whether it's the client or the server socket, and the use `read()` and `write()`, but be careful with endianness. – Iharob Al Asimi Mar 25 '15 at 13:03
  • 1
    When sending `double` you need to take care of more issues than just endianness. Some info here: http://stackoverflow.com/questions/6084279/host-to-network-double – Giorgi Moniava Mar 25 '15 at 13:12
  • As @DrKoch recommended, JSON or something similar is worth looking into. – user5071535 Jul 20 '15 at 22:45

1 Answers1

1

On a socket connection exists nothing but raw bytes. So you have to do two things:

  1. Convert your double into bytes in a way the receiver can understand. This is rather complicated for doubles see here

  2. Invent some field separator which you send between the individual doubles.

After all it is a bad idea to reinvent the wheel. You could use some established format like XML or JSON.

Community
  • 1
  • 1
DrKoch
  • 9,556
  • 2
  • 34
  • 43