📄 server.c
字号:
#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/wait.h>#include <sys/socket.h> #define PORT 80 // The port which is communicate with server#define BACKLOG 10#define LENGTH 1024 // Buffer length char httpweb[]={ "HTTP/1.0 200 OK\r\n" "Date: Mon, 24 Nov 2005 10:26:00 GMT\r\n" "Server: microHttp/1.0 Zlgmcu Corporation\r\n" "Accept-Ranges: bytes\r\n" "Connection: Keep-Close\r\n" "Content-Type: text/html\r\n" "\r\n"}; char web[]={ "<HTML>\r\n" "<HEAD>\r\n" "<TITLE>MagicARM2200演示网页(周立功单片机)</TITLE>\r\n" "<BODY aLink=green background=/100.bmp bgColor=#f1f1dd link=red\r\n" "vLink=#321afd>\r\n" "<H1>HELLO WELCOME TO MagicARM2200 WEBSERVER</H1>\r\n" "<UL>\r\n" "<LI> <A HREF=\"http://www.zlgmcu.com/\">周立功单片机网站 </A>\r\n" "</BODY>\r\n" "</HTML>\r\n" };char httpgif[]={ "HTTP/1.0 200 OK\r\n" "Date: Mon, 24 Nov 2005 10:26:00 GMT\r\n" "Server: microHttp/1.0 Zlgmcu Corporation\r\n" "Accept-Ranges: bytes\r\n" "Connection: Keep-Close\r\n" "Content-Type: image/bmp\r\n" "\r\n"}; 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}; int main (){ int sockfd; // Socket file descriptor int nsockfd; // New Socket file descriptor int i,num; int flag = 1; int sin_size; // to store struct size // char sdbuf[LENGTH]; // Send buffer char revbuf[LENGTH]; struct sockaddr_in addr_local; struct sockaddr_in addr_remote; // char sendstr[16]= {"123456789 abcde"}; /* Get the Socket file descriptor */ if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) { printf ("ERROR: Cannot obtain Socket Despcritor\n"); return (0); } else { printf ("OK: Obtain Socket Despcritor sucessfully\n"); } /* Fill the local socket address struct */ addr_local.sin_family = AF_INET; // Protocol Family addr_local.sin_port = htons(PORT); // Port number addr_local.sin_addr.s_addr = INADDR_ANY; // AutoFill local address bzero(&(addr_local.sin_zero), 8); // Flush the rest of struct /* Blind a special Port */ if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 ) { printf ("ERROR: Cannot bind Port %d\n",PORT); return (0); } else { printf("OK: Bind the Port %d sucessfully\n",PORT); } /* Listen remote connect/calling */ if(listen(sockfd,BACKLOG) == -1) { printf ("ERROR: Cannot listen Port %d\n", PORT); return (0); } else { printf ("OK: Listening the Port %d sucessfully\n", PORT); } while(1) { sin_size = sizeof(struct sockaddr_in); /* Wait a connection, and obtain a new socket file despriptor for single connection */ if ((nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size)) == -1) { printf ("ERROR: Obtain new Socket Despcritor error\n"); continue; } else { printf ("OK: Server has got connect from %s\n", inet_ntoa(addr_remote.sin_addr)); } num = recv(nsockfd, revbuf, LENGTH, 0); /* Child process */ if(!vfork()) { printf("OK: Http web is servering.\n"); if(revbuf[5]==' ') { send(nsockfd, httpweb, 169, 0); send(nsockfd, web, 395, 0); } else if(revbuf[5]=='1') { send(nsockfd, httpgif, 169, 0); send(nsockfd, bmp, 442, 0); } } close(nsockfd); while(waitpid(-1, NULL, WNOHANG) > 0); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -