By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

about inet_ntoa(ptParams->pServerHandle->sockAddrClient.sin_addr)

P: n/a
Hi friends,
I had implemented client server application in C
under VC++ environment. It can handle more than one client. So in this
program i want to keep track of clients IP which gets connected to the
server and the client which gets disconnected. for this i am using
int_ntoa() function. but the problem which i am facing is that the
client which gets connected at last overwrites the o/p of inet_ntoa()

o/p comes as
Client 127.0.0.1 connected at : Wed Nov 08 17:27:08 2006
Client 198.68.18.4 connected at : Wed Nov 08 17:28:08 2006
Client 198.68.18.4 disconnected at : Wed Nov 08 17:29:53 2006
Client 198.68.18.4 disconnected at : Wed Nov 08 17:29:59 2006

The I addres get overwridden. How can I correct it.

Any help is highly appriciated.

Nov 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hello,
I had implemented client server application in C
under VC++ environment. It can handle more than one client. So in this
program i want to keep track of clients IP which gets connected to the
server and the client which gets disconnected. for this i am using
int_ntoa() function. but the problem which i am facing is that the
client which gets connected at last overwrites the o/p of inet_ntoa()
Guess, that's off topic for a newsgroup like c.l.c... But what you need
is a copy of the inet address per client.

HTH,
Loic.

Nov 8 '06 #2

P: n/a
<dh*******@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
I had implemented client server application in C
under VC++ environment. It can handle more than one client. So in this
program i want to keep track of clients IP which gets connected to the
server and the client which gets disconnected. for this i am using
int_ntoa() function. but the problem which i am facing is that the
client which gets connected at last overwrites the o/p of inet_ntoa()

o/p comes as
Client 127.0.0.1 connected at : Wed Nov 08 17:27:08 2006
Client 198.68.18.4 connected at : Wed Nov 08 17:28:08 2006
Client 198.68.18.4 disconnected at : Wed Nov 08 17:29:53 2006
Client 198.68.18.4 disconnected at : Wed Nov 08 17:29:59 2006

The I addres get overwridden. How can I correct it.
<OT>
My response assumes the inet_ntoa() is the BSD/POSIX one; if that's
true, you're probably better off in comp.unix.programmer.

This is from the man page on inet_ntoa() on my system:

The inet_ntoa() function converts the Internet host
address in given in network byte order to a string in
standard numbers-and-dots notation. The string is
returned in a statically allocated buffer, which subse-
quent calls will overwrite.

I don't have the appropriate spec handy for citation, but IIRC this is
the defined behavior for this function; there is no way to "fix" it
because it is behaving correctly according to the spec.

What you need to fix is _your_ code. If you are going to call
inet_ntoa() several times in a row, you will need to copy the string
that it returns into a distinct buffer of your own before proceeding.

If you call inet_ntoa() from multiple threads, you're eventually going
to get garbage because the function is not reentrant.
</OT>

You might as well use sprintf() and produce the string yourself; it's
only a single line of code, and it's fairly straightforward to write.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

--
Posted via a free Usenet account from http://www.teranews.com

Nov 8 '06 #3

P: n/a
dh*******@gmail.com wrote:
# Hi friends,
# I had implemented client server application in C
# under VC++ environment. It can handle more than one client. So in this
# program i want to keep track of clients IP which gets connected to the
# server and the client which gets disconnected. for this i am using
# int_ntoa() function. but the problem which i am facing is that the
# client which gets connected at last overwrites the o/p of inet_ntoa()
#
# o/p comes as
# Client 127.0.0.1 connected at : Wed Nov 08 17:27:08 2006
# Client 198.68.18.4 connected at : Wed Nov 08 17:28:08 2006
# Client 198.68.18.4 disconnected at : Wed Nov 08 17:29:53 2006
# Client 198.68.18.4 disconnected at : Wed Nov 08 17:29:59 2006
#
# The I addres get overwridden. How can I correct it.

Copy the returned string. If you have strdup available, you do
char *s = strdup(inet_ntoa(...));

If not you can do something like
char *s = inet_ntoa(...);
s = strcpy(malloc(strlen(s)+1),s);

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Why are we here?
whrp
Nov 9 '06 #4

P: n/a
Thanks for ur valuable help.
Finally I have to copy the output of inet_ntoa to a character pointer
array to get the desired result.
SM Ryan wrote:
dh*******@gmail.com wrote:
# Hi friends,
# I had implemented client server application in C
# under VC++ environment. It can handle more than one client. So in this
# program i want to keep track of clients IP which gets connected to the
# server and the client which gets disconnected. for this i am using
# int_ntoa() function. but the problem which i am facing is that the
# client which gets connected at last overwrites the o/p of inet_ntoa()
#
# o/p comes as
# Client 127.0.0.1 connected at : Wed Nov 08 17:27:08 2006
# Client 198.68.18.4 connected at : Wed Nov 08 17:28:08 2006
# Client 198.68.18.4 disconnected at : Wed Nov 08 17:29:53 2006
# Client 198.68.18.4 disconnected at : Wed Nov 08 17:29:59 2006
#
# The I addres get overwridden. How can I correct it.

Copy the returned string. If you have strdup available, you do
char *s = strdup(inet_ntoa(...));

If not you can do something like
char *s = inet_ntoa(...);
s = strcpy(malloc(strlen(s)+1),s);

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Why are we here?
whrp
Nov 9 '06 #5

P: n/a
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrites:
[...]
Copy the returned string. If you have strdup available, you do
char *s = strdup(inet_ntoa(...));

If not you can do something like
char *s = inet_ntoa(...);
s = strcpy(malloc(strlen(s)+1),s);
Where "something like" presumably implies the addition of a check on
the result of malloc().

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.