dumps the server root page (index.htm) to the screen

简介:
/*  ------------------------------------------------------------------------ *
 *                                                                          *
 * socket-test.c                                                            *
 *                                                                          *
 * This program demonstrates the communication with a web server            *
 * and dumps the server root page (index.htm) to the screen.                *
 *                                                                          *
 * this program has been written and tested with apache 1.3.28              *
 * and implements the functions from Beej's Guide to Network Programming.   *
 *                            *
 *                                                                          *
 * 20041117 frank4dd                                                        *
 * ------------------------------------------------------------------------ 
*/

#include 
< stdio.h >
#include 
< string .h >
#include 
< unistd.h >
#include 
< stdlib.h >
#include 
< sys / types.h >
#include 
< sys / socket.h >
#include 
< netinet / in .h >
#include 
< arpa / inet.h >

#define  DEST_IP "192.168.11.8"
#define  DEST_PORT 80
#define  MAXDATASIZE 1024

int  main() {

  
int  i, sum;
  
int  sockfd;
  
int  buflen;
  
int  retcode;
  
struct  sockaddr_in dest_addr;
  
char  buf[MAXDATASIZE];

  printf(
" Creating socket file descriptor.\n " );

  sockfd 
=  socket(AF_INET, SOCK_STREAM,  0 );

  printf(
" Loading content into filedescriptor.\n " );

  dest_addr.sin_family
= AF_INET;
  dest_addr.sin_port
= htons(DEST_PORT);
  dest_addr.sin_addr.s_addr
= inet_addr(DEST_IP);
  printf(
" Zeroing the rest of the struct.\n " );
  memset(
& (dest_addr.sin_zero),  ' \0 ' 8 );  //  zero the rest of the struct

  printf(
" Try connecting to %s.\n " , DEST_IP);

  
if  ( connect(sockfd, ( struct  sockaddr  * & dest_addr,
                              
sizeof ( struct  sockaddr))  ==   - 1  ) {
    perror(
" Error connecting " );
    exit(
1 );
  } 
else  {
    printf(
" Connected to %s.\n " , DEST_IP);
  }

  printf(
" Sending data request to %s.\n " , DEST_IP);

  sprintf(buf, 
" GET / HTTP/1.1\r\nHost: DEST_IP\r\nConnection: close\r\n\r\n " );
  buflen
= strlen(buf);
  retcode 
=  write(sockfd, buf, buflen);

  printf(
" Receiving data from %s.\n " , DEST_IP);

  sum
= 0 ;

  
do  {

    retcode
= read(sockfd, buf, MAXDATASIZE);
    sum
= sum + retcode;
    
if (retcode  >   0 ) {
      
for (i = 0 ;i < retcode;i ++ ) {
        putchar(buf[i]);
      }
    } 
else   break ;
  }
  
while  ( 1 );

  printf(
" Received %d bytes of data from %s.\n " , sum, DEST_IP);

  printf(
" Closing connection to %s.\n " , DEST_IP);
  close(sockfd);
  exit(
0 );

}



    本文转自 OldHawk  博客园博客,原文链接:http://www.cnblogs.com/taobataoma/archive/2007/08/14/855279.html,如需转载请自行联系原作者




相关文章
|
SQL 算法
【hacker的错误集】html5lib使用报错Couldn‘t find a tree builder with the features you requested: html5lib
分析得出:bs4的特征没有找到:找不到具有您请求功能的树生成器:html5lib。您需要安装解析器库吗?
413 0
【hacker的错误集】html5lib使用报错Couldn‘t find a tree builder with the features you requested: html5lib
|
应用服务中间件 数据安全/隐私保护 容器
websphere Error 404:SRVE0190E: File not found: index.action
websphere Error 404:SRVE0190E: File not found: index.action
|
移动开发 监控 安全
HTML访问本地路径报错 Not allowed to load local resource
HTML访问本地路径报错 Not allowed to load local resource
HTML访问本地路径报错 Not allowed to load local resource
|
JavaScript 关系型数据库 PHP
Notice: Undefined index: user in D:\phpStudy\WWW\js\ls\lsmc\php\add.php on line 9
原文:Notice: Undefined index: user in D:\phpStudy\WWW\js\ls\lsmc\php\add.php on line 9 (初用数据库(mysql)做用户登录注册这一块,遇到很多问题,通过搜索找到解决方案,把问题分享出来,希望可以帮助到和我一样问题的博友们,有问题还望多多指教。
1805 0
|
Spring
Cannot find template location(s): [classpath:/templates/]
版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢。 https://blog.csdn.net/testcs_dn/article/details/80914838 ...
5309 0
|
前端开发 移动开发
RN Exception: Failed to load http://localhost:8081/index.delta?platform=android&dev=true&minify=f...
异常 React Native调试时报如下错误 Failed to load http://localhost:8081/index.delta?platform=android&dev=true&minify=false: No 'Access-Co...
3003 0
|
应用服务中间件 nginx