北京电子科技学院(BESTI)

             

课程:信息安全系统设计基础                班级:  1353

姓名:20135303 魏昊卿

学号:20135326 王亦可

成绩:             指导教师: 娄嘉鹏             实验日期:2015.12.03

实验密级:         预习程度:                     实验时间:15:30~18:00

仪器组次:          必修/选修:必修              实验序号:5

实验名称:   2.7简单嵌入式WEB服务器实验

实验目的与要求:         1.掌握在ARM开发板实现一个简单 WEB服务器的过程

2.学习在ARM开发板上的socket网络编程

3.学习linux下的signal()函数的使用

实验仪器:

名称

型号

数量

计算机

1

嵌入式开发平台

UP-NETARM2410-CL

1

实验内容、步骤与体会:

1.配置实验环境

2.阅读理解实验源码。

  进入07_httpd文件夹,使用vi编辑器阅读理解源代码。

3.编译应用程序

4.下载调试

使用NFS服务方式将HPPTD下载到开发板上,并拷贝测试用的网页进行调试。(此处忘记截图)

使用如下命令:

mount -t nfs -o nolock 192.168.0.234:/home/bc /host(手册中的指导使用/root/bc /host命令)

进入07_httpd目录后,输入./httpd 命令

5.本机调试

在台式机的浏览器中输入http://192.168.0.121,观察在客户机的浏览器中的连接请求结果和在开发板上的服务器的打印信息

重要代码:

//makefile
TOPDIR = ../
include $(TOPDIR)Rules.mak
EXTRA_LIBS += -lpthread

EXEC = $(INSTALL_DIR)/httpd  ./httpd
OBJS = httpd.o copy.o

HTTPD_DOCUMENT_ROOT = /mnt/yaffs
CFLAGS += -DHTTPD_DOCUMENT_ROOT=\"$(HTTPD_DOCUMENT_ROOT)\"

all: $(EXEC)

$(EXEC): $(OBJS)
    $(CC) $(LDFLAGS) -o $@ $(OBJS) $(EXTRA_LIBS)

install:
    $(EXP_INSTALL) $(EXEC) $(INSTALL_DIR)

clean:
    -rm -f $(EXEC) *.elf *.gdb *.o
/* httpd.c:  A very simple http server
 * Copyfight (C) 2003      Zou jian guo <ah_zou@163.com>
 * Copyright (C) 2000         Lineo, Inc.  (www.lineo.com)
 * Copyright (c) 1997-1999 D. Jeff Dionne <jeff@lineo.ca>
 * Copyright (c) 1998      Kenneth Albanowski <kjahds@kjahds.com>
 * Copyright (c) 1999      Nick Brok <nick@nbrok.iaehv.nl>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/stat.h>
#include <dirent.h>
#include <signal.h>
#include <unistd.h>
#include <ctype.h>
#include "pthread.h"

#define DEBUG

;
;

#ifndef O_BINARY
#define O_BINARY 0
#endif

];
int content_length;

#define SERVER_PORT 80

int PrintHeader(FILE *f, int content_type)
{
  alarm(TIMEOUT);
  fprintf(f,"HTTP/1.0 200 OK\n");
  switch (content_type)
  {
   case 't':
    fprintf(f,"Content-type: text/plain\n");
    break;
   case 'g':
    fprintf(f,"Content-type: image/gif\n");
    break;
   case 'j':
    fprintf(f,"Content-type: image/jpeg\n");
    break;
   case 'h':
    fprintf(f,"Content-type: text/html\n");
    break;
  }
  fprintf(f,"Server: uClinux-httpd 0.2.2\n");
  fprintf(f,"Expires: 0\n");
  fprintf(f,"\n");
  alarm();
  );
}

int DoJpeg(FILE *f, char *name)//发送jpeg图像文件内容
{
  char *buf;
  FILE * infile;
  int count;

  if (!(infile = fopen(name, "r"))) {
    alarm(TIMEOUT);
    fprintf(stderr, "Unable to open JPEG file %s, %d\n", name, errno);
    fflush(f);
    alarm();
    ;
  }

  PrintHeader(f,'j');    

  copy(infile,f); /* prints the page */

  alarm(TIMEOUT);
  fclose(infile);
  alarm();

  ;
}

int DoGif(FILE *f, char *name)
{
  char *buf;
  FILE * infile;
  int count;

  if (!(infile = fopen(name, "r"))) {
    alarm(TIMEOUT);
    fprintf(stderr, "Unable to open GIF file %s, %d\n", name, errno);
    fflush(f);
    alarm();
    ;
  }

  PrintHeader(f,'g');

  copy(infile,f); /* prints the page */  

  alarm(TIMEOUT);
  fclose(infile);
  alarm();

  ;
}

int DoDir(FILE *f, char *name)//发送当前目录文件列表信息
{
  char *buf;
  DIR * dir;
  struct dirent * dirent;

  ) {
    fprintf(stderr, "Unable to open directory %s, %d\n", name, errno);
    fflush(f);
    ;
  }

  PrintHeader(f,'h');

  alarm(TIMEOUT);
  fprintf(f, "<H1>Index of %s</H1>\n\n",name);
  alarm();

  ] != '/') {
    strcat(name, "/");
  }

  while(dirent = readdir(dir)) {
    alarm(TIMEOUT);

    fprintf(f, "<p><a href=\"/%s%s\">%s</a></p>\n", name, dirent->d_name, dirent->d_name);
    alarm();
  }

  closedir(dir);
  ;
}

int DoHTML(FILE *f, char *name)//发送html文件内容
{
  char *buf;
  FILE *infile;
  int count;
  ;

  if (!(infile = fopen(name,"r"))) {
    alarm(TIMEOUT);
    fprintf(stderr, "Unable to open HTML file %s, %d\n", name, errno);
    fflush(f);
    alarm();
    ;
  }

  PrintHeader(f,'h');
  copy(infile,f); /* prints the page */  

  alarm(TIMEOUT);
  fclose(infile);
  alarm();

  ;
}

int DoText(FILE *f, char *name)//发送纯文本文件内容
{
  char *buf;
  FILE *infile;
  int count;

  if (!(infile = fopen(name,"r"))) {
    alarm(TIMEOUT);
    fprintf(stderr, "Unable to open text file %s, %d\n", name, errno);
    fflush(f);
    alarm();
    ;
  }

  PrintHeader(f,'t');
  copy(infile,f); /* prints the page */  

  alarm(TIMEOUT);
  fclose(infile);
  alarm();

  ;
}

int ParseReq(FILE *f, char *r)//解析客户请求
{
      char *bp;
      struct stat stbuf;
      char * arg;
      char * c;
      int e;
      int raw;

#ifdef DEBUG
      printf("req is '%s'\n", r);
#endif

      while(*(++r) != ' ');  /*skip non-white space*/
      while(isspace(*r))
          r++;

      while (*r == '/')
          r++;
      bp = r;

      while(*r && (*(r) != ' ') && (*(r) != '?'))
          r++;

#ifdef DEBUG
      printf("bp='%s' %x, r='%s' \n", bp, *bp,r);
#endif

      if (*r == '?')
      {
          char * e;
          *r = ;
          arg = r+;
          if (e = strchr(arg,' '))
        {
              *e = '\0';
          }
      } else
    {
          arg = ;
          *r = ;
    }

      c = bp;

/*zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz*/
      ] == 0x20){
        c[]='.';
        c[]='\0';
    }
/*zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz*/
    ] == '\0') strcat(c,".");

    if (c && !stat(c, &stbuf))
      {
        if (S_ISDIR(stbuf.st_mode))
        {
            char * end = c + strlen(c);
            strcat(c, "/index.html");
            if (!stat(c, &stbuf))
            {
                DoHTML(f, c);
            }
            else
            {
                  *end = '\0';
                DoDir(f,c);
            }
        }
        , ".gif"))
              DoGif(f,c);
        , , ".jpeg"))
              DoJpeg(f,c);
        , , ".html"))
            DoHTML(f,c);
             else
                  DoText(f,c);
    }
    else{
          PrintHeader(f,'h');//发送http协议数据头
          alarm(TIMEOUT);
          fprintf(f, "<html><head><title>404 File Not Found</title></head>\n");
        fprintf(f, "<body>The requested URL was not found on this server</body></html>\n");
          alarm();
    }
      ;
}

void sigalrm(int signo)
{
    /* got an alarm, exit & recycle */
    exit();
}

int HandleConnect(int fd)
{
  FILE *f;

  ];
  ];

  f = fdopen(fd,"a+");
  if (!f) {
    fprintf(stderr, "httpd: Unable to open httpd input fd, error %d\n", errno);
    alarm(TIMEOUT);
    close(fd);
    alarm();
    ;
  }
  setbuf(f, );

  alarm(TIMEOUT);

  , f)) {
    fprintf(stderr, "httpd: Error reading connection, error %d\n", errno);
    fclose(f);
    alarm();
    ;
  }
#ifdef DEBUG
      printf("buf = '%s'\n", buf);
#endif

      alarm();

      referrer[] = '\0';
      content_length = -;

     alarm(TIMEOUT);
    //read other line to parse Rrferrer and content_length infomation
    , f) && (strlen(buf1) > )) {
          alarm(TIMEOUT);
        #ifdef DEBUG
            printf("Got buf1 '%s'\n", buf1);
        #endif
        )) {
              ;
              while (isspace(*c))
                c++;
            strcpy(referrer, c);
        }
        )) {
              ;
              while (isspace(*c))
                c++;
              strcpy(referrer, c);
        }
        )) {
              content_length = atoi(buf1+);
        }
      }
      alarm();

      if (ferror(f)) {
        fprintf(stderr, "http: Error continuing reading connection, error %d\n", errno);
        fclose(f);
        ;
      }    

      ParseReq(f, buf);

      alarm(TIMEOUT);
      fflush(f);
      fclose(f);
      alarm();
      ;
}

void* key(void* data)
{
    int c;
    for(;;){
        c=getchar();
        if(c == 'q' || c == 'Q'){
            KEY_QUIT=;
            exit();
            break;
        }
    }

}

int main(int argc, char *argv[])
{
  int fd, s;
  int len;
  ;
  struct sockaddr_in ec;
  struct sockaddr_in server_sockaddr;

  pthread_t th_key;
  void * retval;

  signal(SIGCHLD, SIG_IGN);
  signal(SIGPIPE, SIG_IGN);
  signal(SIGALRM, sigalrm);

  chroot(HTTPD_DOCUMENT_ROOT);
  printf("starting httpd...\n");
  printf("press q to quit.\n");
//  chdir("/");

   && !strcmp(argv[], "-i")) {
    /* I'm running from inetd, handle the request on stdin */
    fclose(stderr);
    HandleConnect();
    exit();
  }

  ) {
    perror("Unable to obtain network");
    exit();
  }

  if((setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&true,
         ) {
    perror("setsockopt failed");
    exit();
  }

  server_sockaddr.sin_family = AF_INET;
  server_sockaddr.sin_port = htons(SERVER_PORT);
  server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);

  if(bind(s, (struct sockaddr *)&server_sockaddr,
      )  {
    perror("Unable to bind socket");
    exit();
  }

  *) == -) { /* Arbitrary, 8 files/page, 3 clients */
    perror("Unable to listen");
    exit();
  }

       pthread_create(&th_key, NULL, key, );
  /* Wait until producer and consumer finish. */
  printf("wait for connection.\n");
  ) {

    len = sizeof(ec);
    ) {
      exit();
      close(s);
    }
    HandleConnect(fd);

  }
  pthread_join(th_key, &retval);
}

实验过程中遇到的问题以及解决方案

在使用make编译的过程中出现错误,如下图:

解决方法:

有两种解决办法,其一是服务器代码使用交叉编译器进行编译,客户端代码使用gcc编译),另一种是修改makefile文件。

我们采取的是第二种方法,如下图:

实验5 简单嵌入式WEB服务器实验 实验报告 20135303 20135326的更多相关文章

  1. 20145216 20145330 《信息安全系统设计基础》 实验五 简单嵌入式WEB 服务器实验

    20145216 20145330 <信息安全系统设计基础> 实验五 简单嵌入式WEB 服务器实验 实验报告封面 实验步骤 1.阅读理解源码 进入/arm2410cl/exp/basic/ ...

  2. 20145208《信息安全系统设计基础》实验五 简单嵌入式WEB 服务器实验

    20145208<信息安全系统设计基础>实验五 简单嵌入式WEB 服务器实验 20145208<信息安全系统设计基础>实验五 简单嵌入式WEB 服务器实验

  3. 20145210 20145226 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验

    20145210 20145226 <信息安全系统设计基础>实验五 简单嵌入式WEB服务器实验 结对伙伴:20145226 夏艺华 实验报告封面 实验目的与要求 · 掌握在ARM开发板实现 ...

  4. 20145221 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验

    20145221 <信息安全系统设计基础>实验五 简单嵌入式WEB服务器实验 实验报告 队友博客:20145326蔡馨熠 实验博客:<信息安全系统设计基础>实验五 简单嵌入式W ...

  5. 20145303 20145339 《信息安全系统设计基础》 实验五 简单嵌入式WEB服务器实验

    20145303 20145339 <信息安全系统设计基础> 实验五 简单嵌入式WEB服务器实验 实验目的与要求 1.掌握在ARM开发板实现一个简单WEB服务器的过程 2.学习在ARM开发 ...

  6. 学号20145332 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验

    实验目的 掌握在 ARM 开发板实现一个简单 WEB 服务器的过程. 学习在 ARM 开发板上的 SOCKET 网络编程. 学习 Linux 下的 signal()函数的使用. 实验内容 学习使用 s ...

  7. 20145202、20145225、20145234 《信息安全系统设计基础》实验五 简单嵌入式WEB 服务器实验

    实验内容 1.配置环境 2.使用vi 编辑器阅读理解源码 2.编译应用程序 运行 make 产生可执行文件httpd 3.下载调试 使用 NFS 服务方式将HTTPD 下载到开发板上,并拷贝测试用的网 ...

  8. 20145324 20145325《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验

    实验五报告链接为:http://www.cnblogs.com/SJZGM10/p/6106668.html

  9. 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验

    实验报告链接:http://www.cnblogs.com/lx20145332/p/6058790.html

随机推荐

  1. 有用的.NET库

    1. Polly,重试 Polly is a .NET 3.5 / 4.0 / 4.5 / PCL library that allows developers to express transien ...

  2. Android程序入口以及项目文件夹的含义和使用总结—入门

    新接触一门程序或者开发框架,我一般都要先弄清楚程序的入口在哪里,程序怎么运行的:建立一个项目后,各个文件夹有什么作用以及如何使用等等.理清楚这些东西对以后开发是很有好处的,古话说得好,工欲善其事,必先 ...

  3. VISUAL STUDIO 调试

    调试术语 Visual Studio调试之断点基础篇 Visual Studio调试之断点进阶篇 不能设置断点的检查步骤 Visual Studio调试之断点技巧篇 Visual Studio调试之断 ...

  4. proxool详细配置

    proxool详细配置 博客分类: Java 配置管理SQLServletprototypeXML  proxool一个数据库连接池框架,提供了对你选择的其它类型的驱动程序的连接池封装.可以非常简单的 ...

  5. Redis的数据类型及操作

    Strings 最简单的类型,一个Key对应一个Value,string类型是二进制安全的.Redis的string可以包含任何数据,如图片或序列化的对象 操作 Set:设置key对应的值为strin ...

  6. nyoj 203 三国志 dijkstra+01背包

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=203 思路:先求点0到每个点的最短距离,dijkstra算法,然后就是01背包了 我奇怪的 ...

  7. Error during installing HAXM, VT-X not working 在安装HAXM错误,开始不工作

    最佳答案 (Best Answer) Some antivirus options prevent Haxm installation. ie: Avast : settings (parametre ...

  8. C++ new(1)

    如果找工作的同学看一些面试的书,我相信都会遇到这样的题:sizeof 不是函数,然后举出一堆的理由来证明 sizeof 不是函数.在这里,和 sizeof 类似,new 和 delete 也不是函数, ...

  9. Hive history date mapping

    Hive history table mapping create table fdl_family as select * from (select 'acc1' as account,'famil ...

  10. css中元素居中总结

    很多时候,我们需要让元素居中显示:1. 一段文本的水平居中,2. 一张图片的水平居中,3. 一个块级元素的水平居中:4. 单行文本的竖直居中,5. 不确定高度的一段文本竖直居中,6. 确定高度的块级元 ...