资源简介
多线程,并发
代码片段和文件信息
#include “http.h“
const int http_init(){
pthread_t http_s;
non_keeping = (struct no_keep *)malloc(sizeof(struct no_keep));
keeping = (struct keep *)malloc(sizeof(struct keep));
if(non_keeping == NULL || keeping ==NULL){
return 0;
}
non_keeping->in = 0;
non_keeping->out = 0;
keeping->in = 0;
keeping->out = 0;
pthread_create(&http_s NULL (void *)http_serve NULL);
return 1;
}
void http_start(){
int conn_fd;
char buf[1000];
int len;
int server_sockfd = socket(AF_INET SOCK_STREAM 0);
//int server_sockfd = socket(AF_INET SOCK_STREAM IPPROTO_TCP);
struct sockaddr_in server_sockaddr;
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(HTTP_PORT);
server_sockaddr.sin_addr.s_addr = INADDR_ANY;
if(bind(server_sockfd(struct sockaddr *)&server_sockaddrsizeof(server_sockaddr))==-1){
perror(“serve_init:bind error-->“);
return;
}
if(listen(server_sockfd120) == -1){
perror(“serve_init:listen error-->“);
return;
}
printf(“The service has been built at port(%d)\n“ HTTP_PORT);
//struct sockaddr_in client_addr;
socklen_t length = sizeof(struct sockaddr_in);
if(!http_init()){
perror(“http_init:error-->“);
return;
}
while(1){
if((conn_fd = accept(server_sockfd (struct sockaddr *)&non_keeping->sock[non_keeping->in] &length)) == -1){
perror(“http_start:accept error.“);
continue;
}
non_keeping->sockfd[non_keeping->in] = conn_fd;
non_keeping->in = (++non_keeping->in)%QUE_SIZE;
}
}
void http_stop(){
//kill threads
//free memory
free(non_keeping);
free(keeping);
}
void *http_serve(){
char *buf;
long int len;
int nk;
buf = (char *)malloc(sizeof(char)*BUF_SIZE);
if(buf == NULL){
perror(“http_serve:run out of memeroy-->“);
//exit(0);
}
while(1){
if(non_keeping->in == non_keeping->out){
continue;
}
nk = http_recv();
if(nk == 0){
continue;
}
if(!http_send()){
http_send();
}
if(nk == 2){
//change_que();
}
close(non_keeping->sockfd[non_keeping->out]);
non_keeping->out = (++non_keeping->out)%QUE_SIZE;
}
}
int http_recv(){
int len = 0;
int nk = 1;
char buf[BUF_SIZE];
len = recv(non_keeping->sockfd[non_keeping->out] &buf BUF_SIZE 0);
if(len <= 0){
return 0;
}
get_message(buf);
if(len == BUF_SIZE){
//next read
}
return nk;
}
int get_message(char *buf){
int i = 0;
int j = 0;
int data_flag = 0;
int nk = 1;
char temp[1024];
for(i=0; i if(buf[i] == ‘ ‘){
(non_keeping->message[non_keeping->out]).method[j] = ‘\0‘;
i++;
break;
}
(non_keeping->message[non_keeping->out]).method[j++] = buf[i];
}
for(j=0; i if(buf[i] == ‘ ‘){
temp[j] = ‘\0‘;
(non_keeping->message[non_keeping->out]).url = (char *)malloc(sizeof(char)*j);
strcpy((non_keeping->message[non_keeping->out]).url temp);
i++;
break;
}
temp[j++] = buf[i
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5179 2017-04-04 19:20 serve\http.c
文件 1104 2017-04-04 17:43 serve\http.h
文件 5684 2017-04-04 15:24 serve\index.html
文件 95 2017-04-04 16:09 serve\main.c
文件 155 2017-04-02 00:51 serve\Makefile
文件 261 2017-04-01 18:37 serve\serve.h
文件 1117 2017-04-04 15:24 serve\st
文件 2781 2017-04-04 15:24 serve\st
文件 3662 2017-04-04 15:24 serve\st
文件 72362 2017-04-04 15:24 serve\st
文件 174 2017-04-02 00:51 serve\test.c
文件 268 2017-04-04 16:33 serve\test.html
文件 21176 2017-04-04 19:21 serve\web
..AD... 0 2016-11-13 21:37 serve\st
..AD... 0 2017-04-04 19:30 serve
----------- --------- ---------- ----- ----
114018 15
- 上一篇:vs2019离线版
- 下一篇:自己实现的ping源码
评论
共有 条评论