problem of sendto()...

Hi,

When I call sendto() function under QNX6.20 it occurred a abnormal
situation:
it print some other character and the contents I want to send
out,actually I don’t print anything in the context codes.

my codes is very simple like this:

if( sock_wave=socket( AF_INET, SOCK_DGRAM, 0) < 0 )
{
perror( “Unable create socket” );
return 1;
}

bzero( &dest_wave, sizeof(struct sockaddr_in) );
dest_wave.sin_family = AF_INET;
dest_wave.sin_addr.s_addr = inet_addr(“192.168.1.210”);
dest_wave.sin_port = htonl(5533);

for( ; ; )
{
delay(50);

wave_display[0] = ‘T’;
wave_display[1] = ‘E’;

r = sendto(sock_wave, wave_display,
sizeof(wave_display),
0,
(struct sockaddr *)&dest_wave,
sizeof(struct sockaddr) );
if( r <= 0 )
{ //never print this message
perror(“sendto I-value display PC occured.\n”);
}
}

and the results are:
A"Ã0/00TE
A"Ã0/00TE
A"Ã0/00TE
A"Ã0/00TE

I use “netstat -a” utility got the follow info:

udp:
9704 datagrams received //? I just send,not open port for receive
0 with incomplete header
0 with bad data length field
0 with bad checksum
1 dropped due to no socket
9700 broadcast/multicast datagrams dropped due to no socket //??
0 dropped due to full socket buffers
3 delivered
9703 PCB hash misses
14 datagrams output

Why got this result? may I wrong configure some files?

Thanks for any suggestion!

Is 192.168.1.210 yourself? Is it some remote node?
Who is receiving at port 5533 ?

-xtang

Chen <hichun@ipp.ac.cn> wrote in message news:3F8E818D.9060208@ipp.ac.cn

Hi,

When I call sendto() function under QNX6.20 it occurred a abnormal
situation:
it print some other character and the contents I want to send
out,actually I don’t print anything in the context codes.

my codes is very simple like this:

if( sock_wave=socket( AF_INET, SOCK_DGRAM, 0) < 0 )
{
perror( “Unable create socket” );
return 1;
}

bzero( &dest_wave, sizeof(struct sockaddr_in) );
dest_wave.sin_family = AF_INET;
dest_wave.sin_addr.s_addr = inet_addr(“192.168.1.210”);
dest_wave.sin_port = htonl(5533);

for( ; ; )
{
delay(50);

wave_display[0] = ‘T’;
wave_display[1] = ‘E’;

r = sendto(sock_wave, wave_display,
sizeof(wave_display),
0,
(struct sockaddr *)&dest_wave,
sizeof(struct sockaddr) );
if( r <= 0 )
{ //never print this message
perror(“sendto I-value display PC occured.\n”);
}
}

and the results are:
A"Ã0/00TE
A"Ã0/00TE
A"Ã0/00TE
A"Ã0/00TE

I use “netstat -a” utility got the follow info:
snip
udp:
9704 datagrams received //? I just send,not open port for receive
0 with incomplete header
0 with bad data length field
0 with bad checksum
1 dropped due to no socket
9700 broadcast/multicast datagrams dropped due to no socket //??
0 dropped due to full socket buffers
3 delivered
9703 PCB hash misses
14 datagrams output
/snip

Why got this result? may I wrong configure some files?

Thanks for any suggestion!

By the way, you should also check what inet_addr() returned, just in case.

-xtang

Xiaodan Tang <xtang@qnx.com> wrote in message
news:bmmd34$mbc$1@nntp.qnx.com

Is 192.168.1.210 yourself? Is it some remote node?
Who is receiving at port 5533 ?

-xtang

Chen <> hichun@ipp.ac.cn> > wrote in message
news:> 3F8E818D.9060208@ipp.ac.cn> …
Hi,

When I call sendto() function under QNX6.20 it occurred a abnormal
situation:
it print some other character and the contents I want to send
out,actually I don’t print anything in the context codes.

my codes is very simple like this:

if( sock_wave=socket( AF_INET, SOCK_DGRAM, 0) < 0 )
{
perror( “Unable create socket” );
return 1;
}

bzero( &dest_wave, sizeof(struct sockaddr_in) );
dest_wave.sin_family = AF_INET;
dest_wave.sin_addr.s_addr = inet_addr(“192.168.1.210”);
dest_wave.sin_port = htonl(5533);

for( ; ; )
{
delay(50);

wave_display[0] = ‘T’;
wave_display[1] = ‘E’;

r = sendto(sock_wave, wave_display,
sizeof(wave_display),
0,
(struct sockaddr *)&dest_wave,
sizeof(struct sockaddr) );
if( r <= 0 )
{ //never print this message
perror(“sendto I-value display PC occured.\n”);
}
}

and the results are:
A"Ã0/00TE
A"Ã0/00TE
A"Ã0/00TE
A"Ã0/00TE

I use “netstat -a” utility got the follow info:
snip
udp:
9704 datagrams received //? I just send,not open port for
receive
0 with incomplete header
0 with bad data length field
0 with bad checksum
1 dropped due to no socket
9700 broadcast/multicast datagrams dropped due to no socket
//??
0 dropped due to full socket buffers
3 delivered
9703 PCB hash misses
14 datagrams output
/snip

Why got this result? may I wrong configure some files?

Thanks for any suggestion!

Xiaodan Tang wrote:

Is 192.168.1.210 yourself? Is it some remote node?
Who is receiving at port 5533 ?

of course,the “192.168.1.210” is remote node, and one of its processes
receiving
5533 port.

And i check the inet_addr() return value, it not indicated any error
message.

“Chen” <hichun@ipp.ac.cn> wrote in message
news:3F8E818D.9060208@ipp.ac.cn

Hi,

When I call sendto() function under QNX6.20 it occurred a abnormal
situation:
it print some other character and the contents I want to send
out,actually I don’t print anything in the context codes.

my codes is very simple like this:

if( sock_wave=socket( AF_INET, SOCK_DGRAM, 0) < 0 )
{
perror( “Unable create socket” );
return 1;
}

bzero( &dest_wave, sizeof(struct sockaddr_in) );
dest_wave.sin_family = AF_INET;
dest_wave.sin_addr.s_addr = inet_addr(“192.168.1.210”);
dest_wave.sin_port = htonl(5533);

for( ; ; )
{
delay(50);

wave_display[0] = ‘T’;
wave_display[1] = ‘E’;

If for some reason sock_wave gets trashed to 1 or 2 it would endup on

stdout.

From your description I understand program display output on console while
it should not, right?


r = sendto(sock_wave, wave_display,
sizeof(wave_display),
0,
(struct sockaddr *)&dest_wave,
sizeof(struct sockaddr) );
if( r <= 0 )
{ //never print this message
perror(“sendto I-value display PC occured.\n”);
}
}

and the results are:
A"Ã0/00TE
A"Ã0/00TE
A"Ã0/00TE
A"Ã0/00TE

I use “netstat -a” utility got the follow info:
snip
udp:
9704 datagrams received //? I just send,not open port for receive
0 with incomplete header
0 with bad data length field
0 with bad checksum
1 dropped due to no socket
9700 broadcast/multicast datagrams dropped due to no socket //??
0 dropped due to full socket buffers
3 delivered
9703 PCB hash misses
14 datagrams output
/snip

Why got this result? may I wrong configure some files?

Thanks for any suggestion!

Chen <hichun@ipp.ac.cn> wrote:

Hi,

When I call sendto() function under QNX6.20 it occurred a abnormal
situation:
it print some other character and the contents I want to send
out,actually I don’t print anything in the context codes.

my codes is very simple like this:

if( sock_wave=socket( AF_INET, SOCK_DGRAM, 0) < 0 )

if( (sock_wave=socket( AF_INET, SOCK_DGRAM, 0)) < 0 )

Order of operations – you did

sock_wave = ( socket() < 0);
socket() returned > 0; so socket() < 0 was false → 0, so you
assigned sock_wave = 0;

Then, sendto(0, … ) became the equivalent of
write(0, …)
which is stdout.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

if( (sock_wave=socket( AF_INET, SOCK_DGRAM, 0)) < 0 )

Order of operations – you did

sock_wave = ( socket() < 0);
socket() returned > 0; so socket() < 0 was false → 0, so you
assigned sock_wave = 0;

Then, sendto(0, … ) became the equivalent of
write(0, …)
which is stdout.

-David

Yes, David as you say the sock_wave==0 sometimes.

I think the zero value of sock_wave is very surprising, why it can get
zero??

Thank you, David.

Hichun Chen <hichun@ipp.ac.cn> wrote:

if( (sock_wave=socket( AF_INET, SOCK_DGRAM, 0)) < 0 )

Order of operations – you did

sock_wave = ( socket() < 0);
socket() returned > 0; so socket() < 0 was false → 0, so you
assigned sock_wave = 0;

Then, sendto(0, … ) became the equivalent of
write(0, …)
which is stdout.

-David

HC > Yes, David as you say the sock_wave==0 sometimes.

HC > I think the zero value of sock_wave is very surprising, why it can get
HC > zero??

HC > Thank you, David.

It should not be surprising. You just need an extra pair of
parenthesees (sp?). What was happening ws nothing like what you
intended to have happen. You wanted a file descriptor for the socket
and what you got was the truthfulness of socket() open failed or
not.

Bill Caroselli <qtps@earthlink.net> wrote:
BC > Hichun Chen <hichun@ipp.ac.cn> wrote:

if( (sock_wave=socket( AF_INET, SOCK_DGRAM, 0)) < 0 )

Order of operations – you did

sock_wave = ( socket() < 0);
socket() returned > 0; so socket() < 0 was false → 0, so you
assigned sock_wave = 0;

Then, sendto(0, … ) became the equivalent of
write(0, …)
which is stdout.

-David

BC > HC > Yes, David as you say the sock_wave==0 sometimes.

BC > HC > I think the zero value of sock_wave is very surprising, why it can get
BC > HC > zero??

BC > HC > Thank you, David.

BC > It should not be surprising. You just need an extra pair of
BC > parenthesees (sp?). What was happening ws nothing like what you
BC > intended to have happen. You wanted a file descriptor for the socket
BC > and what you got was the truthfulness of socket() open failed or
BC > not.

Let me take this one step further and make a bold statement.

NEVER DO ASSIGNMENT INSIDE AN IF STATEMENT !

i.e. Don’t do:
if( ( this = that() ) != OK )
Instead do:
this = that();
if( this != OK )

It is easier to read but the really big advantage is that you can set
a break point in the debugger after the assignment and before the test.

Hichun Chen <hichun@ipp.ac.cn> wrote:

if( (sock_wave=socket( AF_INET, SOCK_DGRAM, 0)) < 0 )

Order of operations – you did

sock_wave = ( socket() < 0);
socket() returned > 0; so socket() < 0 was false → 0, so you
assigned sock_wave = 0;

Then, sendto(0, … ) became the equivalent of
write(0, …)
which is stdout.

-David

Yes, David as you say the sock_wave==0 sometimes.

I think the zero value of sock_wave is very surprising, why it can get
zero??

Did you notice that when I repeated your line of code, I changed it
by adding an extra set of brackets?

You made a C order of operations mistake – the test was done before
the assignment. You wanted the assignment done first, you need more
brackets in your if statement to achieve this.

This behaviour is entirely unsurprising, and is also a common form
of error.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.