LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 900|回复: 2

一个浏览器

[复制链接]
发表于 2004-2-29 14:23:11 | 显示全部楼层 |阅读模式
抄了一段别人的浏览器代码,自己编译成功了,不知道能不能,大家帮忙测试一下,
我的linux下不能上,麻烦大家了,

  1. #include <unistd.h>
  2. #include <sys/socket.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <stdarg.h>
  6. #include <netdb.h>
  7. #include <stdlib.h>

  8. int sock_connect(char *domain, int port)
  9. {
  10.         int        white_sock;
  11.         struct        hostent *site;
  12.         struct        sockaddr_in me;
  13.        
  14.         site = gethostbyname(domain);
  15.         if (site==NULL)
  16.                 return -2;
  17.        
  18.         white_sock = socket(AF_INET, SOCK_STREAM, 0);
  19.         if (white_sock<0)
  20.                 return -1;
  21.        
  22.         memset (&me, 0, sizeof(struct sockaddr_in));
  23.         memcpy (&me.sin_addr, site->h_addr_list[0], site->h_length);
  24.         me.sin_family = AF_INET;
  25.         me.sin_port = htons(port);

  26.         return ( connect(white_sock, (struct sockaddr *)&me, sizeof(struct sockaddr)) <0) ? -1 : white_sock;
  27. }

  28. int sock_send( int sock, char *fmt,...)
  29. {
  30.         char        BUF[1024];
  31.         va_list argptr;
  32.         va_start(argptr, fmt);
  33.         vsprintf( BUF, fmt, argptr);
  34.         va_end(argptr);
  35.         return send(sock, BUF, strlen(BUF), 0);
  36. }
  37.        
  38. int main( int argc, char *argv[])
  39. {
  40.         int         black_sock;
  41.         char        bugs_bunny[3];
  42.        
  43.         if (argc<2) return;
  44.        
  45.         black_sock = sock_connect(argv[1], 80);
  46.         if (black_sock<0)  return;
  47.         sock_send( black_sock, "GET / HTTP /1.0%c", 10);
  48.         sock_send( black_sock, "Host: %s%c", argv[1], 10);
  49.         sock_send( black_sock, "%c", 10);
  50.         while ( read(black_sock, bugs_bunny, 1) >0)
  51.         {
  52.                 printf("%c",bugs_bunny[0]);
  53.         }
  54.         close(black_sock);
  55. }


复制代码
发表于 2004-2-29 19:32:29 | 显示全部楼层
ok!没有问题。下面是我运行的结果:

[kj501@s2023 c_c++]$ gcc tt.c
[kj501@s2023 c_c++]$ ./a.out www.linuxsir.cn
HTTP/1.1 302 Found
Date: Sun, 29 Feb 2004 11:31:34 GMT
Server: Apache/2.0.48 (Unix) PHP/4.3.4
X-Powered-By: PHP/4.3.4
location: http://www.linuxsir.cn/postnuke/index.php
Content-Length: 0
Connection: close
Content-Type: text/html; charset=gb2312
X-Pad: avoid browser bug
发表于 2004-2-29 20:32:25 | 显示全部楼层
编译顺利通过:
就是结果有点奇怪??

# ./a.out www.freebsd.org
HTTP/1.1 400 Bad Request
Date: Sun, 29 Feb 2004 12:32:08 GMT
Server: Apache/1.3.x LaHonda (Unix)
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>400 Bad Request</TITLE>
</HEAD><BODY>
<H1>Bad Request</H1>
Your browser sent a request that this server could not understand.<>
The request line contained invalid characters following the protocol string.<>
<>
</BODY></HTML>

下面的和斑竹一样:
# ./a.out www.linuxsir.cn
HTTP/1.1 302 Found
Date: Sun, 29 Feb 2004 12:32:41 GMT
Server: Apache/2.0.48 (Unix) PHP/4.3.4
X-Powered-By: PHP/4.3.4
location: http://www.linuxsir.cn/postnuke/index.php
Content-Length: 0
Connection: close
Content-Type: text/html; charset=gb2312
X-Pad: avoid browser bug


:ask:ask
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表