Changeset 73f75c1 in network-game for server


Ignore:
Timestamp:
Nov 27, 2012, 3:51:14 AM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
2318fff
Parents:
4da5aa3
git-author:
dportnoy <dmp1488@…> (11/27/12 03:40:30)
git-committer:
dportnoy <dmp1488@…> (11/27/12 03:51:14)
Message:

Created findPlayerByAddr and added some more checks on the client name and address when receiving a message

Location:
server
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • server/player.cpp

    r4da5aa3 r73f75c1  
    1818}
    1919
     20// was meant for the find find function. Currently unused
    2021bool player::operator == (const player &p)
    2122{
  • server/player.h

    r4da5aa3 r73f75c1  
    88
    99class player {
    10 private:
    11    sockaddr_in addr;
    12 
    1310public:
    1411   string name;
     12   sockaddr_in addr;
    1513
    1614   player(string name, sockaddr_in addr);
  • server/server.cpp

    r4da5aa3 r73f75c1  
    11#include "../common/compiler.h"
    22
    3 #include <sys/types.h>
    43#include <cstdlib>
    54#include <unistd.h>
    6 #include <sys/socket.h>
    7 #include <netinet/in.h>
    85#include <string>
    96#include <netdb.h>
     
    129#include <vector>
    1310#include <algorithm>
     11
     12#include <sys/socket.h>
     13#include <netinet/in.h>
     14#include <arpa/inet.h>
    1415
    1516#include <mysql/mysql.h>
     
    4142using namespace std;
    4243
     44// this should probably go somewhere in the common folder
    4345void error(const char *msg)
    4446{
     
    6062}
    6163
     64// not sure if we actually need this function
     65// when I made it, I thought we did
     66player *findPlayerByAddr(vector<player> &vec, sockaddr_in &addr)
     67{
     68   vector<player>::iterator it;
     69
     70   for (it = vec.begin(); it != vec.end(); it++)
     71   {
     72      if ( it->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
     73           it->addr.sin_port == addr.sin_port )
     74         return &(*it);
     75   }
     76
     77   return NULL;
     78}
     79
    6280int main(int argc, char *argv[])
    6381{
    6482   int sock, length, n;
    65    socklen_t fromlen;
    6683   struct sockaddr_in server;
    67    struct sockaddr_in from;
     84   struct sockaddr_in from; // holds the info on the connected client
    6885   NETWORK_MSG clientMsg, serverMsg;
    6986   vector<player> vctPlayers;
     
    7996
    8097   if (argc < 2) {
    81       fprintf(stderr, "ERROR, no port provided\n");
    82       exit(0);
     98      cerr << "ERROR, no port provided" << endl;
     99      exit(1);
    83100   }
    84101   
     
    92109   if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
    93110      error("binding");
    94    fromlen = sizeof(struct sockaddr_in);
     111
    95112   while (true) {
    96113      // if n == 0, means the client disconnected. may want to check this
     
    98115      if (n < 0)
    99116         error("recieveMessage");
    100       cout << "MSG: type: " << clientMsg.type << " contents: " << clientMsg.buffer << endl;
    101 
     117      cout << "ip address: " << inet_ntoa(from.sin_addr) << endl;
     118      cout << "port: " << from.sin_port << endl;
     119      cout << "MSG: type: " << clientMsg.type << endl;
     120      cout << "MSG contents: " << clientMsg.buffer << endl;
     121
     122      // Check that if an invalid message is sent, the client will corectly
     123      // receive and display the response. Maybe make a special error msg type
    102124      switch(clientMsg.type)
    103125      {
     
    134156               strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
    135157            }
     158            else if( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
     159                     p->addr.sin_port != from.sin_port )
     160            {
     161               strcpy(serverMsg.buffer, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server.");
     162            }
    136163            else
    137164            {
     
    144171         case MSG_TYPE_CHAT:
    145172         {
    146             int guess = atoi(clientMsg.buffer);
    147 
    148             cout << "guess: " << guess << endl;
    149 
    150             if (guess < 1 || guess > 1000) {
    151                strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
    152             }else if(guess > num)
    153                strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
    154             else if(guess < num)
    155                strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
    156             else if(guess == num) {
    157                strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
    158                num = (rand() % 1000) + 1;
     173            player *p = findPlayerByAddr(vctPlayers, from);
     174
     175            if (p == NULL)
     176            {
     177               strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
     178            }
     179            else
     180            {
     181               int guess = atoi(clientMsg.buffer);
     182
     183               cout << "guess: " << guess << endl;
     184
     185               if (guess < 1 || guess > 1000) {
     186                  strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
     187               }else if(guess > num)
     188                  strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
     189               else if(guess < num)
     190                  strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
     191               else if(guess == num) {
     192                  strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
     193                  num = (rand() % 1000) + 1;
     194               }
    159195            }   
    160196
Note: See TracChangeset for help on using the changeset viewer.