Networking Forums

Networking Forums > Computer Networking > Linux Networking > How can I send a struct containing a char * with a socket?

Reply
Thread Tools Display Modes

How can I send a struct containing a char * with a socket?

 
 
Patrick Lam
Guest
Posts: n/a

 
      04-03-2006, 04:20 PM
Dear all,

I have a struct like this:

struct sample{
int a;
int b;
char* c;
struct sample* next;
};

struct sample* data_sample;

I wanted to send it to a server using a write statement:

data_sample = (struct sample *)malloc(sizeof(struct sample));
strcpy(data_sample->c, "testing");
data_sample->a = 1;
data_sample->b = 2;
...............
n = write(sockfd, data_sample, sizeof(data_sample));
..................

I notice that the string inside the struct is not being sent over to the
server while everything else is OK. I have to use a char* instead of an
array because the length of c is supposed to be an input argument.

So am I not able to send such a struct with a socket at all? If not so,
what have I done wrong?

Thanks so very much in advance.

Regards,

Patrick


 
Reply With Quote
 
 
 
 
Tauno Voipio
Guest
Posts: n/a

 
      04-03-2006, 04:51 PM
Patrick Lam wrote:
> Dear all,
>
> I have a struct like this:
>
> struct sample{
> int a;
> int b;
> char* c;
> struct sample* next;
> };
>
> struct sample* data_sample;
>
> I wanted to send it to a server using a write statement:
>
> data_sample = (struct sample *)malloc(sizeof(struct sample));
> strcpy(data_sample->c, "testing");
> data_sample->a = 1;
> data_sample->b = 2;
> ..............
> n = write(sockfd, data_sample, sizeof(data_sample));
> .................
>
> I notice that the string inside the struct is not being sent over to the
> server while everything else is OK. I have to use a char* instead of an
> array because the length of c is supposed to be an input argument.
>
> So am I not able to send such a struct with a socket at all? If not so,
> what have I done wrong?



If you transfer the pointer, you're going to tell
the receiving host where in the sending host's
process memory the string is.

You need to send the string itself, either inside
the structure or as a separate entity.

The link pointer is as well invalid for the receiver.

--

Tauno Voipio
tauno voipio (at) iki fi
 
Reply With Quote
 
Patrick Lam
Guest
Posts: n/a

 
      04-03-2006, 04:59 PM
Hi Tauno,

Thanks very much for your suggestion.

I thought about sending the string separately, but then I would have to
reassemble the struct at the receiver, right?

Is this a normal way to do such a thing though?

Thanks again.

Regards,

Patrick

"Tauno Voipio" <(E-Mail Removed)> wrote in message
news:zocYf.202$(E-Mail Removed)...
> Patrick Lam wrote:
>> Dear all,
>>
>> I have a struct like this:
>>
>> struct sample{
>> int a;
>> int b;
>> char* c;
>> struct sample* next;
>> };
>>
>> struct sample* data_sample;
>>
>> I wanted to send it to a server using a write statement:
>>
>> data_sample = (struct sample *)malloc(sizeof(struct sample));
>> strcpy(data_sample->c, "testing");
>> data_sample->a = 1;
>> data_sample->b = 2;
>> ..............
>> n = write(sockfd, data_sample, sizeof(data_sample));
>> .................
>>
>> I notice that the string inside the struct is not being sent over to the
>> server while everything else is OK. I have to use a char* instead of an
>> array because the length of c is supposed to be an input argument.
>>
>> So am I not able to send such a struct with a socket at all? If not so,
>> what have I done wrong?

>
>
> If you transfer the pointer, you're going to tell
> the receiving host where in the sending host's
> process memory the string is.
>
> You need to send the string itself, either inside
> the structure or as a separate entity.
>
> The link pointer is as well invalid for the receiver.
>
> --
>
> Tauno Voipio
> tauno voipio (at) iki fi



 
Reply With Quote
 
Joe Beanfish
Guest
Posts: n/a

 
      04-03-2006, 06:33 PM
On Mon, 03 Apr 2006 12:59:07 -0400, Patrick Lam <yahoo_mail@nospam_tom.com> wrote:
> "Tauno Voipio" <(E-Mail Removed)> wrote in message
> news:zocYf.202$(E-Mail Removed)...
>> Patrick Lam wrote:
>>> Dear all,
>>>
>>> I have a struct like this:
>>>
>>> struct sample{
>>> int a;
>>> int b;
>>> char* c;
>>> struct sample* next;
>>> };
>>>
>>> struct sample* data_sample;
>>>
>>> I wanted to send it to a server using a write statement:
>>>
>>> data_sample = (struct sample *)malloc(sizeof(struct sample));
>>> strcpy(data_sample->c, "testing");
>>> data_sample->a = 1;
>>> data_sample->b = 2;
>>> ..............
>>> n = write(sockfd, data_sample, sizeof(data_sample));
>>> .................
>>>
>>> I notice that the string inside the struct is not being sent over to the
>>> server while everything else is OK. I have to use a char* instead of an
>>> array because the length of c is supposed to be an input argument.
>>>
>>> So am I not able to send such a struct with a socket at all? If not so,
>>> what have I done wrong?

>>
>>
>> If you transfer the pointer, you're going to tell
>> the receiving host where in the sending host's
>> process memory the string is.
>>
>> You need to send the string itself, either inside
>> the structure or as a separate entity.
>>
>> The link pointer is as well invalid for the receiver.

> Hi Tauno,
>
> Thanks very much for your suggestion.
>
> I thought about sending the string separately, but then I would have to
> reassemble the struct at the receiver, right?
>
> Is this a normal way to do such a thing though?


That's Normal. A packet would usually consist of some kind of size
indicator followed by that much data.

I'm sure someone will rant at me for this but you could play some
memory games with the struct.

struct sample{
int a;
int b;
struct sample* next;
size_t clen;
char c[1];
};

Then allocate the structure to sizeof struct+strlen data and put the data
into c[] as if it was the proper sized array. Keep the len of c in clen.
You can then send it with one write. The receiver will still have to do 2
reads to get the struct to know how much to allocate. Then get the string.
 
Reply With Quote
 
Joe Beanfish
Guest
Posts: n/a

 
      04-03-2006, 06:54 PM
On Mon, 03 Apr 2006 14:33:00 -0400, Joe Beanfish <(E-Mail Removed)> wrote:

> On Mon, 03 Apr 2006 12:59:07 -0400, Patrick Lam <yahoo_mail@nospam_tom.com> wrote:
>> "Tauno Voipio" <(E-Mail Removed)> wrote in message
>> news:zocYf.202$(E-Mail Removed)...
>>> Patrick Lam wrote:
>>>> Dear all,
>>>>
>>>> I have a struct like this:
>>>>
>>>> struct sample{
>>>> int a;
>>>> int b;
>>>> char* c;
>>>> struct sample* next;
>>>> };
>>>>
>>>> struct sample* data_sample;
>>>>
>>>> I wanted to send it to a server using a write statement:
>>>>
>>>> data_sample = (struct sample *)malloc(sizeof(struct sample));
>>>> strcpy(data_sample->c, "testing");
>>>> data_sample->a = 1;
>>>> data_sample->b = 2;
>>>> ..............
>>>> n = write(sockfd, data_sample, sizeof(data_sample));
>>>> .................
>>>>
>>>> I notice that the string inside the struct is not being sent over to the
>>>> server while everything else is OK. I have to use a char* instead of an
>>>> array because the length of c is supposed to be an input argument.
>>>>
>>>> So am I not able to send such a struct with a socket at all? If not so,
>>>> what have I done wrong?
>>>
>>>
>>> If you transfer the pointer, you're going to tell
>>> the receiving host where in the sending host's
>>> process memory the string is.
>>>
>>> You need to send the string itself, either inside
>>> the structure or as a separate entity.
>>>
>>> The link pointer is as well invalid for the receiver.

>> Hi Tauno,
>>
>> Thanks very much for your suggestion.
>>
>> I thought about sending the string separately, but then I would have to
>> reassemble the struct at the receiver, right?
>>
>> Is this a normal way to do such a thing though?

>
> That's Normal. A packet would usually consist of some kind of size
> indicator followed by that much data.
>
> I'm sure someone will rant at me for this but you could play some
> memory games with the struct.
>
> struct sample{
> int a;
> int b;
> struct sample* next;
> size_t clen;
> char c[1];
> };
>
> Then allocate the structure to sizeof struct+strlen data and put the data
> into c[] as if it was the proper sized array. Keep the len of c in clen.
> You can then send it with one write. The receiver will still have to do 2
> reads to get the struct to know how much to allocate. Then get the string.
>


p.s.
Just to be clear, writing and reading structures directly isn't remotely
portable. The sender and receiver must be using same data type sizes,
endianness, and structure packing.
 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a

 
      04-03-2006, 08:54 PM

Patrick Lam wrote:

[]

> So am I not able to send such a struct with a socket at all? If not so,
> what have I done wrong?


You might like learning about serialization:
http://boost.org/libs/serialization/doc/index.html

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(socket) send transmits data on same packet alexia.bee@gmail.com Linux Networking 2 05-29-2008 07:02 PM
C/C++ socket send causes program to exit Bjorn Nuyttens Linux Networking 2 07-17-2007 08:53 AM
Purging socket send buffers Markus Linux Networking 1 06-20-2007 07:17 PM
How to send a tcp packet to same machine from a kernel module by creating a sk_buff struct will_u_tellmemore Linux Networking 0 01-11-2007 12:45 PM
Sending a struct over a UDP socket... Adam Balgach Linux Networking 1 09-17-2004 07:36 PM



1 2 3 4 5 6 7 8 9 10 11