Web Server(VxWorks版)
作者:互联网
/* includes */ #include "vxWorks.h" #include "sockLib.h" #include "inetLib.h" #include "taskLib.h" #include "stdioLib.h" #include "strLib.h" #include "ioLib.h" #include "fioLib.h" #include "string.h" /* define */ #define BUFFERSIZE 4096 #define SERVER_PORT_NUM 80 /* server's port number for bind*/ #define SERVER_WORK_PRIORITY 100 /* priority of server's work task */ #define SERVER_STACK_SIZE 10000 /* stack size of server's work task */ #define SERVER_MAX_CONNECTIONS 4 /* max clients connect at a time */ #define REPLY_MSG_SIZE 500 /* function declarations */ VOID tcpServerWorkTask(int sFd, char *address, u_short port); void sendToClient(int ssock) ; STATUS tcpServer (void) { struct sockaddr_in serverAddr; /* server^s socket address */ struct sockaddr_in clientAddr; /* client^s socket address */ int sockAddrSize; int sFd; /* socket file descriptor */ int newFd; /* socket descriptor from accept */ int ix = 0; /* counter for work task names */ char workName[16]; /* name of work task */ /* set up the local address */ /* sockAddrSize = sizeof(struct scckaddr_in); */ sockAddrSize = 16; bzero ((char *) & serverAddr, sockAddrSize);/* memset (&serveraddr, 0, sockAddrSize);*/ serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons (SERVER_PORT_NUM); serverAddr.sin_addr.s_addr = htons (INADDR_ANY); /* serverAddr.sin_port = SERVER_PORT_NU); serverAddr.sin_addr.s_addr = "192.168.2.201";*/ /* creat a TCP-based socket */ if ((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR) { perror("socket"); return (ERROR); } /* bind socket to local address*/ if (bind (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR) { perror("bind"); return (ERROR); } /* create queue for client connetion requests */ if (listen (sFd, SERVER_MAX_CONNECTIONS) == ERROR) { perror ("listen"); return (ERROR); } /* accept new connect requests and spawn tasks to process them */ FOREVER { if ( (newFd = accept (sFd, (struct sockaddr *) &clientAddr, &sockAddrSize)) == ERROR) { perror ("accept"); close (sFd); return (ERROR); } sprintf (workName, "tTcpWorked", ix++); 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) { /* if taskSpawn fails, close fd and return to top of loop */ perror ("taskSpawn"); close (newFd); } }/*end forever*/ } VOID tcpServerWorkTask ( int sFd, /* server^s socket fd */ char *address, /*client^s socket address*/ u_short port /*client^s socket port*/ ) { /* struct request clientRequest;*/ char buf[BUFFERSIZE]; int nRead = 0; /* static char replyMsg[] = ""; */ /* sendToClient(sFd); */ 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"; 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"; 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"; 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"; 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"; static char error[] = "<html><head><title>error</title></head><body><p>error</p></body></html>\r\n"; int count; char *start; char *end; char temp[10] = "xx"; /* store the xx.html */ /* int i = 0; */ int j; char *bptr = buf; int bufLen = BUFFERSIZE; /* recv client request, display message */ /* while ((nRead = recv (sFd, bptr, bufLen, 0)) > 0) { bptr += nRead; bufLen -= nRead; printf ("MESSAGE FROM CLIENT (Internet Address:%s, port:%d);\n%s\n", address, port, buf); } if (nRead == ERROR) { perror ("recv"); } */ /* to do what you want */ /* MESSAGE TO CLIENT*/ count = recv(sFd, buf, BUFFERSIZE, 0); buf[count] = '\0'; printf ("MESSAGE FROM CLIENT (Internet Address:%s, port:%d);\n%s\n", address, port, buf); start = strchr(buf, '/'); end = strchr(buf, '.'); for (j=0;start<(end-1); start++, j++) { temp[j] = *(start + 1); } temp[j] = '\0'; printf("%s", temp); printf("\nwait to send......\n"); send(sFd, header, sizeof(header), 0); /* send(sFd, index, sizeof(index), 0); */ switch (temp[0]) { case 'i': send(sFd, index, sizeof(index), 0); break; case 'h': printf("%s", history); send(sFd, history, sizeof(history), 0); break; case 'r': printf("%s", realtime); send(sFd, realtime, sizeof(realtime), 0); break; case 'f': printf("%s", faraway); send(sFd, faraway, sizeof(faraway), 0); break; default: printf("%s", error); send(sFd, error, sizeof(error), 0); break; } printf(header); close (sFd); /*send (sFd, replyMsg, sizeof(replyMsg), 0); */ };
标签:Web,socket,VxWorks,int,Server,char,ERROR,sFd,port 来源: https://www.cnblogs.com/zhoujiayi/p/12541651.html