Changeset e084950 in network-game for server


Ignore:
Timestamp:
Nov 23, 2012, 8:52:59 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
6c92572
Parents:
d87708d
Message:

Converted the server to use sendMessage and receiveMessage and it now asks the user to guess a number between 1 and 1000

Location:
server
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • server/makefile

    rd87708d re084950  
    1 server : server.cpp
     1server : ../common/message.cpp server.cpp
    22        g++ -o $@ $? -lssl -lmysqlclient
  • server/server.cpp

    rd87708d re084950  
     1#include "../common/compiler.h"
     2
    13#include <sys/types.h>
    24#include <stdlib.h>
     
    1416#include <openssl/ssl.h>
    1517#include <openssl/err.h>
     18#include "../common/message.h"
    1619
    1720using namespace std;
     
    2932   struct sockaddr_in server;
    3033   struct sockaddr_in from;
    31    char buf[1024];
     34   NETWORK_MSG clientMsg, serverMsg;
     35
     36   srand(time(NULL));
     37   int num = (rand() % 1000) + 1;
     38
     39   cout << "num: " << num << endl;
    3240
    3341   SSL_load_error_strings();
     
    4856   server.sin_port=htons(atoi(argv[1]));
    4957   if (bind(sock,(struct sockaddr *)&server,length)<0)
    50        error("binding");
     58      error("binding");
    5159   fromlen = sizeof(struct sockaddr_in);
    5260   while (1) {
    53        n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);
    54        if (n < 0) error("recvfrom");
    55        write(1,"Received a datagram: ",21);
    56        write(1,buf,n);
    57        n = sendto(sock,"Got your message\n",17,
    58                   0,(struct sockaddr *)&from,fromlen);
    59        if (n  < 0) error("sendto");
     61      n = receiveMessage(&clientMsg, sock, &from);
     62      if (n < 0)
     63         error("recieveMessage");
     64      cout << "msg: " << clientMsg.buffer << endl;
     65
     66      if (strcmp(clientMsg.buffer, "Hello"))
     67      {
     68         strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
     69      }else {
     70         int guess = atoi(clientMsg.buffer);
     71
     72         if (guess < 1 || guess > 1000) {
     73            strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
     74         }else if(guess > num)
     75            strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
     76         else if(guess < num)
     77            strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
     78         else if(guess == num) {
     79            strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
     80            num = (rand() % 1000) + 1;
     81         }     
     82      }
     83
     84      cout << "msg: " << serverMsg.buffer << endl;
     85
     86      n = sendMessage(&serverMsg, sock, &from);
     87      if (n  < 0)
     88         error("sendMessage");
    6089   }
    6190   return 0;
Note: See TracChangeset for help on using the changeset viewer.