⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myhttp.cpp

📁 在linux下实现web服务!采用多进程并发服务(不是多线程的方式实现)
💻 CPP
字号:
// myhttp.cpp : Defines the entry point for the console application.
//
//the webserver.cpp
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <sys/time.h>
#include <fstream>
#include <string>
#include <signal.h>
#include <sys/wait.h>

#include "Httphead.h"
#include "HttPresponsion.h"
#include "MyUrl.h"
using namespace std;
#define  SERVER_PORT 83
#define BUFFERSIZE  256
#define ERROR_400 "<head></head><body><html><h1>error 400</h1><p>the server con not unstand your request</html></body>"
#define ERROR_404 "<head></head><body><html><h1>error 404</h1><p>the server con not unstand your request</html></body>"
#define HOME_PAGE "<HTML><head>\r\n</head>\r\n<body>\r\n<html>\r\n<h5>wellcome</h5>\r\n<p>now you are seeing page is my homepage</p>\r\n</html>\r\n</body>"
#define TIME_PAGE "<head>\r\n</head>\r\n<body>\r\n<html>\r\n<h1>The courrent time is %s</h1></html></body>"
#define SERVER_NAME "SCNUYUN"
#define K  1024
#define M  1024*K
#define QUEUE_LENGTH  250 
void sig_chld(int signo);
int main(int argc,char *argv[])
{

	int socklisten;
	struct sockaddr_in  server_addr;
	struct sockaddr_in  client_addr;
	char buff[BUFFERSIZE];
	socklisten=socket(AF_INET,SOCK_STREAM,0);
	server_addr.sin_family=AF_INET;
	server_addr.sin_port=htons(SERVER_PORT);
	server_addr.sin_addr.s_addr=INADDR_ANY;
	bzero(&server_addr.sin_zero,8);

	if (bind(socklisten,(struct sockaddr *)&server_addr,sizeof(server_addr))<0)
	{
		cout<<"the addr bind to the socket was failure"<<endl;
		exit(1);
	}

	cout<<"bind the port successful"<<endl;
	if (listen(socklisten,QUEUE_LENGTH)<0)
	{
		cout<<"listen to the socket was failure"<<endl;
		exit(2);
	}
	cout<<"listen to the socket successful"<<endl;
	//通信端口communicate socket
	int comsock=-1;
	pid_t childpid;
	signal(SIGCHLD,sig_chld);
	while (1)
	{
		int addrlen=sizeof(client_addr);
		cout<<"waiting for connect"<<endl;
		comsock=accept(socklisten,(struct sockaddr *)&client_addr,(socklen_t *)&addrlen);
		cout<<"receive a connection"<<endl;  
		if(comsock<0) 
		{
			cout<<"the accept client error and the server stop "<<endl;
			exit(3);
		}
		cout<<"connection was setup finishes"<<endl;
		if(childpid=fork()==0)
		{
			cout<<"create a child process"<<endl;
			//close the listioning socket because the fork function
			close(socklisten);
                        int rec=0;
                        //while(rec<BUFFERSIZE)
			//{
                       int x;
                         x=recv(comsock,buff+rec,BUFFERSIZE-rec,0);
                        // if(x==0||x==-1)
                          //  break;
                         rec+=x;
                       // }
                        CHttphead head(buff,rec);
			 CMyUrl url;
			//结构url的处理
                        url.Setpath(head.geturl());
			string filepath=url.getpath();
			cout<<"filepath"<<filepath<<endl;
          	//http的应答
			CHttPresponsion httpP;
 			ifstream is;
			is.open(filepath.data(),ios::in);
			if(is.fail())
			{
		        ///文件不存在,返回一个默认的页面
				cout<<"the file is't exist"<<endl;
                                string httphead;
                                //  CHttPresponsion httpP;
				int len=strlen(HOME_PAGE);
				httpP.send_head(comsock,200,len,httphead);
				string data=HOME_PAGE;
                                data=httphead+data;
			        //cout<<"HOME_PATE"<<data.data()<<endl;
                                
				const char *buf=data.data();
                                int size=data.size();
                                int sd=0;
                                while(sd<data.size()-2)
                                {
                                 int c=send(comsock,buf+sd,data.size()-sd,0);
                                 if(c==0||c==-1)
                                   break;
                                 sd+=c;
                                }
                                is.close();
			     }
    		          else
			     {
                               //读写该文件,把文件返回
			    
            	                char *filebuff=new char [K*K+1];
				
				int count=0;
				 while (!is.eof())
				 {
					 
					 if (count>=K*K)
					 {
						 //缓冲区溢出
						 break;
					 }
					 filebuff[count]=is.get();
                                         count++;
				  }
				 //发送http应答
                                string fb;
                                httpP.send_head(comsock,200,count--,fb);
				 //发送数据
                                 fb+=filebuff;
                                 const char *fibuf=fb.data();
                                 int sn=0;
                                 while(sn<fb.size())
                                 {
                                 int x=send(comsock,fibuf+sn,fb.size()-sn,0);
                                  if(x==0||x==-1)
                                    break;
                                  sn+=x;
                                 }
				 delete filebuff;
				 is.close();
		              }
			close(comsock);
			exit(0);
		}//end the child process
	close(comsock);
	}//end listen
   close(socklisten);
   
}


void sig_chld(int signo)
{
	pid_t pid;
	int start;
	//等待子进程返回
	while ((pid=waitpid(-1,&start,WNOHANG))>0);
	
	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -