免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1822 | 回复: 7
打印 上一主题 下一主题

求大虾来指导! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-26 21:56 |只看该作者 |倒序浏览
use IO::Socket;
$sock = IO::Socket::INET->new ('172.16.6.37:80')
  or die "Socket could not be created.Because $!\n" unless $sock;
$sock->send("GET HTTP/1.1",0);

$data = <$sock>;
print $data;
close $sock;

为什么得不到服务端回显的信息呢?
PS:新手,还望手下留情啊~~

论坛徽章:
0
2 [报告]
发表于 2006-11-26 22:23 |只看该作者

  1. $sock->send("GET / HTTP/1.1\r\nHost:172.16.6.37\r\n\r\n\r\n",0);
复制代码

论坛徽章:
0
3 [报告]
发表于 2006-11-26 22:33 |只看该作者
谢楼上!
可以看到HTTP/1.1 200 OK的字样了
假如我要请求一个页面或文件,又应该怎么写呢?(http://172.16.6.37/test/test.html)

PS:还是没把HTTP header看懂!!

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2006-11-26 22:33 |只看该作者
原帖由 Namelessxp 于 2006-11-26 22:23 发表

  1. $sock->send("GET / HTTP/1.1\r\nHost:172.16.6.37\r\n\r\n\r\n",0);
复制代码

\r\n 是错误的。是不可移植的。正确的写法是直接用 \x0d \x0a 或者用模块导出的 CRLF

论坛徽章:
0
5 [报告]
发表于 2006-11-26 22:40 |只看该作者
原帖由 flw 于 2006-11-26 22:33 发表

\r\n 是错误的。是不可移植的。正确的写法是直接用 \x0d \x0a 或者用模块导出的 CRLF


这个姐姐更专业!!

论坛徽章:
0
6 [报告]
发表于 2006-11-26 22:41 |只看该作者
原帖由 Namelessxp 于 2006-11-26 22:23 发表

  1. $sock->send("GET /test/test.html HTTP/1.1\r\nHost:172.16.6.37\r\n\r\n\r\n",0);
复制代码


我照着改了一下就搞定了!!
呵呵,,

论坛徽章:
0
7 [报告]
发表于 2006-11-27 08:19 |只看该作者
原帖由 flw 于 2006-11-26 22:33 发表

\r\n 是错误的。是不可移植的。正确的写法是直接用 \x0d \x0a 或者用模块导出的 CRLF


这个倒没大注意,目前不管PHP还是Perl都是在Win平台下运行,看来是个隐患,受教

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
8 [报告]
发表于 2006-11-27 08:46 |只看该作者
原帖由 Namelessxp 于 2006-11-27 08:19 发表

这个倒没大注意,目前不管PHP还是Perl都是在Win平台下运行,看来是个隐患,受教

以下内容摘录自 perldoc perlport:
  1. Newlines

  2. In most operating systems, lines in files are terminated by newlines. Just what is used as a newline may vary from OS to OS. Unix traditionally uses \012, one type of DOSish I/O uses \015\012, and Mac OS uses \015.

  3. Perl uses \n to represent the ``logical'' newline, where what is logical may depend on the platform in use. In MacPerl, \n always means \015. In DOSish perls, \n usually means \012, but when accessing a file in ``text'' mode, STDIO translates it to (or from) \015\012, depending on whether you're reading or writing. Unix does the same thing on ttys in canonical mode. \015\012 is commonly referred to as CRLF.

  4. A common cause of unportable programs is the misuse of chop() to trim newlines:

  5.     # XXX UNPORTABLE!
  6.     while(<FILE>) {
  7.         chop;
  8.         @array = split(/:/);
  9.         #...
  10.     }

  11. You can get away with this on Unix and Mac OS (they have a single character end-of-line), but the same program will break under DOSish perls because you're only chop()ing half the end-of-line. Instead, chomp() should be used to trim newlines. The the Dunce::Files manpage module can help audit your code for misuses of chop().

  12. When dealing with binary files (or text files in binary mode) be sure to explicitly set $/ to the appropriate value for your file format before using chomp().

  13. Because of the ``text'' mode translation, DOSish perls have limitations in using seek and tell on a file accessed in ``text'' mode. Stick to seek-ing to locations you got from tell (and no others), and you are usually free to use seek and tell even in ``text'' mode. Using seek or tell or other file operations may be non-portable. If you use binmode on a file, however, you can usually seek and tell with arbitrary values in safety.

  14. A common misconception in socket programming is that \n eq \012 everywhere. When using protocols such as common Internet protocols, \012 and \015 are called for specifically, and the values of the logical \n and \r (carriage return) are not reliable.

  15.     print SOCKET "Hi there, client!\r\n";      # WRONG
  16.     print SOCKET "Hi there, client!\015\012";  # RIGHT

  17. However, using \015\012 (or \cM\cJ, or \x0D\x0A) can be tedious and unsightly, as well as confusing to those maintaining the code. As such, the Socket module supplies the Right Thing for those who want it.

  18.     use Socket qw(:DEFAULT :crlf);
  19.     print SOCKET "Hi there, client!$CRLF"      # RIGHT

  20. When reading from a socket, remember that the default input record separator $/ is \n, but robust socket code will recognize as either \012 or \015\012 as end of line:

  21.     while (<SOCKET>) {
  22.         # ...
  23.     }

  24. Because both CRLF and LF end in LF, the input record separator can be set to LF and any CR stripped later. Better to write:

  25.     use Socket qw(:DEFAULT :crlf);
  26.     local($/) = LF;      # not needed if $/ is already \012

  27.     while (<SOCKET>) {
  28.         s/$CR?$LF/\n/;   # not sure if socket uses LF or CRLF, OK
  29.     #   s/\015?\012/\n/; # same thing
  30.     }

  31. This example is preferred over the previous one--even for Unix platforms--because now any \015's (\cM's) are stripped out (and there was much rejoicing).

  32. Similarly, functions that return text data--such as a function that fetches a web page--should sometimes translate newlines before returning the data, if they've not yet been translated to the local newline representation. A single line of code will often suffice:

  33.     $data =~ s/\015?\012/\n/g;
  34.     return $data;

  35. Some of this may be confusing. Here's a handy reference to the ASCII CR and LF characters. You can print it out and stick it in your wallet.

  36.     LF  eq  \012  eq  \x0A  eq  \cJ  eq  chr(10)  eq  ASCII 10
  37.     CR  eq  \015  eq  \x0D  eq  \cM  eq  chr(13)  eq  ASCII 13

  38.              | Unix | DOS  | Mac  |
  39.         ---------------------------
  40.         \n   |  LF  |  LF  |  CR  |
  41.         \r   |  CR  |  CR  |  LF  |
  42.         \n * |  LF  | CRLF |  CR  |
  43.         \r * |  CR  |  CR  |  LF  |
  44.         ---------------------------
  45.         * text-mode STDIO

  46. The Unix column assumes that you are not accessing a serial line (like a tty) in canonical mode. If you are, then CR on input becomes ``\n'', and ``\n'' on output becomes CRLF.

  47. These are just the most common definitions of \n and \r in Perl. There may well be others. For example, on an EBCDIC implementation such as z/OS (OS/390) or OS/400 (using the ILE, the PASE is ASCII-based) the above material is similar to ``Unix'' but the code numbers change:

  48.     LF  eq  \025  eq  \x15  eq  \cU  eq  chr(21)  eq  CP-1047 21
  49.     LF  eq  \045  eq  \x25  eq           chr(37)  eq  CP-0037 37
  50.     CR  eq  \015  eq  \x0D  eq  \cM  eq  chr(13)  eq  CP-1047 13
  51.     CR  eq  \015  eq  \x0D  eq  \cM  eq  chr(13)  eq  CP-0037 13

  52.              | z/OS | OS/400 |
  53.         ----------------------
  54.         \n   |  LF  |  LF    |
  55.         \r   |  CR  |  CR    |
  56.         \n * |  LF  |  LF    |
  57.         \r * |  CR  |  CR    |
  58.         ----------------------
  59.         * text-mode STDIO
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP