Changeset 2488852 in network-game for server/server.cpp
- Timestamp:
- Nov 26, 2012, 5:45:25 PM (12 years ago)
- Branches:
- master
- Children:
- 0cc431d
- Parents:
- 6475138
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
server/server.cpp
r6475138 r2488852 2 2 3 3 #include <sys/types.h> 4 #include < stdlib.h>4 #include <cstdlib> 5 5 #include <unistd.h> 6 6 #include <sys/socket.h> 7 7 #include <netinet/in.h> 8 #include <string .h>8 #include <string> 9 9 #include <netdb.h> 10 #include < stdio.h>10 #include <cstdio> 11 11 #include <iostream> 12 #include <vector> 13 #include <algorithm> 12 14 13 15 #include <mysql/mysql.h> … … 16 18 #include <openssl/ssl.h> 17 19 #include <openssl/err.h> 20 21 #include "player.h" 18 22 #include "../common/message.h" 19 23 /////////// 20 24 using namespace std; 21 25 … … 24 28 perror(msg); 25 29 exit(0); 30 } 31 32 player *findPlayerByName(vector<player> &vec, string name) 33 { 34 vector<player>::iterator it; 35 36 for (it = vec.begin(); it != vec.end(); it++) 37 { 38 if ( it->name.compare(name) == 0 ) 39 return &(*it); 40 } 41 42 return NULL; 26 43 } 27 44 … … 33 50 struct sockaddr_in from; 34 51 NETWORK_MSG clientMsg, serverMsg; 52 vector<player> vctPlayers; 35 53 36 54 srand(time(NULL)); … … 53 71 bzero(&server,length); 54 72 server.sin_family=AF_INET; 73 server.sin_port=htons(atoi(argv[1])); 55 74 server.sin_addr.s_addr=INADDR_ANY; 56 server.sin_port=htons(atoi(argv[1])); 57 if (bind(sock,(struct sockaddr *)&server,length)<0) 75 if ( bind(sock, (struct sockaddr *)&server, length) < 0 ) 58 76 error("binding"); 59 77 fromlen = sizeof(struct sockaddr_in); 60 78 while (true) { 79 // if n == 0, means the client disconnected. may want to check this 61 80 n = receiveMessage(&clientMsg, sock, &from); 62 81 if (n < 0) … … 64 83 cout << "msg: " << clientMsg.buffer << endl; 65 84 85 // Ptoyovol Design 86 // 87 // Client sends a login message 88 // Server replies with client's position in the world and positions of 89 // oall other logged in players 90 // Merver sends player ids along with locations 91 // This means a newly logged in client will need to know which id is 92 // assigned to it 93 // So server needs to send an id message, wait for an ack, and then send 94 // the location messages 95 96 // When a client shuts down, it sends a message to indicate this so the 97 // server can remove it from the list of connected clients 98 // Eventually, there'll need to be a way to detect timeouts from clients 99 // (if they crashed or otherwise failed to send the logout message) 100 66 101 if (strcmp(clientMsg.buffer, "Hello") == 0) 67 102 { 68 strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is."); 103 player *p = findPlayerByName(vctPlayers, "Boberty"); 104 105 if (p == NULL) 106 { 107 vctPlayers.push_back(player("Boberty", from)); 108 strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is."); 109 } 110 else 111 { 112 strcpy(serverMsg.buffer, "Player has already logged in."); 113 } 69 114 }else { 70 115 int guess = atoi(clientMsg.buffer);
Note:
See TracChangeset
for help on using the changeset viewer.