473,411 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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

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
5 3706
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
<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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
2
by: Aljo_ | last post by:
Hello! I got a free evaluation copy of Borland's JBuilder9. I also got a book about JAVA, and I am trying to type, compile and run some examples from the book. The following - very simple -...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
0
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.