C套接字编程问题 [英] C Socket programming question

查看:54
本文介绍了C套接字编程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力为便利店行业写一个销售点计划的通讯部分

。每个商店的设置将有不同的
数量的寄存器。可能只有2个或多达12个。我工作的

程序在计算机上运行,​​该计算机与我们的气体通信

泵并将状态更改发送到所有登记册。它还处理特定寄存器的某些

量的个人通信。无论如何,我是

试图在程序开始时编写一个函数来运行

识别所有寄存器并将其主机信息加载到一个阵列中

MAXREGISTERS的长度,这样我可以获得信息

每当我需要发送信息时。


我写了一个功能如下:


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,商店);


/ ******调试代码****** /

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);

}

}

其他

{

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," ;发送套接字时出错:");

sprintf(error_msg_2,无法获取远程注册);

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);

}

返回;
} / *结束Find_Hosts * /


remote_reg的定义如下:

struct hostent * remote_reg [MAXREGISTERS ];


问题是remote_reg [0] == remote_reg [1]的值。在测试中

场景MAXREGISTERS == 2.


我在这里做错了什么?


这是方法不起作用?


我还能怎样处理这个问题?


原来的实现是假设总是有

2个寄存器,所以我只是定义了

struct hostent * remote_reg1;

struct hostent * remote_reg2;

我真的不希望每个不同的商店都有一个单独的程序

基于寄存器的数量。我希望能够从文件中读取
MAXREGISTERS并根据需要运行程序。


提前致谢,

justin

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

推荐答案

文章< bo ********** @ news.tdl.com>,

Justin Robbs< ju ************ @ SPAMhotmail.com>写道:
In article <bo**********@news.tdl.com>,
Justin Robbs <ju************@SPAMhotmail.com> 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.




您的问题看起来(至少对我而言)就像它涉及gethostbyname

正在做,这超出了comp.lang.c的范围。由于这个

看上去模糊不清,comp.unix.programmer将是我第一次猜到

a更好的地方。


ObC:gethostbyname是否可能返回指向静态
缓冲区的指针?如果是这种情况,那么它将每个

时间返回相同的指针,当你按照指针时,你会得到任何填充到最近缓冲的


dave


-

Dave Vandervies dj ****** @ csclub.uwaterloo.ca

++:它是用于只读语言APL的精细竞争者。 / - Mike

++我以为那是COBOL。 APL是一种只写语言。 / Andrews,Dave

所以,我们所需要的只是一个APL-to-COBOL编译器。 /布朗和SDM中的阿比盖尔



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


你的问题看起来(至少对我而言)就像它涉及到什么gethostbyname
正在做,这超出了comp.lang.c的范围。由于这看起来模糊不清,comp.unix.programmer将是我第一次猜到
一个更好的地方。

ObC:gethostbyname是否可能返回一个指向一个静态的缓冲区?如果是这种情况,那么它会在每个
时间返回相同的指针,当你按照指针时,你会得到它最近填充的缓冲区。
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.




我很确定这就是正在发生的事情,因为我得到的值是最后一个查找系统的

值。我想发布到

comp.unix.programmer,因为它与套接字有关。我只是觉得

,因为它更多的是处理静态缓冲区而不是专门处理套接字的
,这将是一个合适的场所。我会将问题发给银联,但与此同时有人会建议用这种方式来解决这个问题吗?


谢谢,

贾斯汀



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


2003年11月12日星期三10:48:15 -0700,Justin Robbs

< ju ************ @ SPAMhotmail.com>写道:
On Wed, 12 Nov 2003 10:48:15 -0700, "Justin Robbs"
<ju************@SPAMhotmail.com> wrote:
我很确定这就是正在发生的事情,因为我得到的值是最后一个查找系统的值。我想发布到
comp.unix.programmer,因为它与套接字有关。我只是想,因为它更像是处理静态缓冲区而不是专门处理套接字,所以这将是一个合适的场所。我
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




如果你真的这么认为,把问题放在那些条件中

就可以明确地说明主题了。为什么不在你再次调用函数之前复制你想要保存的结果?


-

Al Balmer

Balmer Consulting
re************************@att.net


这篇关于C套接字编程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆