source: network-game/server/server.cpp@ 0dde5da

Last change on this file since 0dde5da was cb1f288, checked in by dportnoy <dmp1488@…>, 12 years ago

Fixed a bug with detecting the client Hello message

  • Property mode set to 100644
File size: 3.1 KB
Line 
1#include "../common/compiler.h"
2
3#include <sys/types.h>
4#include <stdlib.h>
5#include <unistd.h>
6#include <sys/socket.h>
7#include <netinet/in.h>
8#include <string.h>
9#include <netdb.h>
10#include <stdio.h>
11#include <iostream>
12
13#include <mysql/mysql.h>
14
15#include <openssl/bio.h>
16#include <openssl/ssl.h>
17#include <openssl/err.h>
18#include "../common/message.h"
19
20using namespace std;
21
22void error(const char *msg)
23{
24 perror(msg);
25 exit(0);
26}
27
28int main(int argc, char *argv[])
29{
30 int sock, length, n;
31 socklen_t fromlen;
32 struct sockaddr_in server;
33 struct sockaddr_in from;
34 NETWORK_MSG clientMsg, serverMsg;
35
36 srand(time(NULL));
37 int num = (rand() % 1000) + 1;
38
39 cout << "num: " << num << endl;
40
41 SSL_load_error_strings();
42 ERR_load_BIO_strings();
43 OpenSSL_add_all_algorithms();
44
45 if (argc < 2) {
46 fprintf(stderr, "ERROR, no port provided\n");
47 exit(0);
48 }
49
50 sock=socket(AF_INET, SOCK_DGRAM, 0);
51 if (sock < 0) error("Opening socket");
52 length = sizeof(server);
53 bzero(&server,length);
54 server.sin_family=AF_INET;
55 server.sin_addr.s_addr=INADDR_ANY;
56 server.sin_port=htons(atoi(argv[1]));
57 if (bind(sock,(struct sockaddr *)&server,length)<0)
58 error("binding");
59 fromlen = sizeof(struct sockaddr_in);
60 while (true) {
61 n = receiveMessage(&clientMsg, sock, &from);
62 if (n < 0)
63 error("recieveMessage");
64 cout << "msg: " << clientMsg.buffer << endl;
65
66 if (strcmp(clientMsg.buffer, "Hello") == 0)
67 {
68 strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
69 }else {
70 int guess = atoi(clientMsg.buffer);
71
72 cout << "guess: " << guess << endl;
73
74 if (guess < 1 || guess > 1000) {
75 strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
76 }else if(guess > num)
77 strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
78 else if(guess < num)
79 strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
80 else if(guess == num) {
81 strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
82 num = (rand() % 1000) + 1;
83 }
84 }
85
86 cout << "msg: " << serverMsg.buffer << endl;
87
88 n = sendMessage(&serverMsg, sock, &from);
89 if (n < 0)
90 error("sendMessage");
91 }
92 return 0;
93}
94
95int dbtest()
96{
97 MYSQL *connection, mysql;
98 MYSQL_RES *result;
99 MYSQL_ROW row;
100 int query_state;
101
102 mysql_init(&mysql);
103
104 connection = mysql_real_connect(&mysql,"localhost","pythonAdmin","pyMaster09*","pythondb",0,0,0);
105
106 if (connection == NULL) {
107 cout << mysql_error(&mysql) << endl;
108 return 1;
109 }else
110 cout << "Connection successful" << endl;
111
112 query_state = mysql_query(connection, "SELECT * FROM users");
113
114 if (query_state !=0) {
115 cout << mysql_error(connection) << endl;
116 return 1;
117 }
118
119 result = mysql_store_result(connection);
120
121 while ( ( row = mysql_fetch_row(result)) != NULL ) {
122 cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
123 }
124
125 mysql_free_result(result);
126 mysql_close(connection);
127
128 cout << "Test finished" << endl;
129
130 return 0;
131}
Note: See TracBrowser for help on using the repository browser.