📄 webserver.c
字号:
//此程序配合上位机y.c、IE浏览器、UDP测试程序
#include <network.h>
#include <pkgconf/system.h>
#include <pkgconf/net.h>
#include <cyg/infra/testcase.h>
#ifdef CYGBLD_DEVS_ETH_DEVICE_H // Get the device config if it exists
#include CYGBLD_DEVS_ETH_DEVICE_H // May provide CYGTST_DEVS_ETH_TEST_NET_REALTIME
#endif
#ifdef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS // do we use the rt test?
# ifdef CYGTST_DEVS_ETH_TEST_NET_REALTIME // Get the test ancilla if it exists
# include CYGTST_DEVS_ETH_TEST_NET_REALTIME
# endif
#endif
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
static char stack[STACK_SIZE],stack1[STACK_SIZE],stack2[STACK_SIZE],stack3[STACK_SIZE],stack4[STACK_SIZE];
static cyg_thread thread_data,thread_data1,thread_data2,thread_data3,thread_data4;
static cyg_handle_t thread_handle,thread_handle1,thread_handle2,thread_handle3,thread_handle4;
#define MTCP 6
static char mstack[MTCP][STACK_SIZE];
static cyg_thread mthread_data[MTCP];
static cyg_handle_t mthread_handle[MTCP];
static flag[MTCP];
static sock[MTCP];
unsigned char httpweb[]={
"HTTP/1.0 200 OK\r\n"
"Date: Mon, 24 Nov 2003 01:24:17 GMT\r\n"
"Server: microHttp/1.0 Zlgmcu Corporation\r\n"
"Accept-Ranges: bytes\r\n"
//"Content-Length: 116\r\n"//"Connection: Keep-Alive\r\n"
"Connection: Keep-Close\r\n"
"Content-Type: text/html\r\n"
"\r\n"
};
unsigned char web[]={
"<HTML>\r\n"
"<HEAD>\r\n"
"<TITLE>ARM_NET演示网页(周立功单片机)</TITLE>\r\n"
"<BODY aLink=green background=/100.bmp bgColor=#f1f1dd link=red\r\n"
"vLink=#321afd>\r\n"
"<H1>HELLO WELCOME TO EasyArm WEBSERVER</H1>\r\n"
"<UL>\r\n"
"<LI> <A HREF=\"http://www.zlgmcu.com/\">周立功单片机网站 </A>\r\n"
"<LI> <A HREF=\"http://www.zlg.cn/\">周立功单片机内部BBS </A>\r\n"
"<LI> <A HREF=\"http://www.zlgmcu.cn/\">周立功单片机 </A>\r\n"
"</UL>\r\n"
"</BODY>\r\n"
"</HTML>\r\n"
};
unsigned char httpgif[]={
"HTTP/1.0 200 OK\r\n"
"Date: Mon, 24 Nov 2003 01:24:17 GMT\r\n"
"Server: microHttp/1.0 Zlgmcu Corporation\r\n"
"Accept-Ranges: bytes\r\n"
//"Content-Length: 116\r\n"//"Connection: Keep-Alive\r\n"
"Connection: Keep-Close\r\n"
"Content-Type: image/bmp\r\n"
"\r\n"
};
unsigned char bmp[442]={
0x42,0x4d,0xb6,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x76,0x00,0x00,0x00,0x28,0x00,
0x00,0x00,0x1a,0x00,0x00,0x00,0x14,0x00,0x00,0x00,0x01,0x00,0x04,0x00,0x00,0x00,
0x00,0x00,0x40,0x01,0x00,0x00,0xc4,0x0e,0x00,0x00,0xc4,0x0e,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x80,
0x00,0x00,0x00,0x80,0x80,0x00,0x80,0x00,0x00,0x00,0x80,0x00,0x80,0x00,0x80,0x80,
0x00,0x00,0xc0,0xc0,0xc0,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0xff,0x00,0x00,0xff,
0x00,0x00,0x00,0xff,0xff,0x00,0xff,0x00,0x00,0x00,0xff,0x00,0xff,0x00,0xff,0xff,
0x00,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf6,0x66,0xff,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x6f,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0x66,0x66,0x6f,0x6f,0xff,0x66,0x66,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xf6,0xff,0xff,0x6f,0xf6,0xff,0xf6,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0x6f,0xff,0x6f,0xf6,0xff,0xf6,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xf6,0xff,0xf6,0xf6,0xff,0xff,
0x6f,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0x6f,0xf6,0xff,0x6f,0xff,
0x6f,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xf6,0x66,0x66,0xf6,0xff,0xf6,0x66,
0x6f,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x6f,0xff,0xff,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x6f,0xff,0xff,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x6f,0xff,0xff,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x00,0x00,0x00
};
void
pexit(char *s)
{
CYG_TEST_FAIL_FINISH(s);
}
void
udp_test(struct bootp *bp)
{
//struct protoent *p;
//struct timeval tv;
struct sockaddr_in host,client;
int s,len,c_len;
unsigned char buf[100];
unsigned char msga[] = {"YY:www.armecos.com"};
unsigned char msge[] = {"Error!"};
unsigned char num = 5;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0) {
pexit("socket");
return;
}
// Set up host address
host.sin_family = AF_INET;
host.sin_len = sizeof(host);
//host.sin_addr = bp->bp_yiaddr;
host.sin_addr.s_addr = INADDR_ANY;
host.sin_port = ntohs(1025);
if(bind(s, (struct sockaddr *) &host, sizeof(host)) < 0) {
pexit("bind /source/ error");
}
while(true){
c_len = sizeof(client);
len = recvfrom(s, buf, sizeof(buf),0,(struct sockaddr *)&client,&c_len);
if(len > 0){
if(buf[0] == 'A')
num = 5;
else
len = sendto(s,buf,sizeof(buf),0,(struct sockaddr *)&client,sizeof(client));
}
while(num != 0){
len = sendto(s,msga,sizeof(msga),0,(struct sockaddr *)&client,sizeof(client));
if(len != sizeof(msga))
len = sendto(s,msge,sizeof(msge),0,(struct sockaddr *)&client,sizeof(client));
//cyg_thread_delay(10);
num--;
}
}
}
void
webserver_test(struct bootp *bp)
{
//struct protoent *p;
//struct timeval tv;
struct sockaddr_in host,client;
int s,sa,e_source,len;
unsigned char buf[400];
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
pexit("socket");
return;
}
// Set up host address
host.sin_family = AF_INET;
host.sin_len = sizeof(host);
host.sin_addr.s_addr = INADDR_ANY;
host.sin_port = ntohs(80);
if(bind(s, (struct sockaddr *) &host, sizeof(host)) < 0) {
pexit("bind /source/ error");
}
listen(s, SOMAXCONN);
while(true){
memset(buf, 0, sizeof(buf));
if ((sa = accept(s, (struct sockaddr *)&client, &len)) < 0) {
printf("Accept ERROR!\n");
continue;
}
printf("SERVER : HTTP request arrived from %s:%d\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
len = read(sa, buf, sizeof(buf));
if(buf[5] == ' '){
len = write(sa, httpweb, sizeof(httpweb)-1);
len = write(sa, web, sizeof(web));
}
else if(buf[5] == '1'){
len = write(sa, httpgif, sizeof(httpgif)-1);
len = write(sa, bmp,sizeof(bmp));
}
close(sa);
}
}
void
rate_test(struct bootp *bp)
{
//struct protoent *p;
//struct timeval tv;
struct sockaddr_in host,client;
int s,sa,e_source,len;
unsigned char buf[400];
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
pexit("socket");
return;
}
// Set up host address
host.sin_family = AF_INET;
host.sin_len = sizeof(host);
host.sin_addr.s_addr = INADDR_ANY;
host.sin_port = ntohs(2280);
if(bind(s, (struct sockaddr *) &host, sizeof(host)) < 0) {
pexit("bind /source/ error");
}
listen(s, SOMAXCONN);
while(true){
memset(buf, 0, sizeof(buf));
printf("Wait...\n");
if ((sa = accept(s, (struct sockaddr *)&client, &len)) < 0) {
printf("accept error\n");
continue;
}
do{
len = read(sa, buf, sizeof(buf));
}while(len != 0 && len <= sizeof(buf));
close(sa);
}
}
void
mrate_test(cyg_addrword_t i)
{
unsigned char buf[400];
int len;
printf("mrate_test(%d)\n",i);
memset(buf, 0, sizeof(buf));
do{
len = read(sock[i], buf, sizeof(buf));
}while(len != 0 && len <= sizeof(buf));
flag[i] = 0;
close(sock[i]);
cyg_thread_exit();
}
void
rate_multi_test(struct bootp *bp)
{
//struct protoent *p;
struct sockaddr_in host,client;
int s,sa,e_source,len;
int i;
for(i = 0;i < MTCP;i++){
flag[i] = 0;
}
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
pexit("socket");
return;
}
// Set up host address
host.sin_family = AF_INET;
host.sin_len = sizeof(host);
host.sin_addr.s_addr = INADDR_ANY;
host.sin_port = ntohs(2290);
if(bind(s, (struct sockaddr *) &host, sizeof(host)) < 0) {
pexit("bind /source/ error");
}
listen(s, SOMAXCONN);
while(true){
printf("mWait...\n");
if ((sa = accept(s, (struct sockaddr *)&client, &len)) < 0) {
printf("accept error\n");
continue;
}
for(i = 0;i < MTCP;i++){
if(flag[i] == 0){
flag[i] = 1;
sock[i] = sa;
cyg_thread_create(10, // Priority - just a number
mrate_test, // entry
i, // entry parameter
"mrate test", // Name
&mstack[i], // Stack
STACK_SIZE, // Size
&mthread_handle[i], // Handle
&mthread_data[i] // Thread data structure
);
cyg_thread_resume(mthread_handle[i]); // Start it
break;
}
}
}
}
void
net_test(cyg_addrword_t p)
{
diag_printf("Start Networking Test...\n");
init_all_network_interfaces();
#ifdef CYGHWR_NET_DRIVER_ETH0
if (eth0_up) {
cyg_thread_create(10, // Priority - just a number
udp_test, // entry
(cyg_addrword_t)ð0_bootp_data, // entry parameter
"Network udp test", // Name
&stack1[0], // Stack
STACK_SIZE, // Size
&thread_handle1, // Handle
&thread_data1 // Thread data structure
);
cyg_thread_resume(thread_handle1); // Start it
cyg_thread_create(10, // Priority - just a number
webserver_test, // entry
(cyg_addrword_t)ð0_bootp_data, // entry parameter
"Network tcp test", // Name
&stack2[0], // Stack
STACK_SIZE, // Size
&thread_handle2, // Handle
&thread_data2 // Thread data structure
);
cyg_thread_resume(thread_handle2); // Start it
cyg_thread_create(10, // Priority - just a number
rate_test, // entry
(cyg_addrword_t)ð0_bootp_data, // entry parameter
"tcp rate test", // Name
&stack3[0], // Stack
STACK_SIZE, // Size
&thread_handle3, // Handle
&thread_data3 // Thread data structure
);
cyg_thread_resume(thread_handle3); // Start it
cyg_thread_create(10, // Priority - just a number
rate_multi_test, // entry
(cyg_addrword_t)ð0_bootp_data, // entry parameter
"m test", // Name
&stack4[0], // Stack
STACK_SIZE, // Size
&thread_handle4, // Handle
&thread_data4 // Thread data structure
);
cyg_thread_resume(thread_handle4); // Start it
}
#endif
}
void
cyg_start(void)
{
// Create a main thread, so we can run the scheduler and have time 'pass'
cyg_thread_create(10, // Priority - just a number
net_test, // entry
0, // entry parameter
"Network test", // Name
&stack[0], // Stack
STACK_SIZE, // Size
&thread_handle, // Handle
&thread_data // Thread data structure
);
cyg_thread_resume(thread_handle); // Start it
cyg_scheduler_start();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -