- Timestamp:
- Nov 24, 2012, 6:55:37 PM (12 years ago)
- Branches:
- master
- Children:
- 6475138
- Parents:
- d352805
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
client/Client/main.cpp
rd352805 r9a3e6b1 18 18 #include <string> 19 19 #include <iostream> 20 #include <vector> 20 21 21 22 #include <allegro5/allegro.h> … … 54 55 bool redraw = true; 55 56 bool doexit = false; 56 57 58 vector<string> vctChat; 59 string strPrompt; 60 string strEnteredInput; 61 57 62 if(!al_init()) { 58 63 fprintf(stderr, "failed to initialize allegro!\n"); … … 121 126 122 127 al_flip_display(); 123 128 129 int sock, n; 130 struct sockaddr_in server, from; 131 struct hostent *hp; 132 char buffer[256]; 133 NETWORK_MSG msgTo, msgFrom; 134 135 if (argc != 3) { 136 cout << "Usage: server port" << endl; 137 exit(1); 138 } 139 140 initWinSock(); 141 142 sock = socket(AF_INET, SOCK_DGRAM, 0); 143 if (sock < 0) 144 error("socket"); 145 146 server.sin_family = AF_INET; 147 hp = gethostbyname(argv[1]); 148 if (hp==0) 149 error("Unknown host"); 150 151 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); 152 server.sin_port = htons(atoi(argv[2])); 153 154 strcpy(msgTo.buffer, "Hello"); 155 n=sendMessage(&msgTo, sock, &server); 156 if (n < 0) 157 error("sendMessage"); 158 159 n = receiveMessage(&msgFrom, sock, &from); 160 if (n < 0) 161 error("receiveMessage"); 162 163 vctChat.push_back(string(msgFrom.buffer)); 164 124 165 al_start_timer(timer); 125 166 … … 149 190 } 150 191 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 151 break;192 doexit = true; 152 193 } 153 194 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { … … 168 209 key[KEY_RIGHT] = true; 169 210 break; 170 } 211 212 case ALLEGRO_KEY_ENTER: 213 strEnteredInput = strPrompt; 214 strPrompt.clear(); 215 216 if (strEnteredInput.compare("q") == 0) { 217 doexit = true; 218 } 219 else 220 { 221 strcpy(msgTo.buffer, strEnteredInput.c_str()); 222 n=sendMessage(&msgTo, sock, &server); 223 if (n < 0) 224 error("sendMessage"); 225 226 n = receiveMessage(&msgFrom, sock, &from); 227 if (n < 0) 228 error("receiveMessage"); 229 230 vctChat.push_back(string(msgFrom.buffer)); 231 } 232 233 break; 234 } 235 236 if(ALLEGRO_KEY_A <= ev.keyboard.keycode && ev.keyboard.keycode <= ALLEGRO_KEY_Z) 237 { 238 char newChar = 'a'+ev.keyboard.keycode-ALLEGRO_KEY_A; 239 strPrompt.append(1, newChar); 240 } 241 242 if(ALLEGRO_KEY_0 <= ev.keyboard.keycode && ev.keyboard.keycode <= ALLEGRO_KEY_9) 243 { 244 char newChar = '0'+ev.keyboard.keycode-ALLEGRO_KEY_0; 245 strPrompt.append(1, newChar); 246 } 171 247 } 172 248 else if(ev.type == ALLEGRO_EVENT_KEY_UP) { … … 200 276 201 277 al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0); 202 al_draw_text(font, al_map_rgb(255,255,255), 10, 10, ALLEGRO_ALIGN_LEFT, "Your Text Here!"); 278 279 for(int x=0; x<vctChat.size(); x++) 280 al_draw_text(font, al_map_rgb(255,255,255), 10, 10+x*15, ALLEGRO_ALIGN_LEFT, vctChat[x].c_str()); 281 282 al_draw_text(font, al_map_rgb(255,255,255), 10, 460, ALLEGRO_ALIGN_LEFT, strPrompt.c_str()); 203 283 204 284 al_flip_display(); 205 285 } 206 286 } 287 288 #if defined WINDOWS 289 closesocket(sock); 290 #elif defined LINUX 291 close(sock); 292 #endif 293 294 shutdownWinSock(); 207 295 208 296 al_destroy_event_queue(event_queue); … … 213 301 return 0; 214 302 } 215 216 /*217 int main(int argc, char *argv[])218 {219 int sock, n;220 struct sockaddr_in server, from;221 struct hostent *hp;222 char buffer[256];223 NETWORK_MSG msgTo, msgFrom;224 225 if (argc != 3) {226 cout << "Usage: server port" << endl;227 exit(1);228 }229 230 initWinSock();231 232 sock = socket(AF_INET, SOCK_DGRAM, 0);233 if (sock < 0)234 error("socket");235 236 server.sin_family = AF_INET;237 hp = gethostbyname(argv[1]);238 if (hp==0)239 error("Unknown host");240 241 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);242 server.sin_port = htons(atoi(argv[2]));243 244 strcpy(msgTo.buffer, "Hello");245 n=sendMessage(&msgTo, sock, &server);246 if (n < 0)247 error("sendMessage");248 249 n = receiveMessage(&msgFrom, sock, &from);250 if (n < 0)251 error("receiveMessage");252 253 cout << msgFrom.buffer << endl;254 255 while(true) {256 cout << "Please enter a message (or q to quit): ";257 cin.getline(msgTo.buffer, 256);258 259 if (strcmp(msgTo.buffer, "q") == 0) {260 break;261 }262 263 n=sendMessage(&msgTo, sock, &server);264 if (n < 0)265 error("sendMessage");266 267 n = receiveMessage(&msgFrom, sock, &from);268 if (n < 0)269 error("receiveMessage");270 271 cout << msgFrom.buffer << endl;272 }273 274 #if defined WINDOWS275 closesocket(sock);276 #elif defined LINUX277 close(sock);278 #endif279 280 shutdownWinSock();281 282 cout << "Thank you for playing!" << endl;283 getchar();284 285 return 0;286 }287 */288 303 289 304 void initWinSock()
Note:
See TracChangeset
for help on using the changeset viewer.