Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sending and receiving 2D array using UDP

Posted on 2003-11-04
10
Medium Priority
?
462 Views
Last Modified: 2010-04-15
I am trying to send and recieve a 5x5 int array.

In my code I have for sending(UDP) int distvector[5][5]:

        remoteServAddr.sin_port = htons(5000+i*1000);
        rc = sendto(sd, &distvector, sizeof((int)distvector[5][5]), 0,
        (struct sockaddr *) &remoteServAddr,
        sizeof(remoteServAddr));


And for recieving I have (UDP) int recv_DV[5][5]:

        recvfrom(sd, &recv_DV, sizeof((int)recv_DV[5][5]), 0,
         (struct sockaddr *) &cliAddr, &cliLen);

i dont recieve the right array, recv_DV[0][0] has an odd number in it (I suppose its the array's address) and the rest of the array is '0'.  Also in the sendto() function I used distvector instead of &distvector but still the same result.

ne help would be much appreciated.

thnxs
0
Comment
Question by:imranqurai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
10 Comments
 
LVL 22

Accepted Solution

by:
grg99 earned 2000 total points
ID: 9684022
your size expression "sizeof((int)distvector[5][5]), " is probably computing the size of an integer.

Try making a new variable:  Length = sizeof( distvector ), print out it's value to be sure, and use it in the send and recv calls.
That should work a heck of a lot better.

And you do realize that UDP isnt guaranteed to deliver anything at all, and no guarantee what order packets arrive?

0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9684198
-----> rc = sendto(sd, &distvector, sizeof((int)distvector[5][5]), 0,
        (struct sockaddr *) &remoteServAddr,
        sizeof(remoteServAddr));


U are using sizeof ( (int) distvector[5][5] ) ---.> This would give u the size of the last element in your array i.e. the size of int on your machine

For getting the size of whole array, u should use         sizeof ( distvector )
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9684203
Same comment above applies to recvfrom also
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9684246
Also I don't think you would need to pass &distvector in sendto OR recvfrom

both these functions expect a void pointer *. So U can just pass szDataFromUdp as the second parameter to sendto and recvfrom.

Basically the second parameter expects the starting address of the data to be passed and the amount of data passed is governed by the next parameter.

Thus only using szDataFromUdp  as second parameter would give u the base address of the data and sizeof( szDataFromUdp ) would give u the amount of data to be passed
0
 
LVL 6

Expert Comment

by:Ajar
ID: 9684401
Your code looks clean and should work .. just a suggestion calculate the length before hand and use that in send and recv functions like

//for sending
int distvector[5][5],len;
len = sizeof(distvector);
sendto(sd,&distvector,len,0,(struct sockaddr *) &remoteServAddr,sizeof(remoteServAddr);

// for receiving
int recv_DV[5][5],len;
len = sizeof(recv_DV);
recvfrom(sd, &recv_DV, len, 0,(struct sockaddr *) &cliAddr, &cliLen);
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9684435
Hi Ajar,

I think, as I already posted, the second parameter shoudl be distvector and NOT &distvector

Your code would pass the len amount of data starting from ADDRESS of distvector


0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9684478
Sorry Guys,

I am really sorry

Did not come to mind that distvector and &distvector would be same
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question