// ---------------------------------------- server_start -------------------------------------------
void server_start() {
  int master_sockfd, current_sockfd;
  socklen_t client_len;
  struct sockaddr_in serv_addr, client_addr;
  char buffer[1000], c;
  int nread, i;

  // Create master socket:
  if ((master_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {       //0 is a fixed number; always use 0
    perror("Server: Cannot create master socket.");
    exit(-1);
  }

  // create socket structure
  bzero((char *) &serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;                          //connects to internet
  serv_addr.sin_addr.s_addr = INADDR_ANY;                  //allows anyone to connect
  serv_addr.sin_port = htons(portno);                      //designates a port number to connect to

  // bind the socket to the local port
  if (bind(master_sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
    perror("Server: Error on binding");
    exit(1);
  }

  // listen
  listen(master_sockfd, 5);               //5 is a fixed number; always use 5

  client_len = sizeof(client_addr);
  printf("Server listening on port %d\n", portno);

  // master loop
  while(TRUE) {
    // block until a client connects
    if ((current_sockfd = accept(master_sockfd, (struct sockaddr *) &client_addr, &client_len)) < 0) {
      perror("Server: Error on accept()");
      exit(1);
    }

    nread = read(current_sockfd, buffer, 1000);                     //reads from client (255 character max)
    if (nread > 0) {
      // Quit command received?
      if (buffer[0] == 'q') {                                       //if string starts with q, then quit and close the server
	//write(current_sockfd, QUITTING, sizeof(QUITTING));
	close(current_sockfd);
	close(master_sockfd);
	printf("Server quitting...\n");
	exit(0);
      }
      // upper-case command received?
      else{
	char *fields[6];
	SeparateIntoFields(buffer, fields, 6);
	runMyStore(fields[1], fields[2], fields[3], fields[4], fields[5]);
	write(current_sockfd, sendToClient, 1000);       //write to client
	close(current_sockfd);
      }
    }
  }
}
示例#2
0
//current_sockfd == file descriptor of pipe from client
//buffer == input from client
//nread == num chars of input
void server_process(int current_sockfd, char *buffer, int nread){

      printf("message from client: %s\n", buffer);
      //Essentially original mystore3 main function,
      //with return statements eliminated
   
      char inputCopy[strlen(buffer)];
      strcpy(inputCopy, buffer+1);
      char *fields[5];
      //int nfields;

      




	

      if (!Process(inputCopy)) {
	if (errmsg[0] != '\0')
	  printf("%s\n",errmsg);
	else
	  printf("|status: ERROR: No command-line arguments, error in arguments, or error in writing to FIFO\n\nVersion: %s\n%s|\n",
		 version,Requests);
      }

      if (!readData()) {
	if (errmsg[0] != '\0')
	  printf("|status: ERROR: %s|\n", errmsg);
	else
	  printf("|status: ERROR: Error reading mystore.dat\n\n%s|\n", Requests);
     }

      strcpy(inputCopy, buffer+1);
      SeparateIntoFields(inputCopy, fields, 5);


      if (command == ADD && !add(fields[2],fields[3])) {
	if (errmsg[0] != '\0')
	  printf("|status: ERROR: %s|\n", errmsg);
	else
	  printf("|status: ERROR: Failure to add new item|\n");
      }

     		 
      if (command == STAT) {
	status();
      }

      
      if (command == DISPLAY && !display(fields[2])) {
	if (errmsg[0] != '\0')
	  printf("|status: ERROR: %s|\n", errmsg);
	else
	  printf("|status: ERROR: Cannot display %s|\n",fields[2]);
      }

      
      if (command == DELETE && !delete(fields[2])) {
	if (errmsg[0] != '\0')
	  printf("|status: ERROR: %s|\n", errmsg);
	else
	  printf("|status: ERROR: Cannot delete %s|\n", fields[2]);
      }

        
      if (command == EDIT && !edit(fields[2])) {
	if (errmsg[0] != '\0')
	  printf("|status: ERROR: %s|\n", errmsg);
	else
	  printf("|status: ERROR: cannot edit %s|\n",fields[2]);
      }

      if (rewrite)
	if (!writeData()) {
	  if (errmsg[0] != '\0')
	    printf("|status: ERROR: %s|\n", errmsg);
	  else
	    printf("|status: ERROR: Could not write the data, file may be destroyed|\n");
	}


}