0

I'm trying to send the structure via UDP socket, I received the value of DRB_count correct but unable to receive the value of KenbStar. What am I doing wrong? I'm using same machine, loop back ip 127.0.01 in client and server with same port.

client:

typedef struct tseTargetCellInformation{
   UInt8 DRB_count;                     
   UInt8 *KenbStar;
}tTargetCellConfiguration;

trecTargetCellConfiguration *rx_TargetCellConfiguration_str;

rx_TargetCellConfiguration_str = (trecTargetCellConfiguration*)malloc(sizeof(trecTargetCellConfiguration));

send_TargetCellConfiguration_str->DRB_count=1;
send_TargetCellConfiguration_str->KenbStar = (UInt8*) malloc(1);
send_TargetCellConfiguration_str->KenbStar[0]= 0x5b;

sendto(sd, (char *) (send_TargetCellConfiguration_str), sizeof(tTargetCellConfiguration), 0, (struct sockaddr *)&server, slen)

server:

typedef struct tseTargetCellInformation{
   UInt8 DRB_count;                     
   UInt8 *KenbStar;
}tTargetCellConfiguration;

rx_TargetCellConfiguration_str->KenbStar = (UInt8*) malloc(1);

recvfrom(sd, (char *) (rx_TargetCellConfiguration_str), sizeof(trecTargetCellConfiguration), 0, (struct sockaddr*) &client, &client_length);
Till Helge
  • 9,253
  • 2
  • 40
  • 56

2 Answers2

3

Because KenbStar is a pointer, you have to dereference in order to send the value that it points to, or receive the value. Otherwise you are just sending and receiving the pointer (i.e., not the contents pointed to), which usually makes no sense at all (especially if the client and the server are different processes).

In other words, something like:

sendto(sd, (char *) send_TargetCellConfiguration_str->KenbStar, sizeof(UInt8), ...

and

recvfrom(sd, (char *) rx_TargetCellConfiguration_str->KenbStar, sizeof(UInt8), ...

However, probably it would be easiest to make KenbStar a regular member, just like DRB_count, unless you have a specific reason why it must be a pointer. Then you can just send (and receive) the whole struct with a single call.

Community
  • 1
  • 1
Reunanen
  • 7,921
  • 2
  • 35
  • 57
  • thanks , it worked , but then only for pointer variable, what about the rest of variables in the structure . I have following str to send via UDP. what should i do ? – mohsin AHMED Mar 01 '13 at 13:13
  • If your values are scattered in memory (i.e., not consecutive), then you need several sendto calls. Normally, it is better to collect all the data in a contiguous block (a single struct with values only, i.e. no pointers) and then send (and receive) with a single call. – Reunanen Mar 01 '13 at 13:21
0

You can't send a pointer into one memory space to another memory space and expect it to point to the same thing, especially when you haven't sent what it pointed to. Sending unencoded structs across the wire is a no-no for about ten other reasons as well. You need to investigate some presentation layers such as XDR.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • can u send me some link where i can find this topic, i think u r talking about encoding the str and then sending it on socket and then decode on other side – mohsin AHMED Mar 01 '13 at 15:10
  • You can Google XDR as well as I can, and that should lead you to several more modern alternatives. – user207421 Mar 01 '13 at 22:11
  • I tried XDR library to encode the message and then decode it through sockets but at decoding side I get some unwanted characters then my decoded message. I doing like this: – mohsin AHMED Mar 05 '13 at 09:31
  • char buffer[34] = "This is a message from the client"; xdrmem_create(&xdrs, buffer, 34, XDR_ENCODE); xdrmem_create(&xdrs, buffer1, 34, XDR_DECODE); printf("buffer::%s\n",buffer1); – mohsin AHMED Mar 05 '13 at 09:32