Professional Web Applications Themes

recvfrom returns with an error code of 14, EFAULT "Bad Address" - UNIX Programming

I'll post the code at the bottom of the post. Whenever I try to retreive a udp packet sent via broadcast I get the errorcode EFAULT(bad address). The same program sends out the packets without any complaints but it fails when retreiving, I'm lost as to why it would report this error. It returns the number of bytes correctly but returns the char* buffer as NULL. Any help would be appriciated and I wouldn't be supprised if I was just missing something simple with broacasting as I am still learning some aspects of socket programiing.Thanks in advance to all whom ...

  1. #1

    Default recvfrom returns with an error code of 14, EFAULT "Bad Address"

    I'll post the code at the bottom of the post. Whenever I try to
    retreive a udp packet sent via broadcast I get the errorcode
    EFAULT(bad address). The same program sends out the packets without
    any complaints but it fails when retreiving, I'm lost as to why it
    would report this error. It returns the number of bytes correctly but
    returns the char* buffer as NULL. Any help would be appriciated and I
    wouldn't be supprised if I was just missing something simple with
    broacasting as I am still learning some aspects of socket
    programiing.Thanks in advance to all whom reply and here is the code:


    int Communications::GetBroadcastSocket()
    {
    int toReturn;
    int on = 1; // flag to turn on socket options

    memset(&broadcast, 0, sizeof(broadcast));
    broadcast.sin_family = AF_INET;
    broadcast.sin_port = PORTNUM + 1;
    broadcast.sin_addr.s_addr = inet_addr(BCAST_ADDR);

    if((toReturn = socket(AF_INET, SOCK_DGRAM, 0)) == -1) return -1;
    if( SetNonBlocking(toReturn) < 0 ||
    setsockopt(toReturn, SOL_SOCKET, SO_REUSEADDR, &on,
    sizeof(on)) < 0 ||
    setsockopt(toReturn, SOL_SOCKET, SO_BROADCAST, &on,
    sizeof(on)) < 0 ||
    bind(toReturn, (sockaddr *) &broadcast, sizeof(broadcast)) <
    0)
    {
    close(toReturn);
    return -1;
    }

    return toReturn;
    }
    Chris Ritchey Guest

  2. #2

    Default Re: recvfrom returns with an error code of 14, EFAULT "Bad Address"

    In article <480de79d.0307030820.7b959e89posting.google.com >,
    Chris Ritchey <rethnor> wrote:
    >I'll post the code at the bottom of the post. Whenever I try to
    It looks like you didn't post the correct code, I don't see any calls to
    recvfrom() in the excerpt.
    >retreive a udp packet sent via broadcast I get the errorcode
    >EFAULT(bad address). The same program sends out the packets without
    Sounds like you passed an uninitialized pointer as the buffer argument
    argument.
    >any complaints but it fails when retreiving, I'm lost as to why it
    >would report this error. It returns the number of bytes correctly but
    If it returns the number of bytes, then how can it also be reporting an
    error code? Errno is only meaningful if recvfrom() returns -1.
    >returns the char* buffer as NULL. Any help would be appriciated and I
    I don't understand this at all. Buffer is an input parameter, not an
    output parameter. You have to pass in the pointer to the place where you
    want the data written.
    >wouldn't be supprised if I was just missing something simple with
    >broacasting as I am still learning some aspects of socket
    >programiing.Thanks in advance to all whom reply and here is the code:
    Have you looked at the sample code in Unix Network Programming, Vol.1?

    --
    Barry Margolin, [email]barry.margolinlevel3.com[/email]
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Margolin Guest

  3. #3

    Default Re: recvfrom returns with an error code of 14, EFAULT "Bad Address"

    Barry Margolin <barry.margolinlevel3.com> wrote in message news:<4KZMa.16$Qd3.146paloalto-snr1.gtei.net>...
    > In article <480de79d.0307030820.7b959e89posting.google.com >,
    > Chris Ritchey <rethnor> wrote:
    > >I'll post the code at the bottom of the post. Whenever I try to
    >
    > It looks like you didn't post the correct code, I don't see any calls to
    > recvfrom() in the excerpt.
    >
    Depest appologies I was posting about that, I was posting how I set
    the socket up in case I did something wrong. I know that I'm not
    handling any errors in my code as it is still very early in
    development. What it does it takes the packet off the socket and
    places it in a linked list where another function can pull it off and
    use the data. The code is at the bottom of the page again.
    > >retreive a udp packet sent via broadcast I get the errorcode
    > >EFAULT(bad address). The same program sends out the packets without
    >
    > Sounds like you passed an uninitialized pointer as the buffer argument
    > argument.
    This is the main problem I am haveing. Before I pass the point to
    recvfrom I am initializing it to NULL. This exact same function was
    working before when the message was sent unicast instead of broadcast,
    but when the message is sent through broadcast the same code returns-1
    with errno set to 14, EFAULT.
    > >any complaints but it fails when retreiving, I'm lost as to why it
    > >would report this error. It returns the number of bytes correctly but
    >
    > If it returns the number of bytes, then how can it also be reporting an
    > error code? Errno is only meaningful if recvfrom() returns -1.
    Ah yes you are correct, I was looking at the number thinking it was
    the size of the packet received and instead it was the size of the
    packet that was sent. recv from is returning -1, I think in my
    frustration (and annoyance with flies) over looked this.
    > >returns the char* buffer as NULL. Any help would be appriciated and I
    >
    > I don't understand this at all. Buffer is an input parameter, not an
    > output parameter. You have to pass in the pointer to the place where you
    > want the data written.
    Sorry wasn't feeling all that awake when I originally wrote this post.
    What I ment was the char* buff that is passed by referance that was
    being returned to null.
    > >wouldn't be supprised if I was just missing something simple with
    > >broacasting as I am still learning some aspects of socket
    > >programiing.Thanks in advance to all whom reply and here is the code:
    >
    > Have you looked at the sample code in Unix Network Programming, Vol.1?
    Yes I have but I was wondering there was something I needed to do
    anything special with the the sockaddr_in other than
    sind_addr.s_addr = inet_addr(BCAST_ADDR)


    Phew thanks for looking through my errors in my writting. Hopefully I
    can give a clearer discription of the problem by correctly describing
    what is going on :)


    Here is half the function which uses recvfrom, the other half is
    basically the same.

    int Communications::QueueMessages(int socket)
    {
    QueueNode *newNode = new QueueNode;
    newNode->message=NULL;

    cout << "QueueMessages: Entering... ";
    if(first == NULL) // Then last is NULL, and there are no
    messages in queue
    {
    if( (newNode->messageSize = recvfrom(socket, newNode->message,
    MAXPACKETSIZE + 2, 0, (sockaddr*)&(newNode->client),
    NULL)) < 0 )
    {
    cout << "Error message: " << strerror(errno) << endl;
    numMsg = 0;
    delete newNode;
    return numMsg; // nothing to retreive
    }
    newNode->next = NULL;
    first = last = newNode;
    numMsg = 1;
    newNode = new QueueNode;
    }
    ...

    Where QueueNode is defined as

    // nodes for the queue in communications
    struct QueueNode
    {
    char* message;
    int messageSize;
    sockaddr_in client;
    QueueNode* next;
    };
    Chris Ritchey Guest

  4. #4

    Default Re: recvfrom returns with an error code of 14, EFAULT "Bad Address"

    In article <480de79d.0307031340.25199705posting.google.com >,
    Chris Ritchey <rethnor> wrote:
    >Barry Margolin <barry.margolinlevel3.com> wrote in message
    >news:<4KZMa.16$Qd3.146paloalto-snr1.gtei.net>...
    >> In article <480de79d.0307030820.7b959e89posting.google.com >,
    >> Chris Ritchey <rethnor> wrote:
    >> >returns the char* buffer as NULL. Any help would be appriciated and I
    >>
    >> I don't understand this at all. Buffer is an input parameter, not an
    >> output parameter. You have to pass in the pointer to the place where you
    >> want the data written.
    >
    >Sorry wasn't feeling all that awake when I originally wrote this post.
    >What I ment was the char* buff that is passed by referance that was
    >being returned to null.
    Buffer isn't a reference parameter. It's a pointer to an existing buffer
    that you must provide.

    --
    Barry Margolin, [email]barry.margolinlevel3.com[/email]
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Margolin Guest

  5. #5

    Default Re: recvfrom returns with an error code of 14, EFAULT "Bad Address"

    Barry Margolin <barry.margolinlevel3.com> wrote in message news:<oJ1Na.42$Qd3.161paloalto-snr1.gtei.net>...
    > In article <480de79d.0307031340.25199705posting.google.com >,
    > Chris Ritchey <rethnor> wrote:
    > >Barry Margolin <barry.margolinlevel3.com> wrote in message
    > >news:<4KZMa.16$Qd3.146paloalto-snr1.gtei.net>...
    > >> In article <480de79d.0307030820.7b959e89posting.google.com >,
    > >> Chris Ritchey <rethnor> wrote:
    > >> >returns the char* buffer as NULL. Any help would be appriciated and I
    > >>
    > >> I don't understand this at all. Buffer is an input parameter, not an
    > >> output parameter. You have to pass in the pointer to the place where you
    > >> want the data written.
    > >
    > >Sorry wasn't feeling all that awake when I originally wrote this post.
    > >What I ment was the char* buff that is passed by referance that was
    > >being returned to null.
    >
    > Buffer isn't a reference parameter. It's a pointer to an existing buffer
    > that you must provide.
    ok I did get it to work, the problem was that I set the char* to NULL,
    onc eI commented out this line of code it worked.

    I always thought that recv took a pointer and set it to a section of
    memory where the char array was at, not needing allocate it before
    hand, I am thinking thats what you mean. I'm not quite sure what you
    mean when you say we need to provide a buffer, I would think you mean
    we need to allocate memmory before hand but wouldn't that cause a
    memory think when the pointer was set to the what recv was
    "returning."
    Chris Ritchey Guest

  6. #6

    Default Re: recvfrom returns with an error code of 14, EFAULT "Bad Address"

    In article <480de79d.0307070905.5e8c9120posting.google.com >,
    Chris Ritchey <rethnor> wrote:
    >ok I did get it to work, the problem was that I set the char* to NULL,
    >onc eI commented out this line of code it worked.
    You need to call malloc() or set it to point to an automatic or static
    array. If you simply removed the NULL assignment, you've now got an
    uninitialized variable, and you'll be scribbling over random memory.
    >I always thought that recv took a pointer and set it to a section of
    >memory where the char array was at, not needing allocate it before
    >hand, I am thinking thats what you mean.
    How could it do this? C doesn't have reference parameters, so it can't set
    a pointer that it receives. C++ is able to do this, but recv() is an
    ordinary C function, not a C++ function. If it were intended to allocate
    the memory for you, it would have to receive a char** parameter.
    > I'm not quite sure what you
    >mean when you say we need to provide a buffer, I would think you mean
    >we need to allocate memmory before hand but wouldn't that cause a
    >memory think when the pointer was set to the what recv was
    What's a "memory think"? I think you meant "memory leak".
    >"returning."
    Recv() doesn't (and *can't*) set the pointer. It's not returning anything,
    it's just filling in the memory that you allocated for it.

    --
    Barry Margolin, [email]barry.margolinlevel3.com[/email]
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Margolin Guest

  7. #7

    Default Re: recvfrom returns with an error code of 14, EFAULT "Bad Address"

    [email]rethnor[/email] (Chris Ritchey) writes:
    >ok I did get it to work, the problem was that I set the char* to NULL,
    >onc eI commented out this line of code it worked.
    By shear luck; now you're corrupting memory somewhere.
    >I always thought that recv took a pointer and set it to a section of
    >memory where the char array was at, not needing allocate it before
    >hand, I am thinking thats what you mean. I'm not quite sure what you
    >mean when you say we need to provide a buffer, I would think you mean
    >we need to allocate memmory before hand but wouldn't that cause a
    >memory think when the pointer was set to the what recv was
    >"returning."
    But "recv" has no way to communicate to you where the allocated pointer
    would point; so the calling sequence tells you that you must provide the
    preallocated buffer. Now you're using a trash value which happens to point to
    some allocated memory.

    Casper
    --
    Expressed in this posting are my opinions. They are in no way related
    to opinions held by my employer, Sun Microsystems.
    Statements on Sun products included here are not gospel and may
    be fiction rather than truth.
    Casper H.S. Dik Guest

Similar Threads

  1. Replies: 1
    Last Post: February 14th, 12:04 PM
  2. Replies: 1
    Last Post: February 25th, 03:59 PM
  3. Replies: 0
    Last Post: November 17th, 10:56 PM
  4. dr("field").toString returns "400.0000" instead of "400"
    By Dan C Douglas in forum ASP.NET General
    Replies: 5
    Last Post: July 22nd, 05:48 PM
  5. Replies: 4
    Last Post: July 21st, 09:07 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139