zoukankan      html  css  js  c++  java
  • 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);
    }

  • 相关阅读:
    js记录
    快速最好响应式布局(CSS3)
    CSS3终极动画制作属性animation
    虚拟机中试用windows 8(视频)
    平板电脑上完美体验Windows 8 (视频)
    面对电磁泄漏您的电脑还安全吗?--计算机设备信息泄漏揭秘
    基于Linux平台Softimage XSI 演示
    为您的Office文档加把锁-ADRMS的安装
    VNC Server模拟攻击实战
    暴力破解FTP服务器技术探讨与防范措施
  • 原文地址:https://www.cnblogs.com/taobataoma/p/855279.html
Copyright © 2011-2022 走看看