- Timestamp:
- Nov 23, 2012, 8:52:59 PM (12 years ago)
- Branches:
- master
- Children:
- 6c92572
- Parents:
- d87708d
- Location:
- server
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
server/makefile
rd87708d re084950 1 server : server.cpp1 server : ../common/message.cpp server.cpp 2 2 g++ -o $@ $? -lssl -lmysqlclient -
server/server.cpp
rd87708d re084950 1 #include "../common/compiler.h" 2 1 3 #include <sys/types.h> 2 4 #include <stdlib.h> … … 14 16 #include <openssl/ssl.h> 15 17 #include <openssl/err.h> 18 #include "../common/message.h" 16 19 17 20 using namespace std; … … 29 32 struct sockaddr_in server; 30 33 struct sockaddr_in from; 31 char buf[1024]; 34 NETWORK_MSG clientMsg, serverMsg; 35 36 srand(time(NULL)); 37 int num = (rand() % 1000) + 1; 38 39 cout << "num: " << num << endl; 32 40 33 41 SSL_load_error_strings(); … … 48 56 server.sin_port=htons(atoi(argv[1])); 49 57 if (bind(sock,(struct sockaddr *)&server,length)<0) 50 58 error("binding"); 51 59 fromlen = sizeof(struct sockaddr_in); 52 60 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"); 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"); 60 89 } 61 90 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.