473,804 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="t his is test1";
myData.data2="t his 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(GatewayA ddr)) != sizeof(myData))
DieWithError("s endto() 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*)m alloc(sizeof(my Data)+1);
memcpy(toSend,& myData,sizeof(m yData));
toSend[sizeof(myData)]='\0';

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

if (sendto(sock, toSend, sizeof(myData), 0, (struct sockaddr *)
&GatewayAddr , sizeof(GatewayA ddr)) != sizeof(myData))
DieWithError("s endto() 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 7649
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.c om> wrote in message
news:a5******** *************** ***@posting.goo gle.com...
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="t his is test1";
myData.data2="t his 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(GatewayA ddr)) != sizeof(myData))
DieWithError("s endto() 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*)m alloc(sizeof(my Data)+1);
memcpy(toSend,& myData,sizeof(m yData));
toSend[sizeof(myData)]='\0';

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

if (sendto(sock, toSend, sizeof(myData), 0, (struct sockaddr *)
&GatewayAddr , sizeof(GatewayA ddr)) != sizeof(myData))
DieWithError("s endto() 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.co m (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="t his is test1";
myData.data2="t his 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*)m alloc(sizeof(my Data)+1);
memcpy(toSend,& myData,sizeof(m yData));
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.l earn.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
34384
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 the actual function call. I will post the code below of the structure, the prototype and and function call and if someone can explain this I would be very appreciative: struct data { float amount; string fname;
4
7704
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 simple, but its just not popping into my mind at the moment. My little snippet of code is below. Basically, the studentID array is dynamic so it will fit any length of a Student's Name. What I'm trying to do is place this chunk of code into a...
2
1971
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 to get bogged down. Do the arrays previously passed to the function stay memory resident? If not, what's causing it and what can I do to correct it? Thanks, Dave
3
3333
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 library like this: double VBEXPORT aigIGApproximation( double intervalCount,
2
3348
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 and i want to insert a row #pragma pack(push, 8) struct _EuroWhite{ char Panz; si32 Extra; char IntDex;
2
1974
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'), (2,'B') Thank
3
1765
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. Next I have a class, and I want to pass a structure member from my array of Structures to the class constructor. In the form I instantiate a class object. Module1
2
3003
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 have tried numerous variations on the theme;
5
3088
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 10.2) on an i686 intell processor. its my first attempt to convert a fileIO procedure, to a pthread so it won't stall the execution of the code. im going through an online tutorial
6
2940
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: "XXhYYm" byte hrs, mins ; char time ; // global scratch variable txt4 is also char hrs = decimin / (60 * 10) ; // convert 1/10s of a minute into hours mins = decimin % (60 * 10) ; // remainder of same division is minutes ...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10351
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9174
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6866
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.