Chinaunix首页 | 论坛 | 博客
  • 博客访问: 279707
  • 博文数量: 109
  • 博客积分: 2116
  • 博客等级: 大尉
  • 技术积分: 1062
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-22 15:38
文章分类

全部博文(109)

文章存档

2013年(2)

2011年(16)

2010年(90)

2009年(1)

我的朋友

分类: 嵌入式

2010-08-10 11:11:13

完成一个webserver的实验。

 

原理:

1 socket编程

2 http语言编程

3 tcp协议

 

步骤:

1 在主机端编程,交叉编译,调试。

2 在开发板运行服务器webserver

3 在主机端打开浏览器,输入 (开发板的IP地址)

从上面看出开发板上的服务器打开以后,在主机端浏览器下可以访问此IP地址对应的网页

 

源代码来自周立功:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

 

#define PORT    81

#define BACKLOG 10

#define LENGTH  1024

 

char httpweb[]={

    "HTTP/1.0 200 OK\r\n"

    "Date: Mon,24 Nov 2005 10:26:00 GMT\r\n"

    "Server: microHttp/1.0 ZHIYUAN Electronics CO.,LTD\r\n"

    "Accept-Ranges:bytes\r\n"

    "Connection:Keep-Close\r\n"

    "Content-Type:text/html\r\n"

    "\r\n"

};

 

char web[]={

    "\r\n"

    "\r\n"

    "Magic2410演示网页(广州致远电子有限公司)\r\n"

    ""

    "\r\n"

    "

HELLO WELCOME TO MagicARM2410 WEBSERVER

\r\n"

    "

    \r\n"

        "广州致远电子\r\n"

        " \r\n"

        "\r\n"

    };

     

    char httpgif[]={

         "HTTP/1.0 200 OK\r\n"

            "Date: Mon,24 Nov 2005 10:26:00 GMT\r\n"

            "Server: microHttp/1.0 ZHIYUAN Electronics CO.,LTD\r\n"

            "Accept-Ranges:bytes\r\n"

            "Connection:Keep-Close\r\n"

            "Content-Type:text/html\r\n"

            "\r\n"

    };

     

    int main()

    {

        int sockfd;

        int nsockfd;

        int num;

    //  int flag = 1;

        int sin_size;

        char revbuf[LENGTH];

        struct sockaddr_in addr_local;

        struct sockaddr_in addr_remote;

     

        if((sockfd = socket(AF_INET, SOCK_STREAM, 0))== -1){

            printf("ERROR: Cannot obtain Socket Desperitor.\n");

            return (0);

        }else{

            printf("OK:Obtain Socket Desperitor Successfully.\n");

        }

     

        addr_local.sin_family = AF_INET;

        addr_local.sin_port = htons(PORT);

        addr_local.sin_addr.s_addr = INADDR_ANY;

        bzero(&(addr_local.sin_zero), 8);

     

        if(bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1){

            printf("ERROR: Cannot bind Port %d\n", PORT);

            return (0);

        } else{

            printf("OK: Bind the Port %d successfuly.\n", PORT);

        }

     

        if(listen(sockfd, BACKLOG) == -1){

            printf("ERROR: Cannot listen Port %d\n", PORT);

            return (0);

        }else{

            printf("OK: Listening the Port %d successfuly.\n", PORT);

        }

     

        while(1){

            sin_size = sizeof(struct sockaddr_in);

     

            if((nsockfd = accept(sockfd, (struct sockaddr*)&addr_remote, &sin_size))== -1){

            printf("ERROR: Obtain new Socket Despcritor error.\n");

            continue;//跳过本次循环

        }else{

            printf("OK: Server had got connect from %s\n", inet_ntoa(addr_remote.sin_addr));

        }

     

        num = recv(nsockfd, revbuf, LENGTH, 0);

       

        if(!fork()){

            printf("OK: Http web is servering.\n");

     

            if(revbuf[5] == ' '){

                send(nsockfd, httpweb, sizeof(httpweb), 0);

                send(nsockfd, web, sizeof(web), 0);

            }

            else if(revbuf[5] == '1'){

                send(nsockfd, httpgif, sizeof(httpgif), 0);

            }

        }

        close(nsockfd);

        while(waitpid(-1, NULL, WNOHANG) > 0);

        }

    }

阅读(562) | 评论(0) | 转发(0) |
0

上一篇:TCP

下一篇:hello驱动

给主人留下些什么吧!~~