Advertisement
Guest User

Untitled

a guest
Sep 30th, 2019
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. using namespace std;
  2. class Server {
  3. private:
  4.     int port;
  5.     std::string address;
  6.     WSADATA wsData;
  7.     WORD ver = MAKEWORD(2, 2);
  8.     int wsOk = WSAStartup(ver, &wsData);
  9.    
  10. public:
  11.     bool start(int p, std::string add) {
  12.         cout << 0x00 << endl;
  13.         //Document oof;
  14.         printf("Starting!\n");
  15.         if (wsOk != 0) {
  16.             cerr << "hey ok" << endl;
  17.             return false;
  18.         }
  19.         SOCKET listener = socket(AF_INET, SOCK_STREAM, 0);
  20.         if (listener == INVALID_SOCKET)
  21.         {
  22.             printf("testing!\n");
  23.             cerr << "Can't create socket, Err #" << WSAGetLastError() << endl;
  24.             WSACleanup();
  25.             return false;
  26.         }
  27.         if (wsOk != 0)
  28.         {
  29.             printf("testing2!\n");
  30.             cerr << "Can't start Winsock, Err #" << wsOk << endl;
  31.             return false;
  32.         }
  33.         int32_t *output;
  34.         this->port = p;
  35.         this->address = add;
  36.         sockaddr_in ser;
  37.         ser.sin_family = AF_INET;
  38.         ser.sin_addr.S_un.S_addr = INADDR_ANY;
  39.         ser.sin_port = htons(port);
  40.         bind(listener, (sockaddr*)&ser, sizeof(ser));
  41.         listen(listener, SOMAXCONN);
  42.         sockaddr_in client;
  43.         int cSize = sizeof(client);
  44.         SOCKET cSocket = accept(listener, (sockaddr*)&client, &cSize);
  45.         char host[NI_MAXHOST];
  46.         char service[NI_MAXHOST];
  47.         ZeroMemory(host, NI_MAXHOST);
  48.         ZeroMemory(service, NI_MAXHOST);
  49.         ByteBuffer m_buffer;
  50.         int num = 0;
  51.         if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0))
  52.         {
  53.             cout << host << " connected on port " << service << endl;
  54.         }
  55.         else {
  56.             //cout << "Warning\n";
  57.             inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
  58.             cout << host << " connected on port " << ntohs(client.sin_port) << endl;
  59.         }
  60.         //closesocket(listener);
  61.  
  62.         char buf[4096];
  63.         while (true)
  64.         {
  65.             const char* data = R"({
  66.    "version": {
  67.        "name": "1.14.4",
  68.        "protocol": 498
  69.    },
  70.    "players": {
  71.        "max": 100,
  72.        "online": 5,
  73.        "sample": [
  74.            {
  75.                "name": "thinkofdeath",
  76.                "id": "4566e69f-c907-48ee-8d71-d7ba5aa00d20"
  77.            }
  78.        ]
  79.     }, 
  80.    "description": {
  81.        "text": "Hello world"
  82.    },
  83. })";       
  84.  
  85.             //send(cSocket, data, sizeof(data), 0);
  86.             //cout << recvfrom(cSocket,(char*)data,sizeof(data),0,(sockaddr*)&client,(int*)sizeof(client));
  87.             //cout << buf << endl;
  88.             cout << "Preparing!\n";
  89.             try {
  90.                 cout << "trying!\n";
  91.                 //cout << typeid(buf).name << " UwU";
  92.                 //ZeroMemory(buf, sizeof(buf));
  93.                 //memset(buf, 0, 4096);
  94.                 size_t bytesReceived = recv(cSocket, buf, 4096, 0);
  95.                 string buffer = buf;
  96.                 memset(buf, 0, 4096);
  97.                 cout << buf << endl;
  98.                 cout << bytesReceived << endl;
  99.                 //cout << buf << endl;
  100.                 /*if (!m_buffer.Write(&buf, sizeof(buf)))
  101.                 {
  102.                     cout << "Protocol error!";
  103.                 }*/
  104.                 //printf(buf);
  105.                 //cout << sizeof(bytesReceived) << endl;
  106.                 int output = 0;
  107.                 //cout << readVarInt(&output, (unsigned char*)buf, bytesReceived) << "egg\n";
  108.                 //cout << output << " Pack "<< endl;
  109.                 //char input;
  110.                
  111.                 //writeString((char*)buf,(unsigned char*)data , sizeof(data));
  112.                 //send(cSocket, buf, sizeof(buf), 0);
  113.                 //
  114.                
  115.                 //if (readVarInt(&output, (unsigned char*)buf, bytesReceived) == 0) {
  116.                     //printf("sending int\n");
  117.                     //WriteInt(cSocket, 0x00);
  118.                     //WriteString(cSocket, data);
  119.                     //printf("sent int?\n");
  120.                 //}
  121.                 //else if (readVarInt(&output, (unsigned char*)buf, bytesReceived) == 1) {
  122.                     //printf("sending JSON\n");
  123.                    
  124.                 //  //printf("sent JSON\n");
  125.                 //}
  126.                 num++;
  127.                 //WriteInt(cSocket, 0x00);
  128.                
  129.                 if (bytesReceived == 0)
  130.                 {
  131.                     cout << "Client left!\n";
  132.                     cout << "Client " << host <<" left\n" << endl;
  133.                     //system("pause");
  134.                     closesocket(cSocket);
  135.                 }
  136.                
  137.                 //send(cSocket, ;
  138.                 //cout << "ok" << bytesReceived << "read?" << endl;
  139.                 //puts(buf);
  140.                 //printf("\n");
  141.                 //send(cSocket, data, sizeof(data), 0);
  142.                 //Client State request stage
  143.                 //1 = Declare Handshake
  144.                 //2 = Login Request
  145.                 //cout << buf << "data" << endl;
  146.                 //send(cSocket, buf, bytesReceived + 1, 0);
  147.             }
  148.             catch (...) {
  149.                 printf("Error: %s\n", "hi");
  150.             }
  151.         }
  152.         return true;
  153.     };
  154. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement