- Timestamp:
- Nov 27, 2012, 3:51:14 AM (12 years ago)
- 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)
- Location:
- server
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
server/player.cpp
r4da5aa3 r73f75c1 18 18 } 19 19 20 // was meant for the find find function. Currently unused 20 21 bool player::operator == (const player &p) 21 22 { -
server/player.h
r4da5aa3 r73f75c1 8 8 9 9 class player { 10 private:11 sockaddr_in addr;12 13 10 public: 14 11 string name; 12 sockaddr_in addr; 15 13 16 14 player(string name, sockaddr_in addr); -
server/server.cpp
r4da5aa3 r73f75c1 1 1 #include "../common/compiler.h" 2 2 3 #include <sys/types.h>4 3 #include <cstdlib> 5 4 #include <unistd.h> 6 #include <sys/socket.h>7 #include <netinet/in.h>8 5 #include <string> 9 6 #include <netdb.h> … … 12 9 #include <vector> 13 10 #include <algorithm> 11 12 #include <sys/socket.h> 13 #include <netinet/in.h> 14 #include <arpa/inet.h> 14 15 15 16 #include <mysql/mysql.h> … … 41 42 using namespace std; 42 43 44 // this should probably go somewhere in the common folder 43 45 void error(const char *msg) 44 46 { … … 60 62 } 61 63 64 // not sure if we actually need this function 65 // when I made it, I thought we did 66 player *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 62 80 int main(int argc, char *argv[]) 63 81 { 64 82 int sock, length, n; 65 socklen_t fromlen;66 83 struct sockaddr_in server; 67 struct sockaddr_in from; 84 struct sockaddr_in from; // holds the info on the connected client 68 85 NETWORK_MSG clientMsg, serverMsg; 69 86 vector<player> vctPlayers; … … 79 96 80 97 if (argc < 2) { 81 fprintf(stderr, "ERROR, no port provided\n");82 exit( 0);98 cerr << "ERROR, no port provided" << endl; 99 exit(1); 83 100 } 84 101 … … 92 109 if ( bind(sock, (struct sockaddr *)&server, length) < 0 ) 93 110 error("binding"); 94 fromlen = sizeof(struct sockaddr_in); 111 95 112 while (true) { 96 113 // if n == 0, means the client disconnected. may want to check this … … 98 115 if (n < 0) 99 116 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 102 124 switch(clientMsg.type) 103 125 { … … 134 156 strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server."); 135 157 } 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 } 136 163 else 137 164 { … … 144 171 case MSG_TYPE_CHAT: 145 172 { 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 } 159 195 } 160 196
Note:
See TracChangeset
for help on using the changeset viewer.