Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C Socket programming question

Reply
Thread Tools

C Socket programming question

 
 
Justin Robbs
Guest
Posts: n/a
 
      11-12-2003
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_msg_1,"Error Sending Socket: ");
sprintf(error_msg_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


 
Reply With Quote
 
 
 
 
Dave Vandervies
Guest
Posts: n/a
 
      11-12-2003
In article <botptk$om4$(E-Mail Removed)>,
Justin Robbs <(E-Mail Removed)> 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.programmer 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 http://www.velocityreviews.com/forums/(E-Mail Removed)
++ : 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
 
Reply With Quote
 
 
 
 
Justin Robbs
Guest
Posts: n/a
 
      11-12-2003

> 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.programmer 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.programmer, 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


 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      11-12-2003
On Wed, 12 Nov 2003 10:48:15 -0700, "Justin Robbs"
<(E-Mail Removed)> 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.programmer, 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
(E-Mail Removed)
 
Reply With Quote
 
Justin Robbs
Guest
Posts: n/a
 
      11-12-2003

"Alan Balmer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Wed, 12 Nov 2003 10:48:15 -0700, "Justin Robbs"
> <(E-Mail Removed)> 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.programmer, 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


 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      11-12-2003
On Wed, 12 Nov 2003 13:30:04 -0700, "Justin Robbs"
<(E-Mail Removed)> wrote:

>
>"Alan Balmer" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>> On Wed, 12 Nov 2003 10:48:15 -0700, "Justin Robbs"
>> <(E-Mail Removed)> 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.programmer, 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
(E-Mail Removed)
 
Reply With Quote
 
Justin Robbs
Guest
Posts: n/a
 
      11-12-2003

> 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


 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      11-12-2003
In article <(E-Mail Removed)>,
Alan Balmer <(E-Mail Removed)> 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 (E-Mail Removed)
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
 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      11-12-2003
On Wed, 12 Nov 2003 14:24:09 -0700, "Justin Robbs"
<(E-Mail Removed)> 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.
>Unfortunately, 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
(E-Mail Removed)
 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      11-12-2003
On Wed, 12 Nov 2003 14:24:09 -0700, "Justin Robbs"
<(E-Mail Removed)> 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.
>Unfortunately, I don't have that on my system.
>

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

--
Al Balmer
Balmer Consulting
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments