473,473 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Structure through sendto()

I'm currently using the function:
sendto(sockfd, message, strlen(message), 0,&client_addr, addr_size)

to sending a message back to a client, and I was wondering if there was
a way of sending a structure instead, so that I can open it in the
client side?
Thanks

May 26 '06 #1
7 8234
In article <11**********************@38g2000cwa.googlegroups. com>,
<at*****@gmail.com> wrote:
I'm currently using the function:
sendto(sockfd, message, strlen(message), 0,&client_addr, addr_size) to sending a message back to a client, and I was wondering if there was
a way of sending a structure instead, so that I can open it in the
client side?


sentto() is not part of the C standard, so the question is probably
more appropriate to the unix programming newsgroup.
Generally speaking, a structure is a platform-dependant assembly
of the representation of values. If the client at the other end has
exactly the same representation of values and exactly the same padding
then you can move the bytes corresponding to the structure into
a buffer that is properly aligned for the platform, and then cast the
buffer pointer to become a structure pointer, and then use that.
Of course if the structure includes pointers, then the transported
pointers are unlikely to be valid at the other end.

If the other end does not have *exactly* the same representation of
values, then on one side or the other you would have to munge the values
to get them into a form that the other side can understand. If you
do not know in advance all the details of the other end, then the easiest
way to do this is to convert the structure values into a portable form,
send that portable form, and have the other end convert to an appropriate
local form.

Probably your operating system offers some useful library routines that are
part of the POSIX standard but not the C standard. I suggest you
look up xdr .
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
May 26 '06 #2
at*****@gmail.com wrote:

I'm currently using the function:
sendto(sockfd, message, strlen(message), 0,&client_addr, addr_size)

to sending a message back to a client, and I was wondering if there was
a way of sending a structure instead, so that I can open it in the
client side?


Well, sendto() isn't part of Standard C, but treating this the same
as "can I fwrite() to a file, take that file to another computer,
and then fread() it there"...

Yes, sort of, maybe.

If (and this is a big "if") the other computer uses the internal
representation of all struct members, has the same byte order and
structure padding, and the stuct contents are useful as-is (for
example, no pointers), then sending the struct as-is to the other
computer may "work".

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

May 26 '06 #3

<at*****@gmail.com> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
I'm currently using the function:
sendto(sockfd, message, strlen(message), 0,&client_addr, addr_size)

to sending a message back to a client, and I was wondering if there was
a way of sending a structure instead, so that I can open it in the
client side?
Thanks

Write a function

void *serialise(struct mystruct *ptr, int *N)

and another

struct mystruct *retrieve(void *serial)
This is sometimes impossible if the structure contains pointers to external
data. In that case you need to rethink your design.
The functions should be entirely portable and assume ASCII and 8-bit bytes.
So convert floating point numbers to text, send integers in big endian, and
so on. Then you know it will survive the passage ove the network.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
May 28 '06 #4
"Malcolm" <re*******@btinternet.com> writes:
[...]
Write a function

void *serialise(struct mystruct *ptr, int *N)

and another

struct mystruct *retrieve(void *serial)
This is sometimes impossible if the structure contains pointers to external
data. In that case you need to rethink your design.
The functions should be entirely portable and assume ASCII and 8-bit bytes.
So convert floating point numbers to text, send integers in big endian, and
so on. Then you know it will survive the passage ove the network.


If it assumes ASCII and 8-bit bytes, it's not entirely portable
(though it may well be portable enough for your purposes).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 28 '06 #5
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Malcolm" <re*******@btinternet.com> writes:
[...]
Write a function

void *serialise(struct mystruct *ptr, int *N)

and another

struct mystruct *retrieve(void *serial)
This is sometimes impossible if the structure contains pointers to
external data. In that case you need to rethink your design.
The functions should be entirely portable and assume ASCII and
8-bit bytes. So convert floating point numbers to text, send
integers in big endian, and so on. Then you know it will survive
the passage ove the network.


If it assumes ASCII and 8-bit bytes, it's not entirely portable
(though it may well be portable enough for your purposes).


Given the prototype he gave for sendto(), it's virtually certain that he's
referring to the POSIX (and Berkeley sockets) function of that name. Since
POSIX requires 8-bit bytes (and the Internet is defined in terms of octets),
that's not likely to be a problem. POSIX allows many character encodings
(unfortunately), but AFAIK all of the allowed ones are supersets of ASCII.

This still leaves problems with different sizes of types, endianness,
alignment/padding, and embedded pointers. The short answer, just like for
writing complex data types to files, is to convert everything to a specific
representation for communication between different systems.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin
*** Posted via a free Usenet account from http://www.teranews.com ***
May 29 '06 #6
In article <44***********************@free.teranews.com>,
Stephen Sprunk <st*****@sprunk.org> wrote:
Given the prototype he gave for sendto(), it's virtually certain that he's
referring to the POSIX (and Berkeley sockets) function of that name. Since
POSIX requires 8-bit bytes (and the Internet is defined in terms of octets),
that's not likely to be a problem. POSIX allows many character encodings
(unfortunately), but AFAIK all of the allowed ones are supersets of ASCII.
Citations?

ISO/IEC 9945-1: 1990 (aka IEEE Std 1003.1-1990 aka POSIX.1)

section 2.2.2.8 defines character as "a sequence of one or more
bytes representing a single graphic symbol", and specifically notes
the definition to be equivilent to C's multibyte character.

section 2.71 imports byte from ISO C without any restriction to
8 bit bytes.

Section 2.2.2.60 refers to the "portable filename character set"
(upper and lower case english letters together with the 10 digits, period,
dash, and underscore), but B.2.2.2 "General Terms" indicates,

portable filename character set: The encoding of this character set
is not specified -- specifically, ASCII is not required. But the
implementation must provide a unique character code for each of
the printable graphics specified by POSIX.1.

8.1.2.2 (Description of setlocale) indicates that,
The value "C" for locale specifies the minimal environment for
C-Language translation.

There's nothing there that would prevent the local "C" locale from using
EBCDIC as the encoding, nor that would prevent bytes from being 9 or
16 bits or whatever.

(and the Internet is defined in terms of octets),


No it isn't. In particular, ethernet frames are defined in terms
of bits. Look, for example, at IEEE 802's use of preamble (some of it
is allowed to be eaten or distorted along the way, as long as enough
is left to establish signal synchronization). See also how the
intraframe gap is defined and notice that the standards allow for it to
shrink.

If "the Internet" was defined in terms of octets, then a received packet
would have to start on a synchronized octet boundary, which is not the
case: many internet transports are asynchronous, with the initial
field of the preamble being used to sychronize the data clocks.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
May 29 '06 #7
"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.ca> wrote in message
news:e5**********@canopus.cc.umanitoba.ca...
In article <44***********************@free.teranews.com>,
Stephen Sprunk <st*****@sprunk.org> wrote:
Given the prototype he gave for sendto(), it's virtually certain that he's
referring to the POSIX (and Berkeley sockets) function of that name.
Since POSIX requires 8-bit bytes (and the Internet is defined in terms
of octets), that's not likely to be a problem. POSIX allows many
character encodings (unfortunately), but AFAIK all of the allowed
ones are supersets of ASCII.
Citations?

ISO/IEC 9945-1: 1990 (aka IEEE Std 1003.1-1990 aka POSIX.1)

.... There's nothing there that would prevent the local "C" locale from using
EBCDIC as the encoding, nor that would prevent bytes from being 9 or
16 bits or whatever.


I'd swear I've seen that stated, unchallenged, several times on clc; I never
looked at the spec myself. Perhaps it's assumed because all known
implementations of POSIX use 8-bit bytes and a superset of ASCII?
(and the Internet is defined in terms of octets),


No it isn't. In particular, ethernet frames are defined in terms
of bits. Look, for example, at IEEE 802's use of preamble (some of it
is allowed to be eaten or distorted along the way, as long as enough
is left to establish signal synchronization). See also how the
intraframe gap is defined and notice that the standards allow for it to
shrink.


I said the Internet, not Ethernet. The IEEE has nothing to do with the
Internet; that's the IETF's bailiwick. IP packets are defined as a sequence
of octets, and TCP sockets are defined to transport streams of octets.
While I'm sure it's possible to implement TCP/IP on top of systems or link
layers that have bytes of more than 8 bits, it's not easy. Ditto for
EBCDIC.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin
*** Posted via a free Usenet account from http://www.teranews.com ***
May 29 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Adam Balgach | last post by:
Greetings everyone, ive got a problem ive been working with for quite a while and need some help. ive got a structure: struct Data { char *data1; char *data2; int val1; int val2;
1
by: Intaek Lim | last post by:
I'm writing a simple path MTU discovery utility by modifying 'traceroute' source code in Win32 with MSVC7. I set DF bit in IP header and send packets filled with N bytes of data. When MTU size...
0
by: J.V. | last post by:
I want to make localhost on my browser a sendto destination, so I can right click on an ASP or PHP file, choose sendto, then localhost and have a browser open up with the ASP file running in it. ...
4
by: B.r.K.o.N.j.A. | last post by:
I've been having trouble with sendto function (I open a socket, bind it to eth0, recvfrom works fine but when I execute following line I get sendto(): invalid argument) if(sendto(sokit, buf,...
4
by: Just | last post by:
Hi everybody I was wondering if it is possible to use SendTo to send a whole IP package including header to another IP than specified in the package. Say I have a complete package and in it...
2
by: Christof Nordiek | last post by:
Hi all, I'm building an application, that should be started from the SendTo Menu. My Setup-Project puts a link into the SendTo Menu. But this works only for the User running the Setup. Even if...
0
by: | last post by:
hello I write a little program, that receive as parameters some filepaths and use them. The program will be "put" into SendTo menu and be available from RightClick->SendTo menu. The problem...
4
by: BlueJ | last post by:
My program is to send data using socket.. but I got the error message: "sendto(msg) points to uninitialised byte(s) " This is my source code 1. key_list = emalloc(BUFSIZE - sizeof(Key) -...
1
by: kavok | last post by:
I am writing a software that needs to sniff packets in the network (raw ethernet) and also, with another thread send regular UDP packets with the common socket API's. However, when the RAW...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.