基于socket建立通信后,偿试建立web服务端,进行演于,是开发板的特有功能之一。这里有linux c建立一个多线程的web服务端。代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <pthread.h>
#define BUF_SIZE 1024
#define SMALL_BUF 100
void * request_handler(void * arg);
void send_data(FILE *fp, char * ct, char * file_name);
char * content_type(char *file);
void send_error(FILE *fp);
void error_handling(char *message);
int main(int argc, char *argv[])
{
int serv_sock, clnt_sock;
struct sockaddr_in serv_adr, clnt_adr;
socklen_t clnt_adr_sz;
char buf[BUF_SIZE];
pthread_t t_id;
if (argc != 2) {
printf("Usage : %s <port> \n", argv[0]);
exit(1);
}
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_adr.sin_port = htons(atoi(argv[1]));
if(bind(serv_sock, (struct sockaddr *) &serv_adr, sizeof(serv_adr)) == -1)
error_handling("bind() error");
if(listen(serv_sock, 5) == -1)
error_handling("listen() error");
while (1)
{
clnt_adr_sz = sizeof(clnt_adr);
clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_adr, &clnt_adr_sz);
printf("Connection Request: %s : %d\n", inet_ntoa(clnt_adr.sin_addr), ntohs(clnt_adr.sin_port));
pthread_create(&t_id, NULL, request_handler, (void*) &clnt_sock);
pthread_detach(t_id);
}
close(serv_sock);
return 0;
}
void * request_handler(void *arg)
{
int clnt_sock = *((int *)arg);
char req_line[SMALL_BUF];
FILE *clnt_read;
FILE *clnt_write;
char method[10];
char ct[15];
char file_name[30];
clnt_read = fdopen(clnt_sock, "r");
clnt_write = fdopen(dup(clnt_sock), "w");
fgets(req_line, SMALL_BUF, clnt_read);
if (strstr(req_line, "HTTP/") == NULL)
{
send_error(clnt_write);
fclose(clnt_read);
fclose(clnt_write);
return NULL;
}
strcpy(method, strtok(req_line, " /"));
strcpy(file_name, strtok(NULL, " /"));
strcpy(ct, content_type(file_name));
if (strcmp(method, "GET") != 0)
{
send_error(clnt_write);
fclose(clnt_read);
fclose(clnt_write);
return NULL;
}
fclose(clnt_read);
send_data(clnt_write, ct, file_name);
return NULL;
}
void send_data(FILE *fp, char *ct, char *file_name)
{
char protocol[] = "HTTP/1.0 200 OK\r\n";
char server[] = "Server: Linux Web Server \r\n";
char cnt_len[] = "Content-length: 2048\r\n";
char cnt_type[SMALL_BUF];
char buf[BUF_SIZE];
FILE *send_file;
sprintf(cnt_type, "Content-type: %s\r\n\r\n", ct);
send_file = fopen(file_name, "r");
if (send_file == NULL)
{
send_error(fp);
return;
}
fputs(protocol, fp);
fputs(server, fp);
fputs(cnt_len, fp);
fputs(cnt_type, fp);
while (fgets(buf, BUF_SIZE, send_file) != NULL)
{
fputs(buf, fp);
fflush(fp);
}
fflush(fp);
fclose(fp);
}
char * content_type(char *file)
{
char extension[SMALL_BUF];
char file_name[SMALL_BUF];
strcpy(file_name, file);
strtok(file_name, ".");
strcpy(extension, strtok(NULL, "."));
if (!strcmp(extension, "html") || !strcmp(extension, "htm"))
return "text/html";
else
return "text/plain";
}
void send_error(FILE *fp)
{
char protocol[] = "HTTP/1.0 400 Bad Request\r\n";
char server[] = "Server: Linux Web Server \r\n";
char cnt_len[] = "Content-length: 2048\r\n";
char cnt_type[] = "Content-type: text/html\r\n\r\n";
char content[] = "发生错误!查看请求文件名和请求方式!";
fputs(protocol, fp);
fputs(server, fp);
fputs(cnt_len, fp);
fputs(cnt_type, fp);
fputs(content, fp);
fflush(fp);
fclose(fp);
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
代码写好后,进行编译,拷贝给开发板:
同时书写一个测试的index.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>我的米尔测试</title>
</head>
<body>
这是基于米尔Y6UL的web测试页面!
</body>
</html>
在开发板上运行:./myserver 8000(创建一个8000端口的web服务)
打开浏览器输入网址:192.168.3.181:8000/index.html效果如下:
致此创建简单的web服务器已完成。