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);
    }

  • 相关阅读:
    使用VS2013直接升级到VS2019,Cmake编译CCC4.0版本错误记录
    最近使用LuaSqlite3时,不得不说的——事务提交ATTACH语句的巨坑
    LuaSocket学习之tcp服务端
    LuaSocket HTTP 初识记录
    LuaRocks安装教程
    Lua5.1-----函数可变参数详解
    DataTable与json互转,字段类型信息丢失问题初探
    json数组与对象数组
    asp.net中封装路由信息的对象--RouteData源码
    人类的心理行为模式(几个心理学实验)
  • 原文地址:https://www.cnblogs.com/taobataoma/p/855279.html
Copyright © 2011-2022 走看看