深入理解计算机系统 CSAPP 家庭作业11.10

A:

复制代码
//home.html
<form action="/cgi-bin/adder" method="GET">
  <ul>
    <li>
      <label for="n1">n1:</label>
      <input type="text" id="n1" name="n1" /> //name的值决定页面提交后,生成的地址。
    </li>
    <li>
      <label for="n2">n2:</label>
      <input type="text" id="n2" name="n2" /> //name的值决定页面提交后,生成的地址。
    </li>
	<li class="button">
	<button type="submit">Send</button>
	</li>

  </ul>
</form>

home.html放在根目录.那样访问127.0.0.1:XX的时候会直接跳转到这个提交数据的页面.

html文件中的name决定用户点击Send按钮后跳转的地址后的参数名.

复制代码
//adder.c
#include "csapp.h"

int main(void) {
    char *buf, *p;
    char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE];
    int n1=0, n2=0;

    /* Extract the two arguments */
    if ((buf = getenv("QUERY_STRING")) != NULL) {
	p = strchr(buf, '&');
	*p = '\0';
	sscanf(buf,"n1=%d",&n1); 	//此处决定读取地址的格式
	sscanf(p+1,"n2=%d",&n2);    //此处决定读取地址的格式
    }

    /* Make the response body */
    sprintf(content, "Welcome to add.com: ");
    sprintf(content, "%sTHE Internet addition portal.\r\n<p>", content);
    sprintf(content, "%sThe answer is: %d + %d = %d\r\n<p>", 
	    content, n1, n2, n1 + n2);
    sprintf(content, "%sThanks for visiting!\r\n", content);
  
    /* Generate the HTTP response */
    printf("Connection: close\r\n");
    printf("Content-length: %d\r\n", (int)strlen(content));
    printf("Content-type: text/html\r\n\r\n");
    printf("%s", content);
    fflush(stdout);

    exit(0);
}

adder.c文件中的sscanf中的按格式输入是"n1=%d",这里是和html文件对应的. .adder程序是从html文件生成的地址中读取变量的.

相关推荐
别动我齐刘海3 小时前
Linux 系统调试学习2
linux·运维·c++·人工智能·学习·机器学习·机器人
龙虾电话3 小时前
语音机器人打断不进去:VAD 双阈值迟滞里一个共用的 bool,和它引发的连锁误判
linux
honsor3 小时前
机房IP化改造首选:以太网温湿度传感器,PoE供电+Modbus TCP一网搞定
linux·运维·服务器
默|笙4 小时前
【Linux网络】_udp_socket(认识)
linux·网络·udp
程序员梅雨4 小时前
ZooKeeper面经总结
java·大数据·linux·分布式·后端·zookeeper·中间件
lingran__4 小时前
Linux进程万字详解(二)|进程状态深度剖析:调度队列、内核链表、D状态设计哲学、僵尸孤儿进程与Slab机制
linux·进程管理·内核链表·pcb·僵尸进程·孤儿进程·进程状态
GeW5 小时前
以Linux为基、数据库为核:新质生产力路线图下的企业架构升级
linux
忆挽篱笙歌6 小时前
进程的状态
linux·ubuntu
kyle~6 小时前
Linux --- Socket 用户进程与内核网络协议栈之间的统一接口
linux·arm开发·网络协议
重生之小比特6 小时前
【Linux系统】07 进程状态
linux·运维·服务器