本帖最后由 jf_1137202360 于 2022-12-22 12:15 编辑
前言 本开发板接口i资源丰富性能也比较强,所以特别适合作为网关终端使用。网关终端一般需要给用户提供配置平台用于参数配置,一般的实现方式有通过串口或者网口和上位机通讯,需要专门的上位机提供GUI工具配置或者命令行配置;还有一种方式就是开发板提供http服务,通过网页配置,后者更方便,因为不需要开发上位机GUI工具,直接网页登录即可配置。 我们这一篇就演示下该方式的实现。
准备
从以下地址https://sourceforge.net/projects/tinyhttpd/下载源码 解压
修改代码 Httpd.c中 注释掉
int namelen = sizeof(name);改为 socklen_t namelen = sizeof(name);
int client_name_len = sizeof(client_name); 改为 socklen_t client_name_len = sizeof(client_name);
注释掉 //pthread_t newthread;
//if (pthread_create(&newthread , NULL, accept_request, client_sock) != 0) // perror("pthread_create");
取消注释 accept_request(client_sock);
源码如下
/* J. David's webserver */
/* This is a simple webserver.
* Created November 1999 by J. David Blackstone.
* CSE 4344 (Network concepts), Prof. Zeigler
* University of Texas at Arlington
*/
/* This program compiles for Sparc Solaris 2.6.
* To compile for Linux:
* 1) Comment out the #include line.
* 2) Comment out the line that defines the variable newthread.
* 3) Comment out the two lines that run pthread_create().
* 4) Uncomment the line that runs accept_request().
* 5) Remove -lsocket from the Makefile.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#include
#define ISspace(x) isspace((int)(x))
#define SERVER_STRING "Server: jdbhttpd/0.1.0rn"
void accept_request(int);
void bad_request(int);
void cat(int, FILE *);
void cannot_execute(int);
void error_die(const char *);
void execute_cgi(int, const char *, const char *, const char *);
int get_line(int, char *, int);
void headers(int, const char *);
void not_found(int);
void serve_file(int, const char *);
int startup(u_short *);
void unimplemented(int);
/**********************************************************************/
/* A request has caused a call to accept() on the server port to
* return. Process the request appropriately.
* Parameters: the socket connected to the client */
/**********************************************************************/
void accept_request(int client)
{
char buf[1024];
int numchars;
char method[255];
char url[255];
char path[512];
size_t i, j;
struct stat st;
int cgi = 0; /* becomes true if server decides this is a CGI
* program */
char *query_string = NULL;
numchars = get_line(client, buf, sizeof(buf));
i = 0; j = 0;
while (!ISspace(buf[j]) && (i < sizeof(method) - 1))
{
method = buf[j];
i++; j++;
}
method = ' |