How to send arbitrary data type to socket

How to send arbitrary data type to socket

Post by Jinghou L » Sat, 29 Jul 1995 04:00:00



I am writing a program that needs to send arbitrary type of data via BSD
socket.
Right now, I am using a char array to buffer those data. In my implementation
one thing is guaranteed: both the sending and receiving ends know what type of
data it's supposed to write/read. So, I am worrying about how to convert
any type of data into char string and the reverse. The solution should be
portable as all kinds fo UNIXs are supposed to run this program.

Any suggestion? Please help!

- Jing-Hou Li

 
 
 

How to send arbitrary data type to socket

Post by Michael Davi » Tue, 01 Aug 1995 04:00:00


: Right now, I am using a char array to buffer those data. In my implementation
: one thing is guaranteed: both the sending and receiving ends know what type of
: data it's supposed to write/read. So, I am worrying about how to convert
: any type of data into char string and the reverse.

Simply use a cast. For example, if you wanted to write a float into a char
array:

   char buffer [BUFSIZE];
   float f;
   int i;
   char *c;

   f = get_float_val ();        /* initialise f */
   c = (char *)f;               /* set c to point to f */

   /* copy f into buffer */
   for (i = 0; i < sizeof (float); ++i)
      buffer [i] = *(c++);

Simply reverse the last line to unpack the buffer back into the
float:

      *(c++) = buffer [i];

The method is the same for an array of floats, or any other data
type (simply adjust the "i < sizeof ..." to the correct size).

--

 Michael Davis   \     WWW:     http://www.he.tdl.com/~jubjub/

 Software engineer solicits new C/C++/UNIX contracts ... see my web page
                PGP public key available via finger, WWW, or key servers

 
 
 

How to send arbitrary data type to socket

Post by Dongxiao Y » Tue, 01 Aug 1995 04:00:00


Quote:Jinghou Li <jli> writes:
>I am writing a program that needs to send arbitrary type of data via BSD
>socket.
>Right now, I am using a char array to buffer those data. In my implementation
>one thing is guaranteed: both the sending and receiving ends know what type of
>data it's supposed to write/read. So, I am worrying about how to convert
>any type of data into char string and the reverse. The solution should be
>portable as all kinds fo UNIXs are supposed to run this program.
>Any suggestion? Please help!

If you are sure that your app will only run on the same kind of (or compatible)
machines, then you can send the bytes one by one as the the other followup says.
Actually you can simply, do
  float f;
  write(socket_descriptor, (char*)&f, sizeof(float));

However, if you want to run the program on different machines (such that they
represent floats differently), you need to use something like XDR, which
converts data into a common format before sending, and converts it back when
receiving.

D. Yue

>- Jing-Hou Li


 
 
 

How to send arbitrary data type to socket

Post by Jeff Dicks » Tue, 01 Aug 1995 04:00:00


Jinghou Li <jli> writes:
>I am writing a program that needs to send arbitrary type of data via BSD
>socket.
>Right now, I am using a char array to buffer those data. In my implementation
>one thing is guaranteed: both the sending and receiving ends know what type of
>data it's supposed to write/read. So, I am worrying about how to convert
>any type of data into char string and the reverse. The solution should be
>portable as all kinds fo UNIXs are supposed to run this program.
>Any suggestion? Please help!
>- Jing-Hou Li


You don't have to worry about converting different types of data into character
strings. You only need ensure that you convert integers and longs to network
byte order before being transmitted and back to host order upon reception (or
before using). A template of sorts to lay over the buffer that would define
the data would make life alot easier. Else you'd have to worry about the num-
ber of bytes wide a particular data was and caculate your own offsets. In a
nutshell you plug the data in and let the underlying hardware figure out how
to break it apart byte-by-byte.

struct template {
    int something;
    long youpick;
    struct gluttonforpunishment {
        char string[104];
        int whatever;
    }

Quote:} *tptr;

In your code:
    .
    .
  char packet_buffer[1024];
    .
    .
  tptr = (struct template *)packet_buffer;
    .
    .
  tptr->youpick = htonl(really_big_number);
  strcpy(tptr->gluttonforpunishment.string, "you get the drift");

Jeff S.*son

 
 
 

How to send arbitrary data type to socket

Post by Jeff Dicks » Tue, 01 Aug 1995 04:00:00




>: Right now, I am using a char array to buffer those data. In my implementation
>: one thing is guaranteed: both the sending and receiving ends know what type of
>: data it's supposed to write/read. So, I am worrying about how to convert
>: any type of data into char string and the reverse.
>Simply use a cast. For example, if you wanted to write a float into a char
>array:
>   char buffer [BUFSIZE];
>   float f;
>   int i;
>   char *c;
>   f = get_float_val ();    /* initialise f */
>   c = (char *)f;           /* set c to point to f */
>   /* copy f into buffer */
>   for (i = 0; i < sizeof (float); ++i)
>      buffer [i] = *(c++);
>Simply reverse the last line to unpack the buffer back into the
>float:
>      *(c++) = buffer [i];
>The method is the same for an array of floats, or any other data
>type (simply adjust the "i < sizeof ..." to the correct size).

Whoa! The C hard liners like Dan Pop over on comp.lang.c would have a field
day with this one! I would imagine this is very unportable, because it makes
assumptions about the underlying hardware (the storage of information).

Jeff S.*son

 
 
 

How to send arbitrary data type to socket

Post by Frederic Mo » Tue, 01 Aug 1995 04:00:00



>I am writing a program that needs to send arbitrary type of data via BSD
>socket.
>Right now, I am using a char array to buffer those data. In my implementation
>one thing is guaranteed: both the sending and receiving ends know what type of
>data it's supposed to write/read. So, I am worrying about how to convert
>any type of data into char string and the reverse. The solution should be
>portable as all kinds fo UNIXs are supposed to run this program.

Two solutions:

1) Do it with sprintf()/sscanf() if the data aren't too large

2)Use XDR and the associated C routine generators to
generate automatic conversion routines from native data to machine-neutral
format and back. Warning: XDR does not exist on all machines.
--

  Ingenieur logiciel - Developpement d'applications AIX
  Software engineer - AIX Application Development

 
 
 

How to send arbitrary data type to socket

Post by Axel-Stéphane C. Sm?rgr » Wed, 02 Aug 1995 04:00:00





> >: Right now, I am using a char array to buffer those data. In my implementation
> >: one thing is guaranteed: both the sending and receiving ends know what type of
> >: data it's supposed to write/read. So, I am worrying about how to convert
> >: any type of data into char string and the reverse.

> >Simply use a cast. For example, if you wanted to write a float into a char
> >array:

> >   char buffer [BUFSIZE];
> >   float f;
> >   int i;
> >   char *c;

> >   f = get_float_val ();       /* initialise f */
> >   c = (char *)f;              /* set c to point to f */

> >   /* copy f into buffer */
> >   for (i = 0; i < sizeof (float); ++i)
> >      buffer [i] = *(c++);

> >Simply reverse the last line to unpack the buffer back into the
> >float:

> >      *(c++) = buffer [i];

> >The method is the same for an array of floats, or any other data
> >type (simply adjust the "i < sizeof ..." to the correct size).

> Whoa! The C hard liners like Dan Pop over on comp.lang.c would have a field
> day with this one! I would imagine this is very unportable, because it makes
> assumptions about the underlying hardware (the storage of information).

It is erroneous too.

c = (char *) &f
             =

If you have a limited set of data types you wish to transfer, the easiest way
to do so is to transfer data as character arrays using sprintf()/sscanf() to
format/extract. For transfer of various complex types, I would look at ASN.1
or XDR

-ascs

 
 
 

How to send arbitrary data type to socket

Post by Joern Rennec » Tue, 08 Aug 1995 04:00:00



>Jinghou Li <jli> writes:
>>I am writing a program that needs to send arbitrary type of data via BSD
>>socket.
>>Right now, I am using a char array to buffer those data. In my implementation
>>one thing is guaranteed: both the sending and receiving ends know what type of
>>data it's supposed to write/read. So, I am worrying about how to convert
>>any type of data into char string and the reverse. The solution should be
>>portable as all kinds fo UNIXs are supposed to run this program.
>>Any suggestion? Please help!
>>- Jing-Hou Li

>You don't have to worry about converting different types of data into character
>strings. You only need ensure that you convert integers and longs to network
>byte order before being transmitted and back to host order upon reception (or
>before using). A template of sorts to lay over the buffer that would define
>the data would make life alot easier. Else you'd have to worry about the num-
>ber of bytes wide a particular data was and caculate your own offsets. In a
>nutshell you plug the data in and let the underlying hardware figure out how
>to break it apart byte-by-byte.
>struct template {
>    int something;
>    long youpick;
>    struct gluttonforpunishment {
>    char string[104];
>    int whatever;
>    }
 ;
>} *tptr;

This is just plain wrong.
You cannot be sure what size the integer types are.
And even you you have two machines with compilers where integer type sizes
match, alignment differences of the compilers can make the memory layout
different. E.g. assume that int is 16 bit and long is 32. Then on a machine
with long alignment <= 2, youpick has offset 2, while on machines with long
alignment 4, youpick has offset 4 .

Portable to a wider range of machines is:

struct template {
    int something:16;
    int pad:16;
    int youpick:32;
    struct gluttonforpunishment {
        char string[104];
        int whatever:16;
    };

Quote:} *tptr;

or /* of course you have to be consistent with your pick */

struct template {
    int youpick:32;
    int something:16;
    struct gluttonforpunishment {
        char string[104];
        int whatever:16;
    };

Quote:} *tptr;

However, keep in mind that there is no guarantee that the compiler will
actually place all members with the desired sizes in the desired order.

It is safer to disassemble / assemble integers to/from single chars.
This way, you can broaden the portability to all systems that have >= 8 bit
chars, BSD style sockets and transmit 8 bit/char on sockets.

( It may be argued that sockets no longer have
  BSD style if you transfer anything but 8 bits/char )

Sort of a tradeoff between the degree of unportability and unmaintability :-)

If you have some CPU cycles to burn, you can write your own portable XDR
library (to overcome the problem that not all machines have XDR) and use
that.

        Joern Rennecke

 
 
 

1. HDI: call an arbitrary function with an arbitrary number of arguments (and possibly arbitrary types)

If only this were lisp where the world is simple...  I'm not too happy
about http://www.eskimo.com/~scs/C-faq/q15.12.html , is there a
solution for Linux/GCC?  How about C++?

I figured out how to call an arbitrary function (here "ioctl") with
known arguments;

      int fd = open("/dev/cdrom", O_RDONLY);
      void *handle = dlopen("/lib/libc.so", RTLD_LAZY);
      int (*function)() = dlsym(handle, "ioctl");
      (*function)(fd, CDROMCLOSETRAY);  // whee
      dlclose(handle);

For a pseudo-arbitrary number of arguments, I could have a switch
statement that handles the cases for 0, 1, 2, 3, x arguments but how
lame would that be?

/me suddenly obsessed with partially implementing WinNT's
    "rundll32.exe" in Linux.

-N.
--
http://www.cs.mcgill.ca/~navindra/editors/

2. Connect Win95 to FreeBSD by PLIP???

3. Sending abstract data type from C over socket to Java and visa versa ?!

4. modprobe: Can't locate module ppp0

5. Server side includes got me down

6. SPAK(Send PAcKets)- tools to send arbitrary packets

7. Tvtwm for Solaris 2.3

8. Sending XML data to Apache web server and capturing this sent data from the Apache server

9. Sending out of band data to Solaris 2.x socket

10. Sending data via sockets

11. send float,binary data over sockets

12. send & receive data through socket