473,890 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C Socket programming question

I am trying to write the communcations part of a Point of Sale program for
the Convenience Store industry. The setup in each store will have varying
numbers of registers. There could be as few as 2 or as many as 12. The
program I am working on runs on a computer which communicates to our gas
pumps and sends status changes to all registers. It also handles a certain
amount of individual communications to a specific register. Anyway, I am
trying to write a function to run at the beginning of the program to
identify all registers and load their host information into an array up to
MAXREGISTERS in length that way I could have the information available to me
whenever I need to send them information.

I have written a function that looks like this:

void
Find_Hosts()
{
int i;
char myname[MAXNAME+1], gasname[MAXNAME+1], store[5], temp[80],
Remote_Reg[8];
FILE *temp_io_stream ;

gethostname( myname, MAXNAME );
sprintf( Local_Gas, myname );

store[0] = myname[2];
store[1] = myname[3];
store[2] = myname[4];
store[3] = myname[5];
store[4] = '\0';

for( i=0; i<MAXREGISTERS ; i++ )
{
sprintf( Remote_Reg, "r%ds%s", i+1, store );

/****** DEBUG CODE ******/
if( ( temp_io_stream = fopen( "REG.FIL"," a" ) ) == NULL )
{
if( ( temp_io_stream = fopen( "REG.FIL"," w" ) ) != NULL )
{
fprintf( temp_io_stream, "%s\n", Remote_Reg );
fclose( temp_io_stream );
}
}
else
{
fprintf( temp_io_stream, "%s\n", Remote_Reg );
fclose( temp_io_stream );
}
/****** DEBUG CODE ******/

if( ( remote_reg [i] = gethostbyname( Remote_Reg ) ) == NULL )
{
sprintf(error_m sg_1,"Error Sending Socket: ");
sprintf(error_m sg_2,"Unable to get remote register");
error_codes(694 );
}
}

if( ( temp_io_stream = fopen( "STORE.FIL" ,"w" ) ) != NULL )
{
fwrite( store, sizeof (char), (size_t) 5, temp_io_stream );
fclose( temp_io_stream );
}
return;
} /* End Find_Hosts */

remote_reg is defined like this:

struct hostent *remote_reg [MAXREGISTERS];

The problem is the value of remote_reg [0] == remote_reg [1]. In the test
scenario MAXREGISTERS == 2.

What am I doing wrong here?

Is this approach not going to work?

How else might I approach this?

The original implementation of this went on the assumption of always having
2 registers, so I simply defined
struct hostent *remote_reg1;
struct hostent *remote_reg2;
I really don't want to have a separate program for each different store
based on the number of registers. I would like to be able to read
MAXREGISTERS from a file and the program run as needed.

Thanks in advance,
justin
Nov 13 '05 #1
25 4362
In article <bo**********@n ews.tdl.com>,
Justin Robbs <ju************ @SPAMhotmail.co m> wrote:
I am trying to write the communcations part of a Point of Sale program for
the Convenience Store industry. The setup in each store will have varying
numbers of registers.


Your problem looks (to me, at least) like it involves what gethostbyname
is doing, which puts it beyond the scope of comp.lang.c. Since this
looks vaguely unixish, comp.unix.progr ammer would be my first guess at
a better place to ask.

ObC: Is it possible that gethostbyname returns a pointer to a static
buffer? If that is the case, then it will return the same pointer every
time and when you follow the pointer you'll get whatever it stuffed into
that buffer most recently.
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
++ : It is written in that fine contender for Read-Only Language, APL. /--Mike
++ I thought that was COBOL. APL is a write-only language. /Andrews, Dave
So, all we need is an APL-to-COBOL compiler. /Brown, and Abigail in the SDM
Nov 13 '05 #2
Your problem looks (to me, at least) like it involves what gethostbyname
is doing, which puts it beyond the scope of comp.lang.c. Since this
looks vaguely unixish, comp.unix.progr ammer would be my first guess at
a better place to ask.

ObC: Is it possible that gethostbyname returns a pointer to a static
buffer? If that is the case, then it will return the same pointer every
time and when you follow the pointer you'll get whatever it stuffed into
that buffer most recently.


I am pretty sure that this is what is happening as the value I get is the
value of the last system that was looked up. I thought about posting to
comp.unix.progr ammer, because it was relating to sockets. I just thought
that since it was more an issure of dealing with a static buffer than
specifically dealing with sockets, this would be an appropriate venue. I
will post the question to C.U.P, but in the meantime could anyone suggest a
way to get around this problem?

Thanks,
Justin
Nov 13 '05 #3
On Wed, 12 Nov 2003 10:48:15 -0700, "Justin Robbs"
<ju************ @SPAMhotmail.co m> wrote:
I am pretty sure that this is what is happening as the value I get is the
value of the last system that was looked up. I thought about posting to
comp.unix.prog rammer, because it was relating to sockets. I just thought
that since it was more an issure of dealing with a static buffer than
specifically dealing with sockets, this would be an appropriate venue. I


If you actually thought that, putting the question in those terms
would have made it clearly on-topic. Why not just copy the results you
want to save before calling the function again?

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #4

"Alan Balmer" <al******@att.n et> wrote in message
news:ea******** *************** *********@4ax.c om...
On Wed, 12 Nov 2003 10:48:15 -0700, "Justin Robbs"
<ju************ @SPAMhotmail.co m> wrote:
I am pretty sure that this is what is happening as the value I get is the
value of the last system that was looked up. I thought about posting to
comp.unix.prog rammer, because it was relating to sockets. I just thought
that since it was more an issure of dealing with a static buffer than
specifically dealing with sockets, this would be an appropriate venue. I


If you actually thought that, putting the question in those terms
would have made it clearly on-topic. Why not just copy the results you
want to save before calling the function again?


I couldn't think of the terminology to describe it until it was he put it in
those terms. As far as copying the results, I don't know why I didn't think
of that. I guess I was just making it to complicated in my head.

Thanks,
Justin
Nov 13 '05 #5
On Wed, 12 Nov 2003 13:30:04 -0700, "Justin Robbs"
<ju************ @SPAMhotmail.co m> wrote:

"Alan Balmer" <al******@att.n et> wrote in message
news:ea******* *************** **********@4ax. com...
On Wed, 12 Nov 2003 10:48:15 -0700, "Justin Robbs"
<ju************ @SPAMhotmail.co m> wrote:
>I am pretty sure that this is what is happening as the value I get is the
>value of the last system that was looked up. I thought about posting to
>comp.unix.prog rammer, because it was relating to sockets. I just thought
>that since it was more an issure of dealing with a static buffer than
>specifically dealing with sockets, this would be an appropriate venue. I


If you actually thought that, putting the question in those terms
would have made it clearly on-topic. Why not just copy the results you
want to save before calling the function again?


I couldn't think of the terminology to describe it until it was he put it in
those terms. As far as copying the results, I don't know why I didn't think
of that. I guess I was just making it to complicated in my head.

Yep, in a complex program, it's easy to get bogged down in the
details. For me, network programming is particularly conducive to
confusion <g>. Dave zeroed in on the trouble spot nicely, though. I
expect that c.u.p would have spotted it quickly, too, though they
might have got distracted by other aspects of the code ;-)

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #6
Yep, in a complex program, it's easy to get bogged down in the
details. For me, network programming is particularly conducive to
confusion <g>. Dave zeroed in on the trouble spot nicely, though. I
expect that c.u.p would have spotted it quickly, too, though they
might have got distracted by other aspects of the code ;-)


I will probably regret asking this, but what other aspects are there with
the code. I am still somewhat of a newbie. I have learned C mostly from
looking at this program in addition to a couple of books, so I am probably
missing some information. I will take advantage of any opportunity to learn
that I can.

I did post to c.u.p and they came up with the same solution although someone
mentioned gethostbyname_r () which is a reentrant version of gethostbyname.
Unfortunately, I don't have that on my system.

Thanks,
Justin
Nov 13 '05 #7
In article <lj************ *************** *****@4ax.com>,
Alan Balmer <al******@spamc op.net> wrote:
Dave zeroed in on the trouble spot nicely, though.


That's what happens when you put a problem in front of a fresh set
of eyes.
My intuition was screaming that there was a C problem hidden behind
all the socket stuff, and seeing the return value from gethostbyname
stuffed into an array of pointers led to what appears to have been an
accurate guess.

I wonder if this means that I've been writing computer programs for
too long. (Not that I'd know what to do instead if it is time for a
career change...)
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
I thought everyone here was insane. That's why I come to visit, so
I'll feel at home.
--Mike Wahler in comp.lang.c
Nov 13 '05 #8
On Wed, 12 Nov 2003 14:24:09 -0700, "Justin Robbs"
<ju************ @SPAMhotmail.co m> wrote:
Yep, in a complex program, it's easy to get bogged down in the
details. For me, network programming is particularly conducive to
confusion <g>. Dave zeroed in on the trouble spot nicely, though. I
expect that c.u.p would have spotted it quickly, too, though they
might have got distracted by other aspects of the code ;-)
I will probably regret asking this, but what other aspects are there with
the code. I am still somewhat of a newbie. I have learned C mostly from
looking at this program in addition to a couple of books, so I am probably
missing some information. I will take advantage of any opportunity to learn
that I can.


I didn't see anything particularly obnoxious in your C code, and if
Barry Margolin didn't spot any Unix aspects, that's probably pretty
good, too :-)
I did post to c.u.p and they came up with the same solution although someone
mentioned gethostbyname_r () which is a reentrant version of gethostbyname.
Unfortunatel y, I don't have that on my system.

The reentrant version is less portable, but some implementations (such
as HP-UX) are at least thread-safe, but that's off-topic here.

However, deep copies were mentioned, and I think that's topical here,
although there's no general solution. What that means is that for
structures which contain pointers, copying the structure may not be
enough,. You may also need to copy the entities the pointers are
pointing to. That's the case here. The hostent structure is:

struct hostent {
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
};

The gthostbyname function returns a pointer to a static instance of
this structure, which will be overwritten on the next call. What may
not be so obvious is that h_name, h_aliases[], and h_addr_list[] may
also be static objects which will be overwritten on the next call, so
you have to copy them as well, else the pointers in your hostent
structure copy will be worthless. What's more, the two arrays are
variable length and null terminated. Getting all this data and
adjusting the pointers to refer to your local copies is called a "deep
copy." It's non-trivial, as you can see.

In real life, you often don't really need all the data (maybe you just
want the host name, for example.) In that case, it may be easier to
make local copies of only the data you need (build an array of
pointers to host name strings, for example.)

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #9
On Wed, 12 Nov 2003 14:24:09 -0700, "Justin Robbs"
<ju************ @SPAMhotmail.co m> wrote:

I will probably regret asking this, but what other aspects are there with
the code. I am still somewhat of a newbie. I have learned C mostly from
looking at this program in addition to a couple of books, so I am probably
missing some information. I will take advantage of any opportunity to learn
that I can.

I did post to c.u.p and they came up with the same solution although someone
mentioned gethostbyname_r () which is a reentrant version of gethostbyname.
Unfortunatel y, I don't have that on my system.

See new subject header "Re: Deep copies - was C Socket programming
question".

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13752
by: Jean-Philippe Guyon | last post by:
Hello, I am trying to compile a class that uses socket using the Visual C++ ..NET compiler. I get the following error: ------ Build started: Project: infCommon, Configuration: Release Win32 ------ Compiling... cl : Command line warning D4029 : optimization is not available in the
2
9238
by: dream machine | last post by:
Hi all , with BegeinReceive I can build async method of Socket Class that Receive the data from the Socket Client . My question is , if I have this code that create 3 Receive Async Call : <code> mySocket.BeginReceive(param1,param2,param3.....); mySocket.BeginReceive(param1,param2,param3......); mySocket.BeginReceive(param1,param2,param3......);
1
3407
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows XP. About the architecture: I have a socket server dll that contains a class that handles connections for a given local ipaddress and port. This class(server) can be started or stopped by calls to the appropriate functions. The server class has...
5
11741
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of 1024 with all values set to Null arrive. Other than examine a block of 1024 to see if the entire block is null, is there any other way to determine if , say a chat message "Hi Charlie" has been received completely?
5
2099
by: Justin Creasy | last post by:
If this is the wrong group for this posting please let me know and I'll move it. I have an application that has an ArrayList of sockets to clients. For standard one-to-one messages my application works great, the problem is if I want to "broadcast" a message to all my clients. Right now I just iterate through my sockets list and send the message to each client. This works fine until I need to send something large like a file. Is there a...
2
15338
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I now see the socket.RecieveTimeout 'property' in the visual studio 2005 help documentation (framework 2.0) and it has example of it being used with TCP socket. This propery is also listed as 'new in .net 2.0'. 1) is the socket.RecieveTimeout...
14
5952
by: =?Utf-8?B?TWlrZVo=?= | last post by:
I have a sync socket application. The client is blocked with Socket.Receive(...) in a thread, another thread calls Socket.Close(). This unblock the blocked thread. But the socket server is still connected. Any idea? Thanks.
3
2774
by: Stuart | last post by:
I am in the process of teaching myself socket programming. I am "playing around" with some simple echo server-client programs for m the book TCP/IP Sockets in C. The Server program is: #include "TCPEchoServer.h" /* TCP echo server includes */ #include <pthread.h /* for POSIX threads */
1
1837
by: =?Utf-8?B?UmFqbmk=?= | last post by:
Dear Sir/Mam, I have written a server code using the Windows Socket API's. Wherein I have created the socket and bound it to a particular IP address and port number. Later I have made the socket in non-blocking mode by using the proper socket option ( i.e. SO_RCVTIMEO). After which with the use of recv() I am trying to get into the receive mode. Here as the receive time out is being used the socket should come out of the block mode after...
4
11256
by: danomano | last post by:
The code below works fine. As of now I just set the buffer length real big and all is good. However, I hate the impreciseness of that. So my question in general is how do I know what length to set the buffer? In specific, my question is what causes the 'completed' event in the OnSocketConnectCompleted method to fire? Does it fire at end of a packet or does happen at some end of data marker? public SocketClient(Page pg, Int32...
0
9976
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9815
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
11215
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
10908
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
9618
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6035
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4665
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4259
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3267
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.