예제 #1
0
int main(int argc, char *argv[]) {
	int serv_sock;
	int clnt_sock;

	char buf[1024];

	struct sockaddr_in serv_addr;
	struct sockaddr_in clnt_addr;
	socklen_t clnt_addr_size;

	char status[] = "HTTP/1.0 200 OK/r/n";
	char header[] = "Server:A Simple Web Server\r\nContent-type: text/html\r\n\r\n";
	char body[] = "<html><head><title>A Simple Web Server</title></head><body><h2>Welcome!></h2><p>This is Beginner!</p></body></html>";

	serv_sock = socket(PF_INET, SOCK_STREAM,0);
	if (-1 == serv_sock) {
		errorHandling("socket() error");
	}
	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET; 
	serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	serv_addr.sin_port = htons(PORT);	
	
	if (-1 == bind(serv_sock,(struct sockaddr*)&serv_addr, sizeof(serv_addr))) {
		errorHandling("bind() error");	
	}

	if (-1 == listen(serv_sock, 5)) {
		errorHandling("listen() error");
	}
	//clnt_addr_size = sizeof(clnt_addr);
	//clnt_sock = accept(serv_sock, (struct sockaddr *) &clnt_addr, &clnt_addr_size);
//	if (-1 == clnt_sock) {
//		errorHandling("accept() error");
//	}
   
	   
    while(1){
        // 接受客户端的请求
        clnt_addr_size = sizeof(clnt_addr);
        clnt_sock = accept(serv_sock, (struct sockaddr *) &clnt_addr, &clnt_addr_size);
        if(-1 == clnt_sock){
            errorHandling("accept() error");
        }

        // 处理客户端请求
        requestHandling((void *) &clnt_sock);
    }

    // 关闭套接字
    close(serv_sock);

	
//	read(clnt_sock, buf, sizeof(buf)-1);
//	printf("%s",buf);
	

//	write(clnt_sock, status, sizeof(status));
//	write(clnt_sock, header, sizeof(header));
//	write(clnt_sock, body, sizeof(body));	
	
//	close(clnt_sock);
//	close(serv_sock);

} 
예제 #2
0
int main(int argc, char *argv[]){

  //保存创建的服务器端套接字
  int serv_sock;
  //保存接受请求的客户端套接字
  int clnt_sock;
  //缓冲区
  char buf[1024];

  //保存服务器套接字地址信息
  struct sockaddr_in serv_addr;
  //保存客户端套接字地址信息
  struct sockaddr_in clnt_addr;
  //套接字地址变量的大小
  socklen_t clnt_addr_size;

  //发送给客户端的固定内容
  char status[] = "HTTP/1.0 200 OK\r\n";
  char header[] = "Server: A Simple Web Server\r\nContent-Type: text/html\r\n\r\n";
  char body[] = "<html><head><title>A Simple Web Server</title><head><body><h1>This is my first Web server</h1></body></html>";

  //创建一个服务器套接字
  serv_sock = socket(PF_INET, SOCK_STREAM, 0);
  if(-1 == serv_sock){
    errorHandling("Socket error()");
  }

  //配置套接字IP和端口信息
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(PORT);

  //绑定服务器套接字
  if (-1 == bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) {
    errorHandling("bind() error");
  }

  //监听服务器套接字
  if(-1 == listen(serv_sock, 5)){
    errorHandling("listen() error");
  }

  while (1) {
    //接受客户端的请求
    clnt_addr_size = sizeof(clnt_addr);
    clnt_sock = accept(serv_sock,(struct sockaddr *) &clnt_addr, &clnt_addr_size);
    if (-1 ==clnt_sock) {
      errorHandling("accept() error");
    }

    requestHandling((void *) &clnt_sock);
  }



  //读取客户端请求
  // read(clnt_sock, buf, sizeof(buf) -1);
  // printf("%s\n", buf);
  //
  // //向客户端套接字发送信息
  // write(clnt_sock, status, sizeof(status));
  // write(clnt_sock, header, sizeof(header));
  // write(clnt_sock, body, sizeof(body));
  //
  // //关闭套接字
  // close(clnt_sock);

  close(serv_sock);

  return 0;

}