473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Passing a structure of arrays to a socket...

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;
};

int main() {

Data myData;
myData.data1="this is test1";
myData.data2="this is test2";
myData.val1=23;
myData.val2=78;
now lets assume i have all the code correctly to open and bind a
datagram packet (UDP) now how do i send this structures values over
the socket...

ive been using this code:

if (sendto(sock, (char *)&myData, sizeof(myData), 0, (struct sockaddr
*)
&GatewayAddr, sizeof(GatewayAddr)) != sizeof(myData))
DieWithError("sendto() sent a different number of bytes than
expected");

assuming sock is an int already opened, and GatewayAddr is a structure
holding the address of the server.

why isnt this working?

ive also tried to manually turn the structure into a char* stream by
doing:

char *toSend;
toSend=(char*)malloc(sizeof(myData)+1);
memcpy(toSend,&myData,sizeof(myData));
toSend[sizeof(myData)]='\0';

printf("Packet: %s\n",toSend); //Displays nothing but 'Packet: '

if (sendto(sock, toSend, sizeof(myData), 0, (struct sockaddr *)
&GatewayAddr, sizeof(GatewayAddr)) != sizeof(myData))
DieWithError("sendto() sent a different number of bytes than
expected");

which of course also doesnt work.

Does anyone know how i can accomplish this seemingly simple task?

Thanks so much.

Cheers,
Adam.
Jul 22 '05 #1
2 7587
In the structure "struct Data", both structure member "data1" and "data2"
actually defined as char pointer, when you trying to send via UDP, you only
send pointers, not the data which the pointer pointing to.
the overall size of the structure on 32 bytes, which makes ur DieWithError
occurred.

2 ways to solve ur problem :

1. change ur structure becomes similiar to this :
struct Data {
char data1[255];
char data2[255];
int val1;
int val2;
};

2. You still can use back the original struct, but b4 you send via UDP, you
got to construct into a "buffer" b4 sending out.

cheers.

"Adam Balgach" <ff**@hotmail.com> wrote in message
news:a5**************************@posting.google.c om...
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;
};

int main() {

Data myData;
myData.data1="this is test1";
myData.data2="this is test2";
myData.val1=23;
myData.val2=78;
now lets assume i have all the code correctly to open and bind a
datagram packet (UDP) now how do i send this structures values over
the socket...

ive been using this code:

if (sendto(sock, (char *)&myData, sizeof(myData), 0, (struct sockaddr
*)
&GatewayAddr, sizeof(GatewayAddr)) != sizeof(myData))
DieWithError("sendto() sent a different number of bytes than
expected");

assuming sock is an int already opened, and GatewayAddr is a structure
holding the address of the server.

why isnt this working?

ive also tried to manually turn the structure into a char* stream by
doing:

char *toSend;
toSend=(char*)malloc(sizeof(myData)+1);
memcpy(toSend,&myData,sizeof(myData));
toSend[sizeof(myData)]='\0';

printf("Packet: %s\n",toSend); //Displays nothing but 'Packet: '

if (sendto(sock, toSend, sizeof(myData), 0, (struct sockaddr *)
&GatewayAddr, sizeof(GatewayAddr)) != sizeof(myData))
DieWithError("sendto() sent a different number of bytes than
expected");

which of course also doesnt work.

Does anyone know how i can accomplish this seemingly simple task?

Thanks so much.

Cheers,
Adam.

Jul 22 '05 #2
On 17 Sep 2004 18:55:21 -0700, ff**@hotmail.com (Adam Balgach) wrote
in comp.lang.c++:

Note that issues about sockets in general are off-topic here and
belong in a group that supports your particular compiler/OS
combination because standard C++ has no built-in support for any sort
of networking at all.

But in this case, at least part of your problem is C++ related.
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;
};

int main() {

Data myData;
myData.data1="this is test1";
myData.data2="this is test2";
At this point, your struct contains the addresses of two string
literals. Those pointer values are almost certainly meaningless to
the receiver.

Just as when storing internal data into an ordinary file, the values
of pointers, that is the addresses of some data items, it not a useful
thing to store. You need to send the data that those pointers point
to, not the contents of the pointers themselves, which are the
addresses of the characters.
myData.val1=23;
myData.val2=78;
[snip off-topic socket stuff]
why isnt this working?

ive also tried to manually turn the structure into a char* stream by
doing:

char *toSend;
toSend=(char*)malloc(sizeof(myData)+1);
memcpy(toSend,&myData,sizeof(myData));
toSend[sizeof(myData)]='\0';
[snip more off-topic socket stuff]
which of course also doesnt work.
Because the characters that you have copied into the allocated array
still contains the contents of the pointers, that is the addresses of
the character string literals, not the actual characters.
Does anyone know how i can accomplish this seemingly simple task?

Thanks so much.

Cheers,
Adam.


You need to change your structure definition to contain arrays of
char, and copy the chars into the arrays, perhaps with std::strcpy.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #3

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

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
2
by: dave.harper | last post by:
I'm relatively new to C++, but have a question regarding functions and arrays. I'm passing a relatively large array to a function several thousand times during the course of a loop, and it seems...
3
by: Sascha Herpers | last post by:
Hi, I wrote a c dll with a type library to use it in vb. No problem, everything works fine. Now I needed to pass an array of type double to the dll. I defined the function in the type...
2
by: Malkocoglu | last post by:
I have a legacy DLL that i may have to use from C# The problem with this DLL is that some functions take void * as arguments Let me illustrate it in C/C++ first , let's say i have a database table...
2
by: David Stephen | last post by:
If you define a structur Private Structure Str Dim X as Intege Dim Y as Strin End Structur Is there a way to populate the structre when you define the array Dim XX() as StrX = Str((1,'A'),...
3
by: Steve | last post by:
Hello, I created a public Structure in a Standard Module and also an array of Structures. Then I load data into the array of structures in a public sub that I call on the Form load event. ...
2
by: Steve | last post by:
I have defined a structure of; Private Structure Menu Private MenuID as string Private Label as string End Structure ....and I would like to create and initialize an array of type "Menu". I...
5
by: nass | last post by:
this is a thought experiment. i do not have the time to implement it and test it to see if it works so i am relying on your good will:) thank you in advance im on a linux machine (slackware...
6
by: nigelmercier | last post by:
I'm still not comfortable with passing character arrays, and I think this is the problem with this code: char * formatHours(int decimin) { // convert integer deci-mins (6 seconds) to string:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.