一个简单的嵌入式WEB服务器(vxworks + arm)
1 /* includes */
2
3 #include "vxWorks.h"
4 #include "sockLib.h"
5 #include "inetLib.h"
6 #include "taskLib.h"
7 #include "stdioLib.h"
8 #include "strLib.h"
9 #include "ioLib.h"
10 #include "fioLib.h"
11 #include "string.h"
12
13 /* define */
14 #define BUFFERSIZE 4096
15 #define SERVER_PORT_NUM 80 /* server's port number for bind*/
16 #define SERVER_WORK_PRIORITY 100 /* priority of server's work task */
17 #define SERVER_STACK_SIZE 10000 /* stack size of server's work task */
18 #define SERVER_MAX_CONNECTIONS 4 /* max clients connect at a time */
19 #define REPLY_MSG_SIZE 500
20
21 /* function declarations */
22
23 VOID tcpServerWorkTask(int sFd, char *address, u_short port);
24 void sendToClient(int ssock) ;
25
26
27 STATUS tcpServer (void)
28 {
29 struct sockaddr_in serverAddr; /* server^s socket address */
30 struct sockaddr_in clientAddr; /* client^s socket address */
31 int sockAddrSize;
32 int sFd; /* socket file descriptor */
33
34 int newFd; /* socket descriptor from accept */
35 int ix = 0; /* counter for work task names */
36 char workName[16]; /* name of work task */
37
38 /* set up the local address */
39 /* sockAddrSize = sizeof(struct scckaddr_in); */
40 sockAddrSize = 16;
41 bzero ((char *) & serverAddr, sockAddrSize);/* memset (&serveraddr, 0, sockAddrSize);*/
42 serverAddr.sin_family = AF_INET;
43 serverAddr.sin_port = htons (SERVER_PORT_NUM);
44 serverAddr.sin_addr.s_addr = htons (INADDR_ANY);
45
46 /* serverAddr.sin_port = SERVER_PORT_NU);
47 serverAddr.sin_addr.s_addr = "192.168.2.201";*/
48
49 /* creat a TCP-based socket */
50 if ((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR)
51 {
52 perror("socket");
53 return (ERROR);
54 }
55
56 /* bind socket to local address*/
57 if (bind (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR)
58 {
59 perror("bind");
60 return (ERROR);
61 }
62
63 /* create queue for client connetion requests */
64 if (listen (sFd, SERVER_MAX_CONNECTIONS) == ERROR)
65 {
66 perror ("listen");
67 return (ERROR);
68 }
69
70 /* accept new connect requests and spawn tasks to process them */
71 FOREVER
72 {
73 if ( (newFd = accept (sFd, (struct sockaddr *) &clientAddr, &sockAddrSize)) == ERROR)
74 {
75 perror ("accept");
76 close (sFd);
77 return (ERROR);
78 }
79
80 sprintf (workName, "tTcpWorked", ix++);
81
85 /* if taskSpawn fails, close fd and return to top of loop */
86
87 perror ("taskSpawn");
88 close (newFd);
89 }
90 }/*end forever*/
91 }
92
93 VOID tcpServerWorkTask
94 (
95 int sFd, /* server^s socket fd */
96 char *address, /*client^s socket address*/
97 u_short port /*client^s socket port*/
98 )
99 {
100 /* struct request clientRequest;*/
101 char buf[BUFFERSIZE];
102 int nRead = 0;
103 /* static char replyMsg[] = ""; */
104
105 /* sendToClient(sFd); */
106 static char header[] = "HTTP/1.1 200 OK\r\nConnection: close\r\nDate: Thu, 03 Jul 2003 12:00:15 GMT\r\nContent-Type: text/html\r\n\r\n";
107 static char index[] = "<html><body><table><tr><td><a href='realtime.html'>RealTime</d></tr><tr><td><a href='history.html'>History</td></tr><tr><td><a href='remote.html'>Remote</a></td></tr></table></body></html>\r\n\r\n";
108 static char history[] = "<html><body><form name='form1' method='post' action='index.html'><table><tr><td>SheBei</td><td>DYFZ(V)</td><td>DLFZ(V)</td><td>WDFZ(C)</td><td>YGGNFZ(W)</td></tr><tr><td>##</td><td>220</td><td>10</td><td>45</td><td>1000</td></tr><tr><td>##</td><td>220</td><td>10</td><td>45</td><td>1000</td></tr><tr><td>##</td><td>220</td><td>10</td><td>45</td><td>1000</td></tr></table><input type='submit' name='Submit' value='return'></form></body></html>\r\n\r\n";
109 static char realtime[] = "<html><body><form name='form1' method='post' action='index.html'><table><tr><td>SheBei</td><td>DianYa(V)</td><td>DianLiu(V)</td><td>WenDu(C)</td><td>YouGGN(W)</td></tr><tr><td>##</td><td>220</td><td>10</td><td>45</td><td>1000</td></tr><tr><td>##</td><td>220</td><td>10</td><td>45</td><td>1000</td></tr><tr><td>##</td><td>220</td><td>10</td><td>45</td><td>1000</td></tr></table><input type='submit' name='Submit' value='return'></form></body></html>\r\n\r\n";
110 static char faraway[] = "<html><body><form name='form1' method='post' action='index.html'><table><tr><td><input name='1' type='radio' value='1'></td><td>SheBei1</td></tr><tr><td><input name='2' type='radio' value='2'></td><td>SheBei2</td></tr></table><p><table><tr><td>DYZ:</td><td><input type='text' name='volumn'></td></tr></table><input type='submit' name='Submit' value='submit'></form></body></html>\r\n\r\n";
111 static char error[] = "<html><head><title>error</title></head><body><p>error</p></body></html>\r\n";
112 int count;
113 char *start;
114 char *end;
115 char temp[10] = "xx"; /* store the xx.html */
116 /* int i = 0; */
117 int j;
118
119 char *bptr = buf;
120 int bufLen = BUFFERSIZE;
121 /* recv client request, display message */
122 /*
123 while ((nRead = recv (sFd, bptr, bufLen, 0)) > 0)
124 {
125 bptr += nRead;
126 bufLen -= nRead;
129
130 if (nRead == ERROR)
131 {
132 perror ("recv");
133 }
134 */
135
136 /* to do what you want */
137 /* MESSAGE TO CLIENT*/
138 count = recv(sFd, buf, BUFFERSIZE, 0);
139 buf[count] = '\0';
140 printf ("MESSAGE FROM CLIENT (Internet Address:%s, port:%d);\n%s\n", address, port, buf);
141
142
143 start = strchr(buf, '/');
144 end = strchr(buf, '.');
145
146 for (j=0;start<(end-1); start++, j++)
147 {
148 temp[j] = *(start + 1);
149 }
150
151 temp[j] = '\0';
152 printf("%s", temp);
153
154 printf("\nwait to send......\n");
155
156 send(sFd, header, sizeof(header), 0);
157 /* send(sFd, index, sizeof(index), 0); */
158
159 switch (temp[0])
160 {
161 case 'i':
162 send(sFd, index, sizeof(index), 0);
163 break;
164 case 'h':
165
166 printf("%s", history);
167 send(sFd, history, sizeof(history), 0);
168 break;
169 case 'r':
170
171 printf("%s", realtime);
172 send(sFd, realtime, sizeof(realtime), 0);
173 break;
174 case 'f':
175
176 printf("%s", faraway);
177 send(sFd, faraway, sizeof(faraway), 0);
178 break;
179 default:
180
181 printf("%s", error);
182 send(sFd, error, sizeof(error), 0);
183 break;
184 }
185
186 printf(header);
187
188
189 close (sFd);
190 /*send (sFd, replyMsg, sizeof(replyMsg), 0); */
191
192 };
193
2
3 #include "vxWorks.h"
4 #include "sockLib.h"
5 #include "inetLib.h"
6 #include "taskLib.h"
7 #include "stdioLib.h"
8 #include "strLib.h"
9 #include "ioLib.h"
10 #include "fioLib.h"
11 #include "string.h"
12
13 /* define */
14 #define BUFFERSIZE 4096
15 #define SERVER_PORT_NUM 80 /* server's port number for bind*/
16 #define SERVER_WORK_PRIORITY 100 /* priority of server's work task */
17 #define SERVER_STACK_SIZE 10000 /* stack size of server's work task */
18 #define SERVER_MAX_CONNECTIONS 4 /* max clients connect at a time */
19 #define REPLY_MSG_SIZE 500
20
21 /* function declarations */
22
23 VOID tcpServerWorkTask(int sFd, char *address, u_short port);
24 void sendToClient(int ssock) ;
25
26
27 STATUS tcpServer (void)
28 {
29 struct sockaddr_in serverAddr; /* server^s socket address */
30 struct sockaddr_in clientAddr; /* client^s socket address */
31 int sockAddrSize;
32 int sFd; /* socket file descriptor */
33
34 int newFd; /* socket descriptor from accept */
35 int ix = 0; /* counter for work task names */
36 char workName[16]; /* name of work task */
37
38 /* set up the local address */
39 /* sockAddrSize = sizeof(struct scckaddr_in); */
40 sockAddrSize = 16;
41 bzero ((char *) & serverAddr, sockAddrSize);/* memset (&serveraddr, 0, sockAddrSize);*/
42 serverAddr.sin_family = AF_INET;
43 serverAddr.sin_port = htons (SERVER_PORT_NUM);
44 serverAddr.sin_addr.s_addr = htons (INADDR_ANY);
45
46 /* serverAddr.sin_port = SERVER_PORT_NU);
47 serverAddr.sin_addr.s_addr = "192.168.2.201";*/
48
49 /* creat a TCP-based socket */
50 if ((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR)
51 {
52 perror("socket");
53 return (ERROR);
54 }
55
56 /* bind socket to local address*/
57 if (bind (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR)
58 {
59 perror("bind");
60 return (ERROR);
61 }
62
63 /* create queue for client connetion requests */
64 if (listen (sFd, SERVER_MAX_CONNECTIONS) == ERROR)
65 {
66 perror ("listen");
67 return (ERROR);
68 }
69
70 /* accept new connect requests and spawn tasks to process them */
71 FOREVER
72 {
73 if ( (newFd = accept (sFd, (struct sockaddr *) &clientAddr, &sockAddrSize)) == ERROR)
74 {
75 perror ("accept");
76 close (sFd);
77 return (ERROR);
78 }
79
80 sprintf (workName, "tTcpWorked", ix++);
81
82 if (taskSpawn (workName, SERVER_WORK_PRIORITY, 0, SERVER_STACK_SIZE,
(FUNCPTR) tcpServerWorkTask, newFd,(int) inet_ntoa(clientAddr.sin_addr),
ntohs (clientAddr.sin_port), 0, 0, 0, 0, 0, 0,0) == ERROR)
84 {85 /* if taskSpawn fails, close fd and return to top of loop */
86
87 perror ("taskSpawn");
88 close (newFd);
89 }
90 }/*end forever*/
91 }
92
93 VOID tcpServerWorkTask
94 (
95 int sFd, /* server^s socket fd */
96 char *address, /*client^s socket address*/
97 u_short port /*client^s socket port*/
98 )
99 {
100 /* struct request clientRequest;*/
101 char buf[BUFFERSIZE];
102 int nRead = 0;
103 /* static char replyMsg[] = ""; */
104
105 /* sendToClient(sFd); */
106 static char header[] = "HTTP/1.1 200 OK\r\nConnection: close\r\nDate: Thu, 03 Jul 2003 12:00:15 GMT\r\nContent-Type: text/html\r\n\r\n";
107 static char index[] = "<html><body><table><tr><td><a href='realtime.html'>RealTime</d></tr><tr><td><a href='history.html'>History</td></tr><tr><td><a href='remote.html'>Remote</a></td></tr></table></body></html>\r\n\r\n";
108 static char history[] = "<html><body><form name='form1' method='post' action='index.html'><table><tr><td>SheBei</td><td>DYFZ(V)</td><td>DLFZ(V)</td><td>WDFZ(C)</td><td>YGGNFZ(W)</td></tr><tr><td>##</td><td>220</td><td>10</td><td>45</td><td>1000</td></tr><tr><td>##</td><td>220</td><td>10</td><td>45</td><td>1000</td></tr><tr><td>##</td><td>220</td><td>10</td><td>45</td><td>1000</td></tr></table><input type='submit' name='Submit' value='return'></form></body></html>\r\n\r\n";
109 static char realtime[] = "<html><body><form name='form1' method='post' action='index.html'><table><tr><td>SheBei</td><td>DianYa(V)</td><td>DianLiu(V)</td><td>WenDu(C)</td><td>YouGGN(W)</td></tr><tr><td>##</td><td>220</td><td>10</td><td>45</td><td>1000</td></tr><tr><td>##</td><td>220</td><td>10</td><td>45</td><td>1000</td></tr><tr><td>##</td><td>220</td><td>10</td><td>45</td><td>1000</td></tr></table><input type='submit' name='Submit' value='return'></form></body></html>\r\n\r\n";
110 static char faraway[] = "<html><body><form name='form1' method='post' action='index.html'><table><tr><td><input name='1' type='radio' value='1'></td><td>SheBei1</td></tr><tr><td><input name='2' type='radio' value='2'></td><td>SheBei2</td></tr></table><p><table><tr><td>DYZ:</td><td><input type='text' name='volumn'></td></tr></table><input type='submit' name='Submit' value='submit'></form></body></html>\r\n\r\n";
111 static char error[] = "<html><head><title>error</title></head><body><p>error</p></body></html>\r\n";
112 int count;
113 char *start;
114 char *end;
115 char temp[10] = "xx"; /* store the xx.html */
116 /* int i = 0; */
117 int j;
118
119 char *bptr = buf;
120 int bufLen = BUFFERSIZE;
121 /* recv client request, display message */
122 /*
123 while ((nRead = recv (sFd, bptr, bufLen, 0)) > 0)
124 {
125 bptr += nRead;
126 bufLen -= nRead;
127 printf ("MESSAGE FROM CLIENT (Internet Address:%s, port:%d);\n%s\n",
address, port, buf);
128 }129
130 if (nRead == ERROR)
131 {
132 perror ("recv");
133 }
134 */
135
136 /* to do what you want */
137 /* MESSAGE TO CLIENT*/
138 count = recv(sFd, buf, BUFFERSIZE, 0);
139 buf[count] = '\0';
140 printf ("MESSAGE FROM CLIENT (Internet Address:%s, port:%d);\n%s\n", address, port, buf);
141
142
143 start = strchr(buf, '/');
144 end = strchr(buf, '.');
145
146 for (j=0;start<(end-1); start++, j++)
147 {
148 temp[j] = *(start + 1);
149 }
150
151 temp[j] = '\0';
152 printf("%s", temp);
153
154 printf("\nwait to send......\n");
155
156 send(sFd, header, sizeof(header), 0);
157 /* send(sFd, index, sizeof(index), 0); */
158
159 switch (temp[0])
160 {
161 case 'i':
162 send(sFd, index, sizeof(index), 0);
163 break;
164 case 'h':
165
166 printf("%s", history);
167 send(sFd, history, sizeof(history), 0);
168 break;
169 case 'r':
170
171 printf("%s", realtime);
172 send(sFd, realtime, sizeof(realtime), 0);
173 break;
174 case 'f':
175
176 printf("%s", faraway);
177 send(sFd, faraway, sizeof(faraway), 0);
178 break;
179 default:
180
181 printf("%s", error);
182 send(sFd, error, sizeof(error), 0);
183 break;
184 }
185
186 printf(header);
187
188
189 close (sFd);
190 /*send (sFd, replyMsg, sizeof(replyMsg), 0); */
191
192 };
193