Thread: send 2D array using UDP

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    send 2D array using UDP

    I am trying to send and recieve a 5x5 int array. (I posted this in the C forum but just found out i overlooked this forum which is probably the proper place for a Q like this)

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

    Code:
            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]:


    Code:
            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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. your sizeof is wrong, you've just picked a (non-existent) element of the array and done sizeof on that.
    If distvector is really your array (and not some pointer to it), then sizeof(distvector) is all you need.

    2. Perhaps it is an address - given your "odd" comment. Lose the & on the second parameter.

    > recv_DV[0][0] has an odd number in it
    Odd, as in 1,3,5,7 are odd, or odd in the "unusual" sense.

    > and the rest of the array is '0'
    Like I said, you're only sending one int, and you're only receiving 1 int as well - fix the sizeof's

    If you're still stuck. post the in-scope declarations of your arrays as well.

    Other considerations
    - neither send/recv guarantee to handle the whole message in a single call. You should check the return status to find out how much data was transferred, and repeat the calls as necessary to get further data.
    - are you sending binary data to a machine with a different architecture?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    UPD flushing

    I got the array sending to work.

    Basically the first parameter is the address of the first term/begining of the data and the size of tells how far fwd to go when sending.

    I just used distvector and sizeof(distvector)

    but i found out that though this allows me to send an array i didnt' fix my problem. And i figured out what it was, my UDP recieve buffer isnt' flushed.

    I'm not sure how to flush my UDP socket? Any ideas.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  2. Treating a 2D array as a 1D array....
    By John_L in forum C Programming
    Replies: 6
    Last Post: 10-18-2007, 02:38 PM
  3. malloced 2d array passed to qsort
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 11-25-2005, 01:55 PM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM