int main(int argc, char *argv[]) {

  if (argc != 2) // Test for correct number of arguments
    DieWithUserMessage("Parameter(s)", "<Server Port/Service>");

  char *servPort = argv[1]; // First arg:  local port
  int servSock = SetupTCPServerSocket(servPort);
  if (servSock < 0)
    DieWithUserMessage("SetupTCPServerSocket() failed", "unable to establish");
  for (;;) { // Run forever
    int clntSock = AcceptTCPConnection(servSock);

    // Create separate memory for client argument
    struct ThreadArgs *threadArgs = (struct ThreadArgs *) malloc(
        sizeof(struct ThreadArgs));
    if (threadArgs == NULL)
      DieWithSystemMessage("malloc() failed");
    threadArgs->clntSock = clntSock;

    // Create client thread
    pthread_t threadID;
    int returnValue = pthread_create(&threadID, NULL, ThreadMain, threadArgs);
    if (returnValue != 0)
      DieWithUserMessage("pthread_create() failed", strerror(returnValue));
    printf("with thread %ld\n", (long int) threadID);
  }
  // NOT REACHED
}
int main(int argc, char *argv[]) {
  if (argc != 2) // Test for correct number of arguments
    DieWithUserMessage("Parameter(s)", "<Server Port/Service>");

  int servSock = SetupTCPServerSocket(argv[1]);
  // servSock is now ready to use to accept connections

  for (;;) { // Run forever

    // Wait for a client to connect
    int clntSock = AcceptTCPConnection(servSock);

    // Create an input stream from the socket
    FILE *channel = fdopen(clntSock, "r+");
    if (channel == NULL)
      DieWithSystemMessage("fdopen() failed");

    // Receive messages until connection closes
    int mSize;
    uint8_t inBuf[MAX_WIRE_SIZE];
    VoteInfo v;
    while ((mSize = GetNextMsg(channel, inBuf, MAX_WIRE_SIZE)) > 0) {
      memset(&v, 0, sizeof(v)); // Clear vote information
      printf("Received message (%d bytes)\n", mSize);
      if (Decode(inBuf, mSize, &v)) { // Parse to get VoteInfo
        if (!v.isResponse) { // Ignore non-requests
          v.isResponse = true;
          if (v.candidate >= 0 && v.candidate <= MAX_CANDIDATE) {
            if (!v.isInquiry)
              counts[v.candidate] += 1;
            v.count = counts[v.candidate];
          } // Ignore invalid candidates
        }
        uint8_t outBuf[MAX_WIRE_SIZE];
        mSize = Encode(&v, outBuf, MAX_WIRE_SIZE);
        if (PutMsg(outBuf, mSize, channel) < 0) {
          fputs("Error framing/outputting message\n", stderr);
          break;
        } else {
          printf("Processed %s for candidate %d; current count is %llu.\n",
              (v.isInquiry ? "inquiry" : "vote"), v.candidate, v.count);
        }
        fflush(channel);
      } else {
        fputs("Parse error, closing connection.\n", stderr);
        break;
      }
    }
    puts("Client finished");
    fclose(channel);
  } // Each client
  // NOT REACHED
}
Beispiel #3
0
int main(int argc, char *argv[])
{
  if (argc != 2)
    ErrorWithUserMessage("Parameter(s), <Server Port>");

  char *service = argv[1];

  int servSock = SetupTCPServerSocket(service);
  if (servSock < 0)
    ErrorWithUserMessage("SetupTCPServerSocket() failed: unable to establish");

  unsigned int childProcessCount = 0;
  while (1)
    {
      int clntSock = AcceptTCPConnection(servSock);

      pid_t processID = fork();
      if (processID < 0)
	ErrorWithSystemMessage("fork() failed");
      else if (processID == 0)
        {
	  close(servSock);
	  HandleTCPClient(clntSock);
	  exit(EXIT_SUCCESS);
        }

      printf("with child process: %d\n", processID);
      close(clntSock);
      childProcessCount++;

      //clean up zombies
      while (childProcessCount)
        {
	  processID = waitpid((pid_t) - 1, NULL, WNOHANG);
	  if (processID < 0)
	    ErrorWithSystemMessage("waitpid() failed");
	  else if (processID == 0)
	    break;
	  else
	    childProcessCount--;
        }

    }

}