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

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

Initial commit for server

  • Property mode set to 100644
File size: 2.1 KB
Line 
1#include <sys/types.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <sys/socket.h>
5#include <netinet/in.h>
6#include <string.h>
7#include <netdb.h>
8#include <stdio.h>
9#include <iostream>
10
11#include <mysql/mysql.h>
12
13#include <openssl/bio.h>
14#include <openssl/ssl.h>
15#include <openssl/err.h>
16
17using namespace std;
18
19void error(const char *msg)
20{
21 perror(msg);
22 exit(0);
23}
24
25int main(int argc, char *argv[])
26{
27 int sock, length, n;
28 socklen_t fromlen;
29 struct sockaddr_in server;
30 struct sockaddr_in from;
31 char buf[1024];
32
33 SSL_load_error_strings();
34 ERR_load_BIO_strings();
35 OpenSSL_add_all_algorithms();
36
37 if (argc < 2) {
38 fprintf(stderr, "ERROR, no port provided\n");
39 exit(0);
40 }
41
42 sock=socket(AF_INET, SOCK_DGRAM, 0);
43 if (sock < 0) error("Opening socket");
44 length = sizeof(server);
45 bzero(&server,length);
46 server.sin_family=AF_INET;
47 server.sin_addr.s_addr=INADDR_ANY;
48 server.sin_port=htons(atoi(argv[1]));
49 if (bind(sock,(struct sockaddr *)&server,length)<0)
50 error("binding");
51 fromlen = sizeof(struct sockaddr_in);
52 while (1) {
53 n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);
54 if (n < 0) error("recvfrom");
55 write(1,"Received a datagram: ",21);
56 write(1,buf,n);
57 n = sendto(sock,"Got your message\n",17,
58 0,(struct sockaddr *)&from,fromlen);
59 if (n < 0) error("sendto");
60 }
61 return 0;
62}
63
64int dbtest()
65{
66 MYSQL *connection, mysql;
67 MYSQL_RES *result;
68 MYSQL_ROW row;
69 int query_state;
70
71 mysql_init(&mysql);
72
73 connection = mysql_real_connect(&mysql,"localhost","pythonAdmin","pyMaster09*","pythondb",0,0,0);
74
75 if (connection == NULL) {
76 cout << mysql_error(&mysql) << endl;
77 return 1;
78 }else
79 cout << "Connection successful" << endl;
80
81 query_state = mysql_query(connection, "SELECT * FROM users");
82
83 if (query_state !=0) {
84 cout << mysql_error(connection) << endl;
85 return 1;
86 }
87
88 result = mysql_store_result(connection);
89
90 while ( ( row = mysql_fetch_row(result)) != NULL ) {
91 cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
92 }
93
94 mysql_free_result(result);
95 mysql_close(connection);
96
97 cout << "Test finished" << endl;
98
99 return 0;
100}
Note: See TracBrowser for help on using the repository browser.