strcat()问题 [英] strcat() Problems

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

问题描述



我有以下代码:


char request [] =" GET";

strcat(请求,路径); // Path是url的路径部分ie。 " /path/test.htm"

strcat(request," HTTP / 1.1");


cout<< request<< " \\\
英寸; //这输出GET /path/test.htm HTTP / 1.1愉快地


//发送数据包(我知道没有错误检查,这只是一个例子)

int numbytes;

numbytes = send(socknum,request,sizeof(request),0));


cout<<"试图发送"<< sizeof(request )<<" bytes,sent"<< numbytes<<" bytes.\\\
" ;;

//此行输出5个字节已尝试,5个字节已发送,这是我的问题


这个问题是当我检查发送的数据包(带有数据包
嗅探器)时,数据包只包含GET信息。 (它应该包含

" GET /path/test.htm HTTP / 1.1")。在我看来,只有变量

声明正确存储数据,并且strcat命令只是

''链接''那些数据......'(我'可能是错的,但那就是我(模糊)对

情况的看法,无论如何)


如何解决这个问题?我知道如果我声明一个固定长度的请求,

ie。

char request [1024] =" GET";

strcat (请求,路径);

strcat(请求," HTTP / 1.1");


输出所有内容,但请求用\填充0更混淆了

其他服务器。此外,如果路径比那个长,它会...... b
搞砸了......


感谢您提前提供任何帮助,

Patrick

解决方案

On Tue,02 Sep 2003 06:17:25 +0800,Patrick Coleman < PC ************* @ yahoo.com>写道:

我有以下代码:

char request [] =" GET" ;;


使用std :: string,因为


strcat(request,path); // Path是url的路径部分ie。 " /path/test.htm"


否则你很可能写入内存的任意部分,不知道,

就像你在这里一样


strcat(request," HTTP / 1.1");


和这里。


cout<< request<<" \ n" ;; //这输出GET /path/test.htm HTTP / 1.1愉快地


这可能会也可能不会工作取决于澳大利亚的天气情况。

//发送数据包(我知道没有错误检查,这只是一个例子)
int numbytes;
numbytes = send(socknum,request,sizeof(request),0));




使用std :: string,因为否则你可能会使用错误的运算符

和函数,就像你在这里一样。


On Mon,01 Sep 2003 22:25:50 +0000,Alf P. Steinbach写道:

On Tue,02 Sep 2003 06:17:25 +0800,Patrick Coleman" < PC ************* @ yahoo.com>写道:

< snip>使用std :: string,因为否则你可能会使用错误的操作符和函数,就像你在这里一样。




我试过用过一个std :: string,结果相同。

-Patrick




" Patrick Coleman" < PC ************* @ yahoo.com>在消息中写道

news:pa **************************** @ yahoo.com ... < blockquote class =post_quotes> On Mon,01 Sep 2003 22:25:50 +0000,Alf P. Steinbach写道:

On Tue,02 Sep 2003 06:17:25 +0800,Patrick Coleman,
< pc ************* @ yahoo.com>写道:


< snip>

使用std :: string,因为否则你可能会使用错误的
运算符和函数在这里。



我尝试过使用std :: string,结果相同。
-Patrick




你不能使用char变量[],就像它会神奇地增长并分配

内存一样。事实上,你说,除了道路比这更长的时候,它还要搞砸了意味着你需要一个更好的数据结构,

一个分配并释放它自己的内存。如前所述,

std :: string非常适合这项工作。如果你真的试一试并得到了

相同的结果我认为你不能正确使用该字符串。

使用std ::发布代码字符串,也许我们可以帮助您找到

问题所在的位置。除了使用std :: string之外,你必须制作一个足够大的

静态数组来处理任何情况。最后处理空字符

可以通过不发送该字节来完成。我不知道你是否使用

tcp或udp,而且两者都不是这个新闻组的主题,但如果你使用tcp而不是考虑投入一个循环你的发送功能

从缓冲区获取,检查和发送每个字节并退出

NULL字符而不是发送它。



克里斯托弗


Hi,
I have the following code:

char request[] = "GET ";
strcat(request, path); //Path is the path section of a url ie. "/path/test.htm"
strcat(request, " HTTP/1.1");

cout<<request<<"\n"; //This outputs "GET /path/test.htm HTTP/1.1" happily

//Send packet (I know there''s no error checking, this is just an example)
int numbytes;
numbytes = send(socknum, request, sizeof(request), 0));

cout<<"Tried to send "<<sizeof(request)<<" bytes, sent "<<numbytes<<" bytes.\n";
//This line outputs 5 bytes Tried, 5 bytes sent, which is my problem

The problem with this is that when I check the packet sent (with a packet
sniffer) the packets only contains "GET " (It should contain
"GET /path/test.htm HTTP/1.1"). It seems to me that only the variable
declaration is storing the data properly, and the strcat command just
''links'' that data somehow...(I''m probably wrong, but thats my (vague) idea of the
situation, anyway)

How can I get around this? I know if I declare request of a fixed length,
ie.
char request[1024] = "GET ";
strcat(request, path);
strcat(request, " HTTP/1.1");

It outputs everything, but request is padded with \0 which confuses the
other server even more. Besides, if the path is longer than that, it''ll
screw up...

Thanks for any help in advance,
Patrick

解决方案

On Tue, 02 Sep 2003 06:17:25 +0800, "Patrick Coleman" <pc*************@yahoo.com> wrote:

I have the following code:

char request[] = "GET ";
Use a std::string, because

strcat(request, path); //Path is the path section of a url ie. "/path/test.htm"
otherwise you''re likely to write to arbitrary parts of memory, willy-nilly,
like you do here

strcat(request, " HTTP/1.1");
and here.

cout<<request<<"\n"; //This outputs "GET /path/test.htm HTTP/1.1" happily
This may or may not "work" depending on the weather situation in Australia.
//Send packet (I know there''s no error checking, this is just an example)
int numbytes;
numbytes = send(socknum, request, sizeof(request), 0));



Use std::string, because otherwise you''re likely to use the wrong operators
and functions, as you do here.


On Mon, 01 Sep 2003 22:25:50 +0000, Alf P. Steinbach wrote:

On Tue, 02 Sep 2003 06:17:25 +0800, "Patrick Coleman" <pc*************@yahoo.com> wrote:
<snip> Use std::string, because otherwise you''re likely to use the wrong operators
and functions, as you do here.



I have tried using an std::string, with the same results.
-Patrick



"Patrick Coleman" <pc*************@yahoo.com> wrote in message
news:pa****************************@yahoo.com...

On Mon, 01 Sep 2003 22:25:50 +0000, Alf P. Steinbach wrote:

On Tue, 02 Sep 2003 06:17:25 +0800, "Patrick Coleman" <pc*************@yahoo.com> wrote:


<snip>

Use std::string, because otherwise you''re likely to use the wrong operators and functions, as you do here.



I have tried using an std::string, with the same results.
-Patrick



You can''t use char variable[] like it will magically grow and allocate
memory by itself. The fact that you said, "besides if the path is longer
than that, it''ll screw up" implies that you need a better data structure,
one that allocates and releases it''s memory by itself. As was said,
std::string is perfect for the job. If you really gave it a try and got the
same results I don''t think you could have been using the string properly.
Post the code using std::string and maybe we can help you find where the
problem is with it. Other than using std::string you will have to make a
static array big enough to handle any case. Dealing with the null character
at the end could be done by not sending that byte. I don''t know if yer using
tcp or udp, and both are really not on topic for this newsgroup, but if you
are using tcp than consider putting a loop around your send function
getting,checking, and sending each byte from the buffer and exiting on a
NULL character instead of sending it.
,
Christopher


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

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