Ask a Question related to UNIX Programming, Design and Development.
-
Chris Ritchey #1
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
-
#40481 [NEW]: mail() sends email to wrong address where there are "-" in localpart of "to"
From: penartur at yandex dot ru Operating system: Linux PHP version: 5.2.1 PHP Bug Type: Mail related Bug description: ... -
"make Makefile.PL" returns "...up to date"; make returns "no target to make"
I'm attempting to install a perl module (AppConfig-1.56) on a FreeBSD 4.9 system. It has both perl 5.5.3 and 5.8.3 and several modules are already... -
#26292 [Opn->Bgs]: substr returns "0" for any offset on the string "0"
ID: 26292 Updated by: sniper@php.net Reported By: ravacholp at hotmail dot com -Status: Open +Status: ... -
dr("field").toString returns "400.0000" instead of "400"
I have just installed VS.NET 2003 on my computer. I have a project that I have been developing on VS.NET 2002. I haven't upgraded this project to... -
ServerVariables("Remote_Addr") returns local address
I hope one of you guys can find a solution to my problem. We are trying to capture the IP addresses of outside users accessing our website.... -
Barry Margolin #2
Re: recvfrom returns with an error code of 14, EFAULT "Bad Address"
In article <480de79d.0307030820.7b959e89@posting.google.com >,
Chris Ritchey <rethnor@yahoo.com> wrote:It looks like you didn't post the correct code, I don't see any calls to>I'll post the code at the bottom of the post. Whenever I try to
recvfrom() in the excerpt.
Sounds like you passed an uninitialized pointer as the buffer argument>retreive a udp packet sent via broadcast I get the errorcode
>EFAULT(bad address). The same program sends out the packets without
argument.
If it returns the number of bytes, then how can it also be reporting an>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
error code? Errno is only meaningful if recvfrom() returns -1.
I don't understand this at all. Buffer is an input parameter, not an>returns the char* buffer as NULL. Any help would be appriciated and I
output parameter. You have to pass in the pointer to the place where you
want the data written.
Have you looked at the sample code in Unix Network Programming, Vol.1?>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:
--
Barry Margolin, [email]barry.margolin@level3.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
-
Chris Ritchey #3
Re: recvfrom returns with an error code of 14, EFAULT "Bad Address"
Barry Margolin <barry.margolin@level3.com> wrote in message news:<4KZMa.16$Qd3.146@paloalto-snr1.gtei.net>...
Depest appologies I was posting about that, I was posting how I set> In article <480de79d.0307030820.7b959e89@posting.google.com >,
> Chris Ritchey <rethnor@yahoo.com> 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.
>
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.
This is the main problem I am haveing. Before I pass the point to>> >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.
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.
Ah yes you are correct, I was looking at the number thinking it was>> >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.
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.
Sorry wasn't feeling all that awake when I originally wrote this post.>> >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.
What I ment was the char* buff that is passed by referance that was
being returned to null.
Yes I have but I was wondering there was something I needed to do>> >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?
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
-
Barry Margolin #4
Re: recvfrom returns with an error code of 14, EFAULT "Bad Address"
In article <480de79d.0307031340.25199705@posting.google.com >,
Chris Ritchey <rethnor@yahoo.com> wrote:Buffer isn't a reference parameter. It's a pointer to an existing buffer>Barry Margolin <barry.margolin@level3.com> wrote in message
>news:<4KZMa.16$Qd3.146@paloalto-snr1.gtei.net>...>>> In article <480de79d.0307030820.7b959e89@posting.google.com >,
>> Chris Ritchey <rethnor@yahoo.com> 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.
that you must provide.
--
Barry Margolin, [email]barry.margolin@level3.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
-
Chris Ritchey #5
Re: recvfrom returns with an error code of 14, EFAULT "Bad Address"
Barry Margolin <barry.margolin@level3.com> wrote in message news:<oJ1Na.42$Qd3.161@paloalto-snr1.gtei.net>...
ok I did get it to work, the problem was that I set the char* to NULL,> In article <480de79d.0307031340.25199705@posting.google.com >,
> Chris Ritchey <rethnor@yahoo.com> wrote:>> >Barry Margolin <barry.margolin@level3.com> wrote in message
> >news:<4KZMa.16$Qd3.146@paloalto-snr1.gtei.net>...> >> >> In article <480de79d.0307030820.7b959e89@posting.google.com >,
> >> Chris Ritchey <rethnor@yahoo.com> 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.
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
-
Barry Margolin #6
Re: recvfrom returns with an error code of 14, EFAULT "Bad Address"
In article <480de79d.0307070905.5e8c9120@posting.google.com >,
Chris Ritchey <rethnor@yahoo.com> wrote:You need to call malloc() or set it to point to an automatic or static>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.
array. If you simply removed the NULL assignment, you've now got an
uninitialized variable, and you'll be scribbling over random memory.
How could it do this? C doesn't have reference parameters, so it can't set>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.
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.
What's a "memory think"? I think you meant "memory leak".> 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
Recv() doesn't (and *can't*) set the pointer. It's not returning anything,>"returning."
it's just filling in the memory that you allocated for it.
--
Barry Margolin, [email]barry.margolin@level3.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
-
Casper H.S. Dik #7
Re: recvfrom returns with an error code of 14, EFAULT "Bad Address"
[email]rethnor@yahoo.com[/email] (Chris Ritchey) writes:
By shear luck; now you're corrupting memory somewhere.>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.
But "recv" has no way to communicate to you where the allocated pointer>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."
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



Reply With Quote

