Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

simply web server by unix c program

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/wait.h>
#define QLEN 20
#define APPLY_HEAD "HTTP/1.1 200 OK\r\nContent-Type: "
#define IMG "image/jpg"
#define TEXT "text/html"
#define APPLY_END "\r\n\r\n"
typedef struct
{
in_port_t port;
char root[1024];
} cnf_t;
void err_sys (char *str) { perror (str); exit (1); }
cnf_t * config (cnf_t * arg)
{
FILE *fp;
char buf[1024];
char *p;
static cnf_t backup;
if (NULL == arg)
arg = &backup;
fp = fopen ("config.ini", "r");
if (fp == NULL)
err_sys ("fail to fopen");
fgets (buf, 1024, fp);
p = strstr (buf, "=");
arg->port = atoi (p + 1);
fgets (buf, 1024, fp);
p = strstr (buf, "=");
strcpy (arg->root, p + 1);
arg->root[strlen (arg->root) - 1] = '\0';
fclose (fp);
return arg;
}
int socket_init (in_port_t port)
{
int lfd, n;
struct sockaddr_in serv_addr;
lfd = socket (AF_INET, SOCK_STREAM, 0);
if (lfd == -1)
err_sys ("fail to socket");
bzero (&serv_addr, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons (port);
inet_pton (AF_INET, "192.168.0.18", &serv_addr.sin_addr.s_addr);
n = bind (lfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr));
if (n == -1)
err_sys ("fail to bind");
n = listen (lfd, QLEN);
if (n == -1)
err_sys ("fail to listen");
return lfd;
}
char *get_path(int cfd, const char *root)
{
char *path; int n;
char *p, *q, *filename;
char buf[1024];
path = (char *)malloc(sizeof(char) * 1024); //or static char path[1024];
if (NULL == path) err_sys("fail to malloc ");
n = read (cfd, buf, 1024);
buf[n]


Ïà¹ØÎĵµ£º

Cº¯ÊýÖ¸ÕëµÄÓ÷¨

   
2008-06-05 12:38------------------------------------------------------------------------------------------------
º¯ÊýÖ¸Õëͨ³£ÓÃÀ´ÊµÏֻص÷£¬Æä»ù±¾Ó÷¨ÈçÏ£º
1¡¢¶¨Ò庯ÊýÖ¸ÕëÀàÐÍ
// ¶¨ÒåÒ»¸öÔ­ÐÍΪint Fun( int a );µÄº¯ÊýÖ¸Õë
typedef int (*PTRFUN) ( int aPara );
2¡¢º¯ÊýÖ¸Õë±äÁ¿µ ......

CλÔËËã

12.1       λÔËËã·û£ÃÓïÑÔÌṩÁËÁùÖÖλÔËËã·û£º
    &          °´Î»Óë
    |          °´Î»»ò
    ^     &nb ......

C/C++

9.2  ¹²ÓÃÌå
CÓïÑÔ³ýÁËÌṩ½á¹¹ÌåÕâÖÖ¿É°üº¬¶àÖÖÀàÐÍÊý¾ÝµÄ¹¹ÔìÀàÐÍÍ⣬»¹ÌṩÁËÒ»ÖÖ´ÓÐÎʽÉÏ¿´ºÍ½á¹¹Ìå¿°³Æ“ÂÏÉúÐֵܔµÄ¹¹ÔìÀàÐÍ——¹²ÓÃÌ壨union£©¡£
±¾½Ú´Ó¹²ÓÃÌåµÄ¸ÅÄîÈëÊÖ£¬´Ó¹²ÓÃÌåµÄ¸ÅÄî¡¢Óë½á¹¹ÌåµÄÒìͬ¡¢Ê¹Óõȷ½Ãæ½øÐÐÏêϸµÄ½éÉÜ¡£
9.2.1  ʲôÊǹ²ÓÃÌå
ÏÖʵÉú»îÖУ¬Ä³Ð©Ê ......

cĬÈÏÊÇ´«ÖµµÄ

ÔÚcÖУ¬²ÎÊýĬÈÏÊÇ´«ÖµµÄ£¬¼´ÔÚ²ÎÊýÈëջʱ±»¸´ÖÆÒ»·Ý
¡£ÔÚº¯ÊýÀïÃæÐÞ¸ÄÕâЩ²ÎÊý£¬²»»áÓ°ÏìÍâÃæµÄµ÷ÓÃÕß¡£
ÀýÈç
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void get_str(char * p)
{
    p = (char *)malloc(sizeof("abcd"));
    s ......

C treeviewÖнڵãÕ¹¿ªÎÊÌâ

ÎÒ´ÓÊý¾Ý¿â¶¯Ì¬¶ÁÈ¡2¸ö¸¸½Úµã£º
111
       a
            a1
       b
            b1
       c
&n ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ