source: network-game/server/server.cpp@ e084950

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

Converted the server to use sendMessage and receiveMessage and it now asks the user to guess a number between 1 and 1000

  • Property mode set to 100644
File size: 3.1 KB
RevLine 
[e084950]1#include "../common/compiler.h"
2
[e3535b3]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>
[e084950]18#include "../common/message.h"
[e3535b3]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;
[e084950]34 NETWORK_MSG clientMsg, serverMsg;
35
36 srand(time(NULL));
37 int num = (rand() % 1000) + 1;
38
39 cout << "num: " << num << endl;
[e3535b3]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)
[e084950]58 error("binding");
[e3535b3]59 fromlen = sizeof(struct sockaddr_in);
60 while (1) {
[e084950]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"))
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 if (guess < 1 || guess > 1000) {
73 strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
74 }else if(guess > num)
75 strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
76 else if(guess < num)
77 strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
78 else if(guess == num) {
79 strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
80 num = (rand() % 1000) + 1;
81 }
82 }
83
84 cout << "msg: " << serverMsg.buffer << endl;
85
86 n = sendMessage(&serverMsg, sock, &from);
87 if (n < 0)
88 error("sendMessage");
[e3535b3]89 }
90 return 0;
91}
92
93int dbtest()
94{
95 MYSQL *connection, mysql;
96 MYSQL_RES *result;
97 MYSQL_ROW row;
98 int query_state;
99
100 mysql_init(&mysql);
101
102 connection = mysql_real_connect(&mysql,"localhost","pythonAdmin","pyMaster09*","pythondb",0,0,0);
103
104 if (connection == NULL) {
105 cout << mysql_error(&mysql) << endl;
106 return 1;
107 }else
108 cout << "Connection successful" << endl;
109
110 query_state = mysql_query(connection, "SELECT * FROM users");
111
112 if (query_state !=0) {
113 cout << mysql_error(connection) << endl;
114 return 1;
115 }
116
117 result = mysql_store_result(connection);
118
119 while ( ( row = mysql_fetch_row(result)) != NULL ) {
120 cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
121 }
122
123 mysql_free_result(result);
124 mysql_close(connection);
125
126 cout << "Test finished" << endl;
127
128 return 0;
129}
Note: See TracBrowser for help on using the repository browser.