Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getaddrinfo failing with: "servname not supported for ai_socktype"

25 views
Skip to first unread message

woodb...@gmail.com

unread,
Feb 25, 2015, 1:25:25 PM2/25/15
to
I have a problem on PC-BSD 10.1/Clang 3.4.

There's a comment in this thread

http://stackoverflow.com/questions/10291923/getaddrinfo-failing-with-error-servname-not-supported-for-ai-socktype-in-c

about getline not removing a newline character.

I'm using fgets and strtok:

port=::strtok(nullptr,"\n ");

When I pass port to gettaddrinfo I get the error.
The same code works fine on Fedora/GCC 4.9, so I'm thinking
it has something to do with fgets or strtok in Clang.
Thanks in advance for ideas on how to resolve this.

And I've been working on improving the software here:

http://webEbenezer.net/build_integration.html

I'm happy with the way things have been going and invite
you to take a look by downloading the archive on that
page to your FreeBSD, Linux or Windows machine.


Brian
Ebenezer Enterprises - So far G-d has helped us.
http://webEbenezer.net

Mr Flibble

unread,
Feb 25, 2015, 2:22:38 PM2/25/15
to
On 25/02/2015 18:25, woodb...@gmail.com wrote:
> I have a problem on PC-BSD 10.1/Clang 3.4.
>
> There's a comment in this thread
>
> http://stackoverflow.com/questions/10291923/getaddrinfo-failing-with-error-servname-not-supported-for-ai-socktype-in-c
>
> about getline not removing a newline character.
>
> I'm using fgets and strtok:
>
> port=::strtok(nullptr,"\n ");

Why are you asking about fgets and strtok in this newsgroup Brian? This
is a C++ newsgroup not a C newsgroup or, given your use of nullptr, some
horrid bastardization of the two language styles. Write C code when
using a C compiler and write C++ code (i.e. use std::string and such)
when using a C++ compiler.

/Flibble

woodb...@gmail.com

unread,
Feb 25, 2015, 2:34:40 PM2/25/15
to
On Wednesday, February 25, 2015 at 12:25:25 PM UTC-6, woodb...@gmail.com wrote:
> I have a problem on PC-BSD 10.1/Clang 3.4.
>
> There's a comment in this thread
>
> http://stackoverflow.com/questions/10291923/getaddrinfo-failing-with-error-servname-not-supported-for-ai-socktype-in-c
>
> about getline not removing a newline character.
>
> I'm using fgets and strtok:
>
> port=::strtok(nullptr,"\n ");
>
> When I pass port to gettaddrinfo I get the error.
> The same code works fine on Fedora/GCC 4.9, so I'm thinking
> it has something to do with fgets or strtok in Clang.

I no longer think it has to do fgets or strtok. If I
change port from being a char* to

char port[6];

and then do this:

::strcpy(port,::strtok(nullptr,"\n "));

It works fine on PC-BSD/Clang. So I guess it
has to do with a C string that isn't null terminated.


Brian
Ebenezer Enterprises - "Like a bad tooth and an
unsteady foot is confidence in a faithless man
in time of trouble." Proverbs 25:19

http://webEbenezer.net

woodb...@gmail.com

unread,
Feb 25, 2015, 2:47:26 PM2/25/15
to
On Wednesday, February 25, 2015 at 1:22:38 PM UTC-6, Mr Flibble wrote:

>
> Why are you asking about fgets and strtok in this newsgroup Brian? This
> is a C++ newsgroup not a C newsgroup or, given your use of nullptr, some
> horrid bastardization of the two language styles. Write C code when
> using a C compiler and write C++ code (i.e. use std::string and such)
> when using a C++ compiler.
>
> /Flibble

Hi, Leigh,

I use some of the older stuff and std:string when using
a C++ compiler.

Brian
Ebenezer Enterprises - "It is better to live in a
corner of the roof than in a house shared with a
contentious woman." Proverbs 25:24

http://webEbenezer.net

Jens Thoms Toerring

unread,
Feb 25, 2015, 4:01:15 PM2/25/15
to
woodb...@gmail.com wrote:
> I have a problem on PC-BSD 10.1/Clang 3.4.

> There's a comment in this thread

> http://stackoverflow.com/questions/10291923/getaddrinfo-failing-with-error-servname-not-supported-for-ai-socktype-in-c

> about getline not removing a newline character.

I dodmn't find anything about that in there. But getline()
isn't supposed to remove the newline characterr at the end,
see for example

http://pubs.opengroup.org/onlinepubs/9699919799/functions/getdelim.html

> I'm using fgets

You just said getline() and now it's fgets()? fgets() is a
different beast since it doesn't try to allocated memory
for the string but stops reading when the buffer is full
(but always makes sure there's a terminating '\0'). It
also doesn't remove the trailing linefeed (if one was
found and fit into the buffer).

> and strtok:

> port=::strtok(nullptr,"\n ");

In the first call of strtok() you must pass it the
address of the string it's to be working on. Only if
you want further snippets of what that string con-
tains you use a NULL pointer. If this is your very
first call of it then it probably will return a NULL
pointer (but since this case isn't documented it may
also crash the program or returns something else, no-
body can know who hasn't taken a closer look at how it
is implemented).

Note that the string you pass to strtok() must be
modifiable - strtok() changes it by replaces de-
limiters by '\0' in that string So you can't e.g.
use the result of applying the c_str() method on a
std::string! You'd have to make a copy in that case.

And if it's just for removing a trailing linefeed
it's probaly simpler to do just something like

if ( port[ strlen( port ) - 1 ] == '\n' )
port[ strlen( port ) - 1 ] = '\0';

What you get from getline() should never have more than
a single trailing '\n' (but it may have none if it read
the last line in a file and there wasn'tx one in that
last line).
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de
0 new messages