- Timestamp:
- Nov 28, 2012, 3:38:24 PM (12 years ago)
- Branches:
- master
- Children:
- 439f7bc
- Parents:
- 59061f6
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
server/server.cpp
r59061f6 r371ce29 2 2 3 3 #include <cstdlib> 4 #include <cstdio> 4 5 #include <unistd.h> 5 6 #include <string> 6 #include <netdb.h>7 #include <cstdio>8 7 #include <iostream> 9 8 #include <vector> 10 9 #include <algorithm> 11 10 11 #include <fcntl.h> 12 #include <assert.h> 13 12 14 #include <sys/socket.h> 15 #include <netdb.h> 13 16 #include <netinet/in.h> 14 17 #include <arpa/inet.h> … … 77 80 } 78 81 82 void set_nonblock(int sock) 83 { 84 int flags; 85 flags = fcntl(sock, F_GETFL,0); 86 assert(flags != -1); 87 fcntl(sock, F_SETFL, flags | O_NONBLOCK); 88 } 89 79 90 int main(int argc, char *argv[]) 80 91 { … … 86 97 87 98 srand(time(NULL)); 88 int num = (rand() % 1000) + 1; 99 int num = 500; 100 //int num = (rand() % 0) + 1; 89 101 90 102 cout << "num: " << num << endl; … … 99 111 } 100 112 113 /* 101 114 DataAccess da; 102 115 … … 116 129 da.printPlayers(); 117 130 cout << endl; 131 */ 118 132 119 sock =socket(AF_INET, SOCK_DGRAM, 0);133 sock = socket(AF_INET, SOCK_DGRAM, 0); 120 134 if (sock < 0) error("Opening socket"); 121 135 length = sizeof(server); … … 127 141 error("binding"); 128 142 143 set_nonblock(sock); 144 129 145 while (true) { 130 // if n == 0, means the client disconnected. may want to check this 146 147 usleep(5000); 148 131 149 n = receiveMessage(&clientMsg, sock, &from); 132 if (n < 0) 133 error("recieveMessage"); 134 135 processMessage(clientMsg, from, vctPlayers, num, serverMsg); 136 137 cout << "msg: " << serverMsg.buffer << endl; 138 139 n = sendMessage(&serverMsg, sock, &from); 140 if (n < 0) 141 error("sendMessage"); 142 } 150 151 if (n >= 0) { 152 cout << "Got a message" << endl; 153 154 processMessage(clientMsg, from, vctPlayers, num, serverMsg); 155 156 cout << "msg: " << serverMsg.buffer << endl; 157 158 n = sendMessage(&serverMsg, sock, &from); 159 if (n < 0) 160 error("sendMessage"); 161 } 162 163 } 164 143 165 return 0; 144 166 }
Note:
See TracChangeset
for help on using the changeset viewer.