473,597 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sending a struct over TCP

Hi, All,
is it possible to send a struct using the the send function. Here is
what I mean

typedef struct{
int ID;
char name[20];
}sampleStruct;

int main(){
sampleStruct a;
//lets assume we have filled out our sin_family and etc. so
we'll have something like:
sd = socket (AF_INET, SOCK_STREAM, 0);

connect (sd, (struct sockaddr*) &socketChann el, sizeof(struct
sockaddr);
a.ID = 1;
a.name="Bob";
send(sd, (void*) a, sizeof(a), 0);
...
...
...
}

Is there anyway to make this work or do I need to make a char buffer
and write

buffer[100] = "1 Bob";
send(sd, (void*) buffer, strlen(buffer), 0);

Thanks

Mar 27 '07 #1
6 26714
On Mar 27, 9:03 am, "Jack" <accpac...@hotm ail.comwrote:
Hi, All,
is it possible to send a struct using the the send function. Here is
what I mean

typedef struct{
int ID;
char name[20];

}sampleStruct;

int main(){
sampleStruct a;
//lets assume we have filled out our sin_family and etc. so
we'll have something like:
sd = socket (AF_INET, SOCK_STREAM, 0);

connect (sd, (struct sockaddr*) &socketChann el, sizeof(struct
sockaddr);

a.ID = 1;
a.name="Bob";
send(sd, (void*) a, sizeof(a), 0);
..
..
..

}

Is there anyway to make this work or do I need to make a char buffer
and write

buffer[100] = "1 Bob";
send(sd, (void*) buffer, strlen(buffer), 0);

Thanks
Dear Jack,

You can send it using the same send function without having another
char buffer.
since a is an object of your struct you need to give the address of
it.
i.e
send(sd, (void*)&a, sizeof(a), 0); will resolve your issue.

One thing you've to notice while dealing with structure variables.

the size of the structure will be aligned according the pack size you
are specifying. Normally the default alignment for the structure
members will be 4 bytes in size.

i.e if you declare

struct MyStruct
{
int a;
char buff[21]; //Oops I meant 20 letters for name and extra one
byte for null character
};

You might be expecting a size 25 bytes for this structure but if you
are giving alignment bytes 4, the size of the struct will be 28 bytes.
(24 for buff and 4 for variable a)

As per your code you will be sending some extra bytes because of the
alignment and you've to make sure that, at the client side too, you
are having same structure alignment. else you may face some unexpected
behavior.

You can specify the alignment using "#pragma pack" directive

#pragma pack(1) // alignment will be 1 byte you will get the exact
size of members
#pragma pack(2) // alignment will be 2 byte members will be aligned to
2 bytes
#pragma pack(4) // alignment will be 4 byte members will be aligned to
4 bytes
#pragma pack(8) // alignment will be 8 byte members will be aligned to
8 bytes

Normally this will be set to the wordlength of the processsor. i.e 4
bytes if you are using 32 bit architecture or 8 bytes if you are using
64 bit architecture.
and for strict size information we will use 1 byte alignment.

Mar 27 '07 #2
Jack wrote:
Hi, All,
is it possible to send a struct using the the send function. Here is
what I mean

typedef struct{
int ID;
char name[20];
}sampleStruct;

int main(){
sampleStruct a;
//lets assume we have filled out our sin_family and etc. so
we'll have something like:
sd = socket (AF_INET, SOCK_STREAM, 0);

connect (sd, (struct sockaddr*) &socketChann el, sizeof(struct
sockaddr);
a.ID = 1;
a.name="Bob";
send(sd, (void*) a, sizeof(a), 0);
..
..
..
}

Is there anyway to make this work or do I need to make a char buffer
and write

buffer[100] = "1 Bob";
send(sd, (void*) buffer, strlen(buffer), 0);

Thanks
You can send any bytes you want. It's up to your send and recv code to
deal with what the bytes mean. Note that you need the '&'

send(sd, (void*)&a, sizeof(a), 0);

--
Scott McPhillips [VC++ MVP]

Mar 27 '07 #3
On Mar 27, 8:03 am, "Jack" <accpac...@hotm ail.comwrote:
Hi, All,
is it possible to send a struct using the the send function. Here is
what I mean

typedef struct{
int ID;
char name[20];

}sampleStruct;

int main(){
sampleStruct a;
//lets assume we have filled out our sin_family and etc. so
we'll have something like:
sd = socket (AF_INET, SOCK_STREAM, 0);

connect (sd, (struct sockaddr*) &socketChann el, sizeof(struct
sockaddr);

a.ID = 1;
a.name="Bob";
send(sd, (void*) a, sizeof(a), 0);
..
..
..

}

Is there anyway to make this work or do I need to make a char buffer
and write

buffer[100] = "1 Bob";
send(sd, (void*) buffer, strlen(buffer), 0);

Thanks
of course,structur e object just a seriate memory buffer, the same as
array of char or other, so you can using send() function to send the
structure(memor y)

Mar 27 '07 #4
Hello, Jack.

Don't forget only that different architectures has different
endienless (little and big)
So, when on the other side you receive a packet ()

recv(sd, (void*) &a, sizeof(a), 0);

the a.id maybe not 1(0x00000001), but (0x01000000)

Good Day
--
Alexander Pazdnikov

Mar 27 '07 #5
And if your struct has pointers to other types of data you must
consider another way of sending your struct.

For ex.

struct tag_MyStruct {
int a, b;
fooStruct *pFoo;
};

You will need to traverse those pointers and write the data to a
single stream of bytes to send() the struct.

Mar 27 '07 #6
On Mar 27, 12:54 pm, "Scott McPhillips [MVP]" <org-dot-mvps-at-
scottmcpwrote:
send(sd, (void*)&a, sizeof(a), 0);
You never need to cast to void* (unless it is corresponding
to the ... part of a variadic function).

It's good programming practice to avoid using casts unless
absolutely necessary, and in this case the cast is worse than
useless.

Also, 'send', 'connect', and 'socket' are not standard C++
functions; OP would be better off posting to comp.unix.progr ammer.
Mar 27 '07 #7

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

Similar topics

4
2368
by: Leo | last post by:
Hello, I have a C dll with a method signature of: int activate(datastruct *data) where datastruct is defined as: typedef struct datastruct { long result;
2
2521
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I am really desperate now, because I havent' been able to locate the origin of the problem for a couple of days now.. PROBLEM: the message digest obtained differs each time I execute the code, but works perfectly when applying the "control", that...
4
5392
by: Abubakar | last post by:
Hi, I am writing a server in C# and client in C++ (pure, not managed). The communication goes on through sockets. In C# I am using NetworkStream to send text data (by converting it to byte array) to the c++ client. In c++ client I have "recv" (winsock) function through which I receive everything. Now there is a need to pass a "struct" of C# through the "write" function of network stream. I want to know how this can be done. Also I would...
9
17974
by: thorley | last post by:
Greetings, since there was no reponse to my previous post about an existing FastCGI server in python, I've taken to writing my own. (which of course I'll share--*if* there's something to share ;) My problem now, is that I need to send certain binary data over a socket. That is, I want to make some bytes, and stuff them in a TCP packet, send them down the pipe, and then listen for a response. socket.send, as best I can tell, will only...
1
2169
by: simu | last post by:
I've a problem with sending a structure of the type pkt over my connected socket. I've not too much experience in programming C/C++. Thus, I've no clue what the problem could be. I've spent quite a lot of time in this but didn't got the problem. I'm using sendmsg() and recvmsg(). Both seems to work. But when I try to access the received data, I recognize that the results are completely wrong (probably initial values instead of received...
2
2643
by: Sean | last post by:
Hi guys, I have a question. I am trying to write a simple echo program. The objective is to read in a file in chuncks into the buffer and transfer the buffer over the socket. We repeat this until the entire file is transfered. The problem that I have right now is that I can only read the data to the buffer one time and one time only. So for instance if my file is 1 MG. I can only read the first 256 char which is the size of my buffer.
8
4088
by: Thomas Dybdahl Ahle | last post by:
Hi, I've writing a python application in which I'd like to have a small "ping label", to always tell the current ping time to the server. It seems however that I have to be root to send those imcp packages, but I guess there must be a workaround since I can easily use the "ping" command as ordinary user. Do anybody know how to do this in python?
2
4561
by: Jack | last post by:
Hi all, I have a problem. I am writing a client server app in C. In my client program, I have a simlple struct similiar to: struct mySt{ int id; char val; struct mySt *next; };
4
1486
by: =?Utf-8?B?RWl0YW4=?= | last post by:
Hello, I need to send a "struct" to an embeded CPU over the Ethernet. The struct is defined like this: public struct MyStruct { public double dVar1;
0
7886
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
8272
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...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8258
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
6688
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...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3886
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...
1
2404
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.