套接字连接问题 [英] socket connection problem

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

问题描述

在我的项目中,我已成功打开两个进程之间的sock_stream样式套接字

连接,并尝试将''缓冲区''发送到

连接的客户端, ''fd''。


下面的代码工作得很好。


void Macsock :: writeData(char * buffer,int fd)

{

char message [] ="这是来自前面的消息。" ;;

if(write(fd,message) ,sizeof(消息))< 0)

perror(在流插座上写作);

}


但是,显然,它没有发送所需的字符串。


以下代码不起作用。


void Macsock :: writeData(char * buffer,int fd)

{

char buf [strlen(buffer)];

strcpy(buf,buffer);

buf [sizeof(buf)] =''\ 0'';

if(write(fd,buf,sizeof(buf))< 0)

perror(写入流插座);

}


我敢肯定,这个非常简单的代码对于任何C程序员来说都是可怕的,

但我不是C程序员,我只是一个试图得到一些东西的黑客

完了。所有stcpy的东西都已完成,因为我提到的连接的

客户端对于由

null字符终止的消息非常挑剔。


任何关于为什么函数参数中的字符串会导致问题的建议,而函数中声明的字符串不是
将非常感激。我欢迎任何关于我的

情况的任何其他问题(我确定我已经忘记了一些重要的事情),但是我想要b / b
喜欢避免关于客户的问题,因为它不是用C ++写的

因此是偏离主题的。可以说,它已经用

其他主机进行了测试,效果很好。我99%确定问题是主机而不是客户问题



[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

In my project, I have successfully opened a sock_stream style socket
connection between two processes and am trying to send ''buffer'' to a
connected client, ''fd''.

The following code works just fine.

void Macsock::writeData(char *buffer, int fd)
{
char message[] = "This is a message from the front.";
if(write(fd, message, sizeof(message)) < 0 )
perror("writing on stream socket");
}

But, obviously, it is not sending the desired string.

The following code does not work.

void Macsock::writeData(char *buffer, int fd)
{
char buf[strlen(buffer)];
strcpy(buf, buffer);
buf[sizeof(buf)] = ''\0'';
if(write(fd, buf, sizeof(buf)) < 0 )
perror("writing on stream socket");
}

I''m sure that this very simple code is horrifying to any C programmer,
but I am not a C programmer, I''m just a hack trying to get something
finished. All of the stcpy stuff was done because the connected
client I mentioned is very picky about messages being terminated by a
null character.

Any suggestions as to why the string in the function parameter would
cause a problem, whereas the string declared in the function does not
would be greatly appreciated. I welcome any other questions about my
situation (I''m sure I have forgotten something important), but I would
like to avoid questions about the client, as it is not written in C++
and is therefore off-topic. Suffice to say, it has been tested with
other hosts and works just fine. I am 99% sure that the problem is
with the host, not the client.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

go **** @ jeffish.org (Jeff Crouse)写道:
go****@jeffish.org (Jeff Crouse) wrote:
在我的项目中,我已成功打开两个进程之间的sock_stream样式套接字连接,并尝试发送''buffer' '连接客户端'''fd''。

以下代码工作得很好。

void Macsock :: writeData(char * buffer,int fd)
{char message [] ="这是来自前面的消息。" ;;
if(write(fd,message,sizeof(message))< 0)
perror(在流插座上写);
}

但是,很明显,它并没有发送所需的字符串。

以下代码不起作用。

void Macsock :: writeData(char * buffer,int fd)
{char buf [strlen(buffer)];
strcpy( buf,buffer);
buf [sizeof(buf)] =''\ 0'';
if(w rite(fd,buf,sizeof(buf))< 0)
perror(在流插座上写作);
}

我很确定这个非常简单的代码对于任何C程序员来说都是可怕的,但是我不是C程序员,我只是一个试图获得某些东西的黑客已经完成了。所有stcpy的东西都已完成,因为我提到的连接的客户端对于由
null字符终止的消息非常挑剔。

有关为什么字符串在函数参数会导致问题,而函数中声明的字符串不会很受欢迎。我欢迎任何关于我的情况的其他问题(我确定我已经忘记了一些重要的事情),但是我希望避免关于客户端的问题,因为它不是用C ++编写的。 >因此是偏离主题的。可以说,它已经与其他主机一起测试过并且运行得很好。我99%确定问题是与主机有关,而不是客户端。
In my project, I have successfully opened a sock_stream style socket
connection between two processes and am trying to send ''buffer'' to a
connected client, ''fd''.

The following code works just fine.

void Macsock::writeData(char *buffer, int fd)
{
char message[] = "This is a message from the front.";
if(write(fd, message, sizeof(message)) < 0 )
perror("writing on stream socket");
}

But, obviously, it is not sending the desired string.

The following code does not work.

void Macsock::writeData(char *buffer, int fd)
{
char buf[strlen(buffer)];
strcpy(buf, buffer);
buf[sizeof(buf)] = ''\0'';
if(write(fd, buf, sizeof(buf)) < 0 )
perror("writing on stream socket");
}

I''m sure that this very simple code is horrifying to any C programmer,
but I am not a C programmer, I''m just a hack trying to get something
finished. All of the stcpy stuff was done because the connected
client I mentioned is very picky about messages being terminated by a
null character.

Any suggestions as to why the string in the function parameter would
cause a problem, whereas the string declared in the function does not
would be greatly appreciated. I welcome any other questions about my
situation (I''m sure I have forgotten something important), but I would
like to avoid questions about the client, as it is not written in C++
and is therefore off-topic. Suffice to say, it has been tested with
other hosts and works just fine. I am 99% sure that the problem is
with the host, not the client.




试试这个:

#include < iostream>

使用命名空间std;


int main(){

char message [] ="这是来自前面的消息。;

char * buf = message;

cout<< sizeof(消息)<< endl;

cout<< sizeof(buf)<< endl;

cout<< strlen(消息)<< endl;

cout<< strlen(buf)<<结束;

}


上述输出应指向正确的方向。



Try this:
#include <iostream>
using namespace std;

int main() {
char message[] = "This is a message from the front.";
char* buf = message;
cout << sizeof( message ) << endl;
cout << sizeof( buf ) << endl;
cout << strlen( message ) << endl;
cout << strlen( buf ) << endl;
}

The output of the above should point you in the right direction.


Jeff Crouse写道:


请不要交叉发帖给受审核的团体。它可以防止消息

及时到达非仲裁群组。我已经从交叉发布列表中删除了

comp.lang.c ++。
Jeff Crouse wrote:

Please don''t cross-post to moderated groups. It prevents the message
from reaching non-moderated groups in a timely manner. I''ve removed
comp.lang.c++.moderated from the cross-post list.
在我的项目中,我已经成功打开了sock_stream样式socket
两个进程之间的连接,我试图将''缓冲区''发送到
连接的客户端,''fd''。


请注意,套接字不是标准C ++的一部分,在这里不是主题。

以下代码工作得很好。

void Macsock :: writeData(char * buffer,int fd)
{char message [] ="这是来自前面的消息。" ;;
if(write(fd, message,sizeof(message))< 0)
perror(在流套接字上写);


你是否确定write()会设置errno?如果没有,你会更好

首先将errno设置为0,然后再测试它,如果

errno不为0则仅使用perror。


write()不是标准函数,所以我们在这里不能说太多。

}

但很明显,它没有发送所需的字符串。

以下代码不起作用。

void Macsock :: writeData(char * buffer,int fd)


你确定const char * buffer不会更好吗?

{
char buf [strlen(buffer)];


数组大小必须是编译时常量。即使这样有效,

数组对于要复制到其中的字符串来说也不够大。

strcpy(buf,buffer);


因为''buf''太小,这将覆盖你不拥有的内存,

导致未定义的行为(可能会导致程序崩溃或腐败

数据)。

buf [sizeof(buf)] =''\''';


''buf''的指数(如果正确宣布buf)从0到

sizeof(buf)-1,所以你写的过了这里阵列的结尾。

导致未定义的行为。这是非常基本的C ++数组 - 如果你不理解它,你最好还是继续学习一段时间

你试图使用数组或用语言做一些非常重要的事情。


这里有一个问题:你为什么试图复制阵列?

你可以可能只是传递原文 - 我猜是写不会

更改它。

if(write(fd,buf,sizeof(buf))< ; 0)
perror(在流套接字上写);


我确信这个非常简单的代码对任何C程序员来说都是可怕的, null字符终止的消息非常挑剔。


嗯,在第二个例子中,你甚至都没有尝试发送null

字符,据我所知。你有一个字符长度足够长的字符串

字符串但不是空终止符。然后你使用sizeof(buf)

作为大小参数发送(大概是 - 我只是猜测,因为你使用了

非标准函数)。这没有机会发送一个空字符,

写在buf数组的末尾(可能是 - undefined

行为,所以谁知道究竟发生了什么?) 。

任何关于为什么函数参数中的字符串会导致问题的建议,而在函数中声明的字符串不会被赞赏。


我确定它与非法破坏代码的使用有很大关系......它不需要编译.b $ b甚至不应该编译。 br />
我欢迎任何关于我的情况的其他问题(我确定我已经忘记了一些重要的事情),但是我想要避免关于客户的问题,因为它是不是用C ++写的
因此是偏离主题的。


套接字也是非主题的。

可以说,它已经过测试与其他主机一起工作得很好。我99%确定问题与主机有关,而不是客户端。
In my project, I have successfully opened a sock_stream style socket
connection between two processes and am trying to send ''buffer'' to a
connected client, ''fd''.
Note that sockets are not part of standard C++ and are not topical here.

The following code works just fine.

void Macsock::writeData(char *buffer, int fd)
{
char message[] = "This is a message from the front.";
if(write(fd, message, sizeof(message)) < 0 )
perror("writing on stream socket");
Are you quite sure that write() will set errno? If not, you''d better
first set errno to 0, then test it afterward and only use perror if
errno is non-0.

write() is not a standard function, so we can''t say much about it here.
}

But, obviously, it is not sending the desired string.

The following code does not work.

void Macsock::writeData(char *buffer, int fd)
Are you sure const char *buffer would not be better?
{
char buf[strlen(buffer)];
Array sizes must be compile-time constants. Even if this worked, the
array is not large enough for the string you want to copy into it.
strcpy(buf, buffer);
Because ''buf'' is too small, this will overwrite memory you don''t own,
causing undefined behavior (probably crashing your program or corrupting
data).
buf[sizeof(buf)] = ''\0'';
The indices for ''buf'' (if buf were properly declared) go from 0 to
sizeof(buf)-1, so you are writing past the end of the array here. That
causes undefined behavior. This is pretty basic C++ array stuff - if you
don''t understand it, you''d better return to studying for a while before
you attempt to use arrays or do anything non-trivial with the language.

And here''s a question: Why are you attempting to copy the array at all?
You could probably just pass in the original - I''m guessing write won''t
change it.
if(write(fd, buf, sizeof(buf)) < 0 )
perror("writing on stream socket");
}

I''m sure that this very simple code is horrifying to any C programmer,
but I am not a C programmer, I''m just a hack trying to get something
finished. All of the stcpy stuff was done because the connected
client I mentioned is very picky about messages being terminated by a
null character.
Well, in the second example you aren''t even attempting to send the null
character, as far as I can tell. You have a char array long enough for
the string but not the null terminator. Then you send using sizeof(buf)
as the size argument (presumably - I''m only guessing since you use a
non-standard function). That has no chance of sending a null character,
which was written past the end of the buf array (maybe - undefined
behavior, so who knows what actually happened?).

Any suggestions as to why the string in the function parameter would
cause a problem, whereas the string declared in the function does not
would be greatly appreciated.
I''m sure it has a lot to do with the use of illegal broken code... It
shouldn''t even compile.
I welcome any other questions about my
situation (I''m sure I have forgotten something important), but I would
like to avoid questions about the client, as it is not written in C++
and is therefore off-topic.
Sockets are off-topic, also.
Suffice to say, it has been tested with
other hosts and works just fine. I am 99% sure that the problem is
with the host, not the client.




你的意思是你发布的代码?呃,是的,我会说它有一些问题。


为什么你还在使用C风格的阵列?如果您使用过std :: string

,几乎所有的问题都会神奇地消失掉。但是,对于您来说,检查

数组如何工作以避免将来出现问题仍然是一个好主意。


-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用最近发布的地址。



You mean the code you post? Uh, yeah, I''d say there''s some problems with it.

Why are you using C-style arrays anyway? If you had used std::string
instead, pretty much all of your problems would have magically
disappeared. But it would still be a good idea for you to review how
arrays work to avoid problems in the future.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


在文章< 50 ************************** @ posting.google.com> ,杰夫

Crouse< go **** @ jeffish.org>写道
In article <50**************************@posting.google.com >, Jeff
Crouse <go****@jeffish.org> writes
在我的项目中,我已成功打开两个进程之间的sock_stream样式套接字连接,并尝试将缓冲区发送到
连接的客户端,'' fd''。

以下代码工作正常。

void Macsock :: writeData(char * buffer,int fd)
{
char message [] ="这是来自前面的消息。" ;;
if(write(fd,message,sizeof(message))< 0)
perror(在流套接字上写) ;);
}

但是,很明显,它并没有发送所需的字符串。

以下代码不起作用。
void Macsock :: writeData(char * buffer,int fd)
{char buf [strlen(buffer)];


由于C ++和C都是静态类型语言,因此必须在编译时知道元素的数量(本例中为字符)

时间。 strcpy(buf,buffer);
buf [sizeof(buf)] =''\ 0'';
if(write(fd,buf,sizeof(buf))< 0)
perror(在流套接字上写);


我确定这个非常简单的代码对于任何C程序员来说都是可怕的,
但我是不是C程序员,我只是一个试图获得一些东西的黑客。所有stcpy的东西都已完成,因为我提到的连接的客户端对于由
null字符终止的消息非常挑剔。
In my project, I have successfully opened a sock_stream style socket
connection between two processes and am trying to send ''buffer'' to a
connected client, ''fd''.

The following code works just fine.

void Macsock::writeData(char *buffer, int fd)
{
char message[] = "This is a message from the front.";
if(write(fd, message, sizeof(message)) < 0 )
perror("writing on stream socket");
}

But, obviously, it is not sending the desired string.

The following code does not work.

void Macsock::writeData(char *buffer, int fd)
{
char buf[strlen(buffer)];
The number of elements (chars in this case) must be known at compile
time as C++ and C are both statically typed languages. strcpy(buf, buffer);
buf[sizeof(buf)] = ''\0'';
if(write(fd, buf, sizeof(buf)) < 0 )
perror("writing on stream socket");
}

I''m sure that this very simple code is horrifying to any C programmer,
but I am not a C programmer, I''m just a hack trying to get something
finished. All of the stcpy stuff was done because the connected
client I mentioned is very picky about messages being terminated by a
null character.




其实一个C程序员会看一眼,注意::并忽略

休息因为它不是C.一个C ++程序员看到其他所有东西和

立即想知道为什么正在定义Macsock的成员函数

,试图编写纯粹的C.


您是否负责Macsock课程?如果是这样,你需要从了解课程设计基础知识的人那里寻求帮助

。如果没有

你需要扔掉它并找到一个好的实现。坦率地说,

以上代码在设计和

实现方面远远不合理的C ++,它的作者需要首先了解

可用的工具由C ++(std :: string,std :: fstream等)在

尝试重新设计和新实现之前提供。或者你需要

找到一个好的第三方实现的套接字类,满足你需要的



-

Francis Glassborow ACCU

64 Southfield Rd

牛津OX4 1PA +44(0)1865 246490

所有意见都是我的,不要代表任何组织的那些

[见 http:/ /www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



Actually a C programmer would take one look, note the :: and ignore the
rest because it isn''t C. A C++ programmer sees everything else and
immediately wonders why a member function of Macsock is being defined
with an attempt at writing pure C.

Are you responsible for the Macsock class? If so you need to find help
from someone who understands the fundamentals of class design. If not
you need to throw it away and find a good implementation. Frankly the
above code is so far from reasonable C++ in both design and
implementation that the writer of it needs to first learn about the
available tools provided by C++ (std::string, std::fstream etc.) before
attempting a redesign and fresh implementation. Alternatively you need
to find a good third party implementation of a socket class that meets
your needs.
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


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

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