Linux网络流媒体服务器的核心代码揭秘

配置环境

1,安装 xinetd :类似消息队列
# sudo apt-get install xinetd 或者 sudo aptitude show xinetd

2,打开 /etc/xinetd.d 文件下面创建 xhttp.d(可以随意取名)(注意三个文件名要统一)
mkdir xhttp.d //创建文件
(1,向文件中写入 配置

         services xhttpd
         {
              socket_type  = stream
              protocol     = tcp
              wait         = no
              user         = nobody
              server       = /home/chenli/dir/xhttpd    //服务的执行2文件
              server_args  = /home/chenli/dir
              disable      = no
              flags        = IPv4
         }

3, 打开/etc/services文件
到底 添加服务器的端口号
xhttpd 201601/tcp #xhttpd server
xhttpd 201601/tup #xhttpd server

4,重启xinetd 服务器 sudo service xinetd restart

程序的框架

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



#define N 4096

void send_err(错误号, 错误名称, 错误描述)
{
    <html>
     <head><title>错误号  错误名称</title></head>
     <body>
        错误描述
     </body>
    </html>
}


void send_handers(char *type)
{
    HTTP/1.1  200 OK
    Content-Type: text/plain;charset=iso-8859-1
    Connection:close
    \r\n
}

int main(int argc, char *argc[])
{
    char line[N];
    char method[N], path[N], protocol[N];

    char  *file;   //文件名

    struct stat sbuf;   //

    FILE *fp;

    char *ich;  //子
    if ( argc != 2)
        //send_err();
    if ( chdir(argv[1]) == -1) //判断文件路径是否是正确  工作目录
        //send_err();
    if (fgets(line, N, stdin) == NULL)   //GET /hello.c HTTP/1.1
        //send_err();
    if(sscanf(line, "%[^ ] %[^ ] %[^ ]", method, path, protocol) != 3)
        //send_err();
    while ( fgets( line, N, stdin) != NULL)  //判断文件有没有末
        if ( strcmp(line, "\r\n");       //浏览器的发过来的信息
            break;

    if ( strcmp(method, "GET") != 0)
         //send_err();

    if (path[0] !='/')     //  这个 反斜杠代表是   /home/chenli/dir
        //send_err();

    file = path + 1;    //获取文件名

    if (stat(file, &sbuf) < 0)   //没有文件存在
        //send_err();

    fp = fopen(file, "r");   

    if ( fp == NULL)  //判断有没有读取的权限
        //send_err();

    //send_hander();

    while((ich = getc(fp)) != EOF)
       putchar(ich);

    fflush(stdout);

    fclose(fp);  //关闭文件流
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值