recvfrom trouble bugging me for days

Hi,

I am creating an UDP client for an UDP server. My UDP client sends some information like id, name and age, etc and the UDP server saves that information in an array. When the client wants to retrieve information, it sends the id, and the server replies back with name and age. Fun stuff.

I am using two structs. I created one structure for the datagram that carries the information. Code for the datagram is as follows:
1
2
3
4
5
typedef struct {
        TYPE type;      // Type of this datagram
        unsigned int seq;       // Sequence number of this datagram
        char data[ MAX_DATA ];  // The data itself (if any).
} Datagram;


The second structure is my data record structure that carries the information such as name, id, age, and command (for adding or retrieving from server). Below is the code:
1
2
3
4
5
6
7
struct Data_Record {
  int command;    // 0 for add, 1 for retrieve
                  // 0 for sucess, 1 for failure
  int id;         // sequence number
  char name[32];  // Name
  int age;        // Age
};


So basically the plan is to send seq number, data type (ACK, SYN, FIN) in datagram while, passing the Data_Record into datagram.data and send the datagram to the server which pulls information from datagram.data.

While sending data to server, I can see my program working properly (the server shows me the data I send which matches up). My code while sending is as follows:

datasend = datagram,
dr = data_record
1
2
3
4
5
datasend.seq = seq;
datasend.type = DATA;
memcpy(datasend.data, &dr, sizeof(dr));
sendto( s, (char *)&datasend, sizesend, 0,
    (struct sockaddr *)&server, sizeof(server));


But while receiving information from the server, my program doesn't seem to get the right information back. The code for receiving is as follows:
1
2
3
4
5
6
 
int from_len = sizeof(server);
int sizereceive = sizeof(datasend);
serversays = recvfrom(s, (char *)&datasend, sizereceive, 0,
                        (struct sockaddr *)&server, &from_len);
memcpy(&dr, datasend.data, sizeof(datasend.data));


Now, when I want to retrieve information, I enter the id that I want to retrieve and the server sends me that information corresponding to the ID. So, the data already exists with id = 1, name = tom, age = 22, and when I want to retrieve, I send the ID to be retrieved as "1" and server should send me "tom" and "22". But in my case, when I enter the id, for example, "1", the server on it's console displays the right information "1, tom, 22", but the client shows me as id = "1", name = blank, age = blank. Now, if I persistently ask for id 1 two more times, only then it shows me "1, tom, 22" in 3rd attempt to retrieve the same ID.

Can someone tell me what I am doing wrong? Is something wrong with my recvfrom code?
1
2
//memcpy(&dr, datasend.data, sizeof(datasend.data));//sizeof(datasend.data) == MAX_DATA
memcpy(&dr, datasend.data, sizeof(dr));
Last edited on
Thanks, but unfortunately that didn't work. Here's a sample output that I have been getting. Notice how I have to retrieve id "1" three times to get the right value. It doesn't make any sense.

Client side:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Enter command (0 for Add, 1 for Retrieve, 2 to quit): 0
Enter id (integer): 1
Enter name (up to 32 char): Tom
Enter age (integer): 20

>> Sending ADD data
>> Sequence number...........0

<< Received data from server
<< Received Sequence number...... 0
<< Received &datasend.data: ffbfeb84

Enter command (0 for Add, 1 for Retrieve, 2 to quit): 1
Enter id (integer): 1
Sequence number........1
<< Receiving Data from server
<< Receieved Sequence number......0
<< Received &Datasend.data: ffbfddd4
<< Received &dr - data record: ffbfdb6c

Data inside the datasend.data converted to data record
ID: 1
Name in dr.name:
Age in dr.age: 0

Enter command (0 for Add, 1 for Retrieve, 2 to quit): 1
Enter id (integer): 1
Sequence number........2
<< Receiving Data from server
<< Received &Datasend.data: ffbfd0ec
<< Received &dr - data record: ffbfce84

Data inside the datasend.data converted to data record
ID: 1
Name in dr.name:
Age in dr.age: 0

Enter command (0 for Add, 1 for Retrieve, 2 to quit): 1
Enter id (integer): 1
Sequence number........3
<< Receiving Data from server
<< Received &Datasend.data: ffbfc404
<< Received &dr - data record: ffbfc19c

Data inside the datasend.data converted to data record
ID: 1
Name in dr.name: Tom
Age in dr.age: 20


Server side:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Command is 0
Id is 1
Name is Tom
Age is 20
Size of the Map : 1
Command is 1
Id is 1
Name is Tom
Age is 20
---------------------------------------
Command is 1
Id is 1
Name is Tom
Age is 20
---------------------------------------
Command is 1
Id is 1
Name is Tom
Age is 20
Topic archived. No new replies allowed.