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

D

dharmdeep

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.
 
L

loic-dev

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.
 
S

Stephen Sprunk

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
 
S

SM Ryan

(e-mail address removed) 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);
 
D

dharmdeep

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.
 
K

Keith Thompson

SM Ryan said:
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().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top