Guessing digital games with TCP communication in Windows

Source: Internet
Author: User
Tags strcmp htons

server#include<iostream>using namespace std; #include <Winsock2.h> #include <stdlib.h > #include <time.h> #include <vector> #include <string> #include <Windows.h> #define  _ listen_sum_ 5#define _ip_  "127.0.0.1" #define  _port_ 8080#pragma comment (Lib, "Ws2 _32.lib ") #define &NBSP;_CRT_SECURE_NO_WARNINGS#DEFINE&NBSP;_SIZE_&NBSP;256VECTOR&LT;STRING&GT;&NBSP;USER_IP (_ Size_) int scos[_size_];//the number of games corresponding to the corresponding User IP int user_index = 0;char result[20];//store game results, Ranked Int startup (Char*ip, int port) {Int sock = socket (AF_INET, SOCK_STREAM, &NBSP;0);//Create Socket if  (sock < 0) {printf ("sock"); exit (1);} Struct sockaddr_in local;local.sin_family = af_inet;local.sin_port = htons (_PORT _); Local.sin_addr.s_addr = inet_addr (_IP_);if  (Bind (sock,  (struct sockaddr*) & Local, sizeof (local)) <0)//bound port number if  (sock < 0) {printf ("bind"); exit (2);} if  (Listen (sock, _listen_sum_)  < 0)//Set listening status {printf ("listen"); exit (3);} Return sock;} Void menu (Int new_sock)//Print the Game menu {char buf[1024] =  "--------------------------\n1 to the User: Start play\n2:restart play\n0:quit play\n--------------------------\ n "; Send (New_sock, buf,  sizeof (BUF) -1, 0);} Int main () {wsadata wsadata; WSAStartup (0x0202, &wsadata);              //Initialize Winsockint sock = startup (_ip_, _port_);//Get Server sockets struct sockaddr_in  Client;int len = sizeof (client);while  (1) {int new_sock = accept (sock,  (struct sockaddr*) &client, &len);//Receive User connection User_ip[user_index] = inet_ntoa ( CLIENT.SIN_ADDR)///store user ipscos[user_index] = 0;//Initialize user game Count if  (new_sock < 0) {printf ( "New_sock"); continue;} Cout <<  "Receive sock"  << new_sock << endl;while  (1) {menu ( New_sock);        //sends game instructions to the client//play (New_sock); Char buf[1024];buf[0]  =  ' Srand '; Unsigned int (Time (NULL)); Int num = rand ()  % 100;     //generates a random number Int ret = 0;char msg[1024];recv (new_sock, buf, sizeof ( BUF) -1, 0);//Receive Client User selection: Start the game or exit Ret = atoi (BUF);//Determine user input switch  (ret) {case 2:strcpy (msg ,  "Your restart play");//Restart the game send (New_sock, msg, sizeof (msg) -1, 0); continue; case 0:strcpy (msg,  "your are quit");//Quit Game Send (New_sock, msg, sizeof (msg)-1,  0); return 0;case 1:strcpy (msg,  "Your are start");//Start the game send (New_sock, msg,  sizeof (msg) -1, 0);} while  (1) {memset (buf,  '),  sizeof (BUF)), memset (msg,  ' n ',  sizeof (msg)); recv(New_sock, buf, sizeof (BUF) -1, 0);//Receive user input value//cout << buf <<  Endl;ret = atoi (BUF);//conversion to a number for comparison if  (ret > num)//user input number Large {strcpy (msg,  "Your  input is biger "), Send (New_sock, msg, sizeof (msg) -1, 0),//Send the game result to the user: large number of inputs scos[ user_index]++;//game Count plus 1}else if  (ret < num)//user input number is smaller {strcpy (msg,  "Your input  is smaller "), Send (New_sock, msg, sizeof (msg) -1, 0),//Send the game result to the user: small number of inputs scos[user_index] ++;//game times plus 1}else//users guessed {strcpy (msg,  "you are right"); int tmp[_size_] ;//sort in another array, Do not change the original record array for  (INT&NBSP;I&NBSP;=&NBSP;0;&NBSP;I&NBSP;&LT;=&NBSP;USER_INDEX;&NBSP;++I)//Sort all user games, get ranked {tmp[ I] = scos[i];} int flag = 0;//bubble sort Get leaderboard for  (Int i = 0; i <= user_index  - 1; ++i) {flag = 0;for  (int j = 0; j <= user_INDEX&NBSP;-&NBSP;I&NBSP;-&NBSP;1;&NBSP;++J) {if  (tmp[j]>tmp[j + 1]) {Swap (tmp[j], tmp[j  + 1]); flag = 1;} if  (flag == 0)//bubble sort optimization: If it has been ordered, exit the loop immediately;}} int top = 0;//Storage ranking for  (int i = 0; i <= user_index; + +i) {if  (Tmp[i] == scos[user_index]) {//ranking from the beginning Top = i + 1;break;}} cout << scos[user_index] <<  " "  << top;sprintf (result,   "You ip is %s,you play %c counts,is top %c",  user_ip[user_ Index].c_str (), scos[user_index]+ ' 0 ', top+ ' 0 ');//output The number of guesses to the customer and return the rank strcat (msg,result); Send (New_sock, msg,  sizeof (msg) -1, 0);//Send the game results to the user scos[user_index] = 0;//reset guess number 0break;}}} return 0;} client#define _crt_secure_no_warnings#include<iostream>using namespace std; #include < winsock2.h> #include <stdlib.h> #define  _ip_  "127.0.0.1" #define  _port_ 8080#pragma comment (lib, "Ws2_32.lib") Int main () { wsadata wsadata; WSAStartup (0x0202, &wsadata);                 //Initialize Winsockint sock = socket (af_inet, sock_stream, 0);// The client generates sockets without binding if  (sock < 0) {printf ("sock"); exit (1);} Struct sockaddr_in remote;remote.sin_family = af_inet;remote.sin_port = htons (_ Port_); remote.sin_addr.s_addr = inet_addr (_IP_); Int ret = connect (sock,  (struct  sockaddr*) &remote, sizeof (remote));//Connection Server//gets (BUF);while  (1) {//play (sock); char buf [1024];buf[0] =  ' + '; recv (sock, buf, sizeof (BUF) -1, 0);//Receive Server-side game description cout <<  buf << endl;//Print game instruction gets (BUF);//Receive user Select Send (sock, buf, sizeof (BUF) -1, 0); /Send Selection recv (sock, buf, sizeof(BUF) -1, 0)//Receive feedback Information if  (strcmp (buf,  "Your restart play")  == 0)// According to feedback information to get the user next action continue;//restart game else if  (strcmp (buf,  "Your are quit")  == 0) return 0;//Exit Game else if  (strcmp (buf,  "Your are start")  == 0)//start Game {cout  <<  "Start play"  << endl;cout <<  "please input  Num in 0~99 "&NBSP;&LT;&LT;&NBSP;ENDL;} while  (1) {printf ("Please input num:"); gets (buf);//input value Send (sock, buf, sizeof (BUF)-1, &NBSP;0);//want the server to send user value//memset (buf,  ' ",  sizeof (BUF)); Recv (sock, buf, sizeof (BUF) -1,  0)//Receive user feedback cout << buf << endl;if  (strncmp (buf,  "You are  right ",  strlen (" You are right "))  == 0)//Guess to exit {cout << " you  Win " << endl;break;} memset (buf,  ',  sizeof (BUF));//Reset BUF}}}

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/83/F5/wKioL1eB4ULBbRatAADrS1cMXLU758.jpg "title=" QQ picture 20160710134652.jpg "alt=" Wkiol1eb4ulbbrataadrs1cmxlu758.jpg "/>

This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1813947

Guessing digital games with TCP communication in Windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.