473,769 Members | 4,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about inet_ntoa(ptPar ams->pServerHandl e->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 3729
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*******@gmai l.comwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.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.progr ammer.

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_nto a(...));

If not you can do something like
char *s = inet_ntoa(...);
s = strcpy(malloc(s trlen(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_nto a(...));

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

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Why are we here?
whrp
Nov 9 '06 #5
SM Ryan <wy*****@tang o-sierra-oscar-foxtrot-tango.fake.orgw rites:
[...]
Copy the returned string. If you have strdup available, you do
char *s = strdup(inet_nto a(...));

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

--
Keith Thompson (The_Other_Keit h) 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
2845
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 story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing screaming crying must stop can not take any more this is killing eScrew...
2
1991
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 - source comes from my book, and I get the errormessage that the applet isn't initialised.
220
19154
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 any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
8
2461
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 story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing screaming crying must stop can not take any more this is killing eScrew...
77
5694
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 database from SQL Server to Oracle or DB2 or vice versa... and that's it.... And a lot of these enterprises don't need it as they already know what database they are going to use and they don't plan on switching in and out database in the first...
125
14843
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 software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
5
3002
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 very funny. eScrew story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing
0
2456
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 story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing screaming crying must stop can not take any more this is killing eScrew...
7
3154
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 question about local variable initialization is still not solved. And some of the replies forked into talking about out parameters. And the thread is becoming way too deep. So I open a new thread here. My question in the previous thead has turned...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7409
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.