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

📄 server.c

📁 實現一套網路TCP/IP的嵌入式系統測試程式...希望可以在多平台下運行
💻 C
字号:
#include <stdio.h> 
#include <string.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 

#include<pthread.h>
#include<time.h>


#define distance 100

int d1=0;
int d2=0;
int d3=0;
int d4=0;
int d5=0;

int winnerNO;

pthread_t Ma1, Ma2, Ma3, Ma4, Ma5;

pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; 

pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond4 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond5 = PTHREAD_COND_INITIALIZER;

void* Ma1Thread(void* ptr) { 
    pthread_mutex_lock(&mut);
    while (d1 < distance)
    {
          if (d2 >= distance || d3 >= distance || d4 >= distance || d5 >= distance)
          {
              pthread_cond_broadcast(&cond1); 
              break;
          }
          else
          {
              d1 += rand()%4; 
              printf("%d ",d1);
              pthread_cond_broadcast(&cond1); 
              if (d1 >= distance)
              {
                  winnerNO = 1;
                  //printf("The winner is N0.1!");
                  break;
              }
              else
                  pthread_cond_wait(&cond5, &mut);
          }
    } 
    pthread_mutex_unlock(&mut);
    
} 
void* Ma2Thread(void* ptr) { 

    pthread_mutex_lock(&mut);
    while (d2 < distance)
    {
          if (d3 >= distance || d4 >= distance || d5 >= distance || d1 >= distance)
          {
              pthread_cond_broadcast(&cond2); 
              break;
          }
          else
          {
              d2 += rand()%3; 
              printf("%d ",d2);
              pthread_cond_broadcast(&cond2); 
              if (d2 >= distance)
              {
                  winnerNO = 2;   
                  //printf("The winner is N0.2!");
                  break;
              }
              else
                  pthread_cond_wait(&cond1, &mut);
          }
    } 
    pthread_mutex_unlock(&mut); 
} 

void* Ma3Thread(void* ptr) { 

    pthread_mutex_lock(&mut);
    while (d3 < distance)
    {
          if (d4 >= distance || d5 >= distance || d1 >= distance || d2 >= distance)
          {
              pthread_cond_broadcast(&cond3); 
              break;
          }
          else
          {
              d3 += rand()%3; 
              printf("%d ",d3);
              pthread_cond_broadcast(&cond3); 
              if (d3 >= distance)
              {
                  winnerNO = 3;
                  //printf("The winner is N0.3!");
                  break;
              }
              else
                  pthread_cond_wait(&cond2, &mut);
          }
    } 
    pthread_mutex_unlock(&mut); 
} 
void* Ma4Thread(void* ptr) { 

    pthread_mutex_lock(&mut);
    while (d4 < distance)
    {
          if (d5 >= distance || d1 >= distance || d2 >= distance || d3 >= distance)
          {
              pthread_cond_broadcast(&cond4); 
              break;
          }
          else
          {
              d4 += rand()%2; 
              printf("%d ",d4);
              pthread_cond_broadcast(&cond4); 
              if (d4 >= distance)
              {
                  winnerNO = 4;
                  //printf("The winner is N0.4!");
                  break;
              }
              else
                  pthread_cond_wait(&cond3, &mut);
          }
    } 
    pthread_mutex_unlock(&mut); 
} 
void* Ma5Thread(void* ptr) { 

    pthread_mutex_lock(&mut);
    while (d5 < distance)
    {
          if (d1 >= distance || d2 >= distance || d3 >= distance || d4 >= distance)
          {
              pthread_cond_broadcast(&cond5); 
              break;
          }
          else
          {
              d5 += rand()%2; 
              printf("%d\n",d5);
              pthread_cond_broadcast(&cond5); 
              if (d5 >= distance)
              {
                  winnerNO = 5;
                  //printf("The winner is N0.5!");
                  break;
              }
              else
                  pthread_cond_wait(&cond4, &mut);
          }
    } 
    pthread_mutex_unlock(&mut); 
} 



float run(int ante, int guessNO)
{
    
    srand(time(0));
    float rate[5] = {1.05, 1.22, 1.39, 1.77, 8};
    float anteALL = 0;
   
    
    //printf("-------------------------------------------\n");
    //printf("| **   **    ***    ******     ***  ***** |\n");
    //printf("| **   **   ** **   **  **   ***    **    |\n");
    //printf("| *******  **   **  ******   ***    ***** |\n");
    //printf("| *******  **   **  ** **      ***  ***** |\n");
    //printf("| **   **   ** **   **  **     ***  **    |\n");
    //printf("| **   **    ***    **   **  ***    ***** |\n");
    //printf("-------------------------------------------\n");
    
    //printf("Welcome!\n");
    //printf("You still have %f.\nWould you wnat to play? (y/n)\n", anteALL);
    //scanf("%c", &ans);
    
    //printf("Horse NO.1 - 1:%f\n", rate[0]);
    //printf("Horse NO.2 - 1:%f\n", rate[1]);
    //printf("Horse NO.3 - 1:%f\n", rate[2]);
    //printf("Horse NO.4 - 1:%f\n", rate[3]);
    //printf("Horse NO.5 - 1:%f\n", rate[4]);
    //printf("Choose a horse.\n");
    //scanf("%d", &guessNO);
    //printf("How much money do you want to bet? ($1-$%f)\n", anteALL);
    //scanf("%f", &ante);
          
          
    

    pthread_create(&Ma1, NULL, Ma1Thread, NULL); 
    pthread_create(&Ma2, NULL, Ma2Thread, NULL); 
    pthread_create(&Ma3, NULL, Ma3Thread, NULL);
    pthread_create(&Ma4, NULL, Ma4Thread, NULL);
    pthread_create(&Ma5, NULL, Ma5Thread, NULL);
    pthread_join(Ma1, NULL); 
    pthread_join(Ma2, NULL); 
    pthread_join(Ma3, NULL);
    pthread_join(Ma4, NULL);
    pthread_join(Ma5, NULL);
          
    if (winnerNO == guessNO)
    {
        anteALL = ante*(*(rate+winnerNO-1));
        return anteALL;
    }     
    else
        return -1;
    
}

 
int main(){  
  struct sockaddr_in addr_svr; 
  struct sockaddr_in addr_cli; 
  char ans[2]; 
  
  int sockfd; 
  int connfd; 
 
  int Ante;
  int Choice;
  
  char QueryBuf1[]="How much money you wanna bet?\n";
  char QueryBuf2[]="Which horses? (1-5)\nHorse NO.1 - 1:1.05\nHorse NO.2 - 1:1.22\nHorse NO.3 - 1:1.35\nHorse NO.4 - 1:1.59\nHorse NO.5 - 1:8\n";
  char AnteBuf[150];
  char ChoiceBuf[150];
  char Result[150];
  
  float r;
  int sLen = sizeof(addr_cli); 
  
  // Set Server Address 
  memset(&addr_svr, 0, sizeof(addr_svr));   
  addr_svr.sin_family = AF_INET;  
  addr_svr.sin_port = htons(80);      
  addr_svr.sin_addr.s_addr = inet_addr("192.168.0.204");
  
  // Create Socket 
  sockfd = socket(AF_INET, SOCK_STREAM, 0);  
  
  // Bind Socket 
  if(bind(sockfd, (struct sockaddr *)&addr_svr, sizeof(addr_svr)) == -1){
    printf("Error: bind()\n"); 
    exit(1); 
  } 
  
  // Make Listening Socket 
  if(listen(sockfd, 10) == -1){   
    printf("Error: listen()\n"); 
    exit(1);  
  }  
  
  for(;;){  // Get Connected Socket 
    connfd = accept(sockfd, (struct sockaddr *)&addr_cli, &sLen); 
  
    if(connfd == -1){    
      printf("Error: accept()\n"); 
      exit(1);   
    }
    while(1)
    {
      if(read(connfd, ans, 2) == -1){ 
        printf("Error: read()\n"); 
        exit(1); 
      }
      
      if (!strcmp(ans,"y"))
      {

        if(write(connfd, QueryBuf1, strlen(QueryBuf1)+1) == -1){    
          printf("Error: write()\n");  
          exit(1);   
        }     
        // Read Data From Client 
        if(read(connfd, AnteBuf, 20) == -1){ 
          printf("Error: read()\n"); 
          exit(1); 
        } 
        Ante = atoi(AnteBuf);
  
        // Write Data Back to Client 
        if(write(connfd, QueryBuf2, strlen(QueryBuf2)+1) == -1){    
          printf( "Error: write()\n");  
          exit(1);   
        }
        if(read(connfd, ChoiceBuf, 20) == -1){ 
          printf("Error: read()\n"); 
          exit(1); 
        }     
        Choice = atoi(ChoiceBuf);
  
        r = run(Ante,Choice);
        
        if (r == -1)
          sprintf(Result, "The winner is NO.%d\nYou lose the game!\n", winnerNO);  
        else
          sprintf(Result, "The winner is NO.%d\nYou win the game!\nYou win $%f.\n", winnerNO, r);
        
        if(write(connfd, Result, strlen(Result)+1) == -1){    
          printf( "Error: write()\n");  
          exit(1);   
        }
                   
     }
  }  
} 

  return 0; 
} 

⌨️ 快捷键说明

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