Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- class Server {
- private:
- int port;
- std::string address;
- WSADATA wsData;
- WORD ver = MAKEWORD(2, 2);
- int wsOk = WSAStartup(ver, &wsData);
- public:
- bool start(int p, std::string add) {
- cout << 0x00 << endl;
- //Document oof;
- printf("Starting!\n");
- if (wsOk != 0) {
- cerr << "hey ok" << endl;
- return false;
- }
- SOCKET listener = socket(AF_INET, SOCK_STREAM, 0);
- if (listener == INVALID_SOCKET)
- {
- printf("testing!\n");
- cerr << "Can't create socket, Err #" << WSAGetLastError() << endl;
- WSACleanup();
- return false;
- }
- if (wsOk != 0)
- {
- printf("testing2!\n");
- cerr << "Can't start Winsock, Err #" << wsOk << endl;
- return false;
- }
- int32_t *output;
- this->port = p;
- this->address = add;
- sockaddr_in ser;
- ser.sin_family = AF_INET;
- ser.sin_addr.S_un.S_addr = INADDR_ANY;
- ser.sin_port = htons(port);
- bind(listener, (sockaddr*)&ser, sizeof(ser));
- listen(listener, SOMAXCONN);
- sockaddr_in client;
- int cSize = sizeof(client);
- SOCKET cSocket = accept(listener, (sockaddr*)&client, &cSize);
- char host[NI_MAXHOST];
- char service[NI_MAXHOST];
- ZeroMemory(host, NI_MAXHOST);
- ZeroMemory(service, NI_MAXHOST);
- ByteBuffer m_buffer;
- int num = 0;
- if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0))
- {
- cout << host << " connected on port " << service << endl;
- }
- else {
- //cout << "Warning\n";
- inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
- cout << host << " connected on port " << ntohs(client.sin_port) << endl;
- }
- //closesocket(listener);
- char buf[4096];
- while (true)
- {
- const char* data = R"({
- "version": {
- "name": "1.14.4",
- "protocol": 498
- },
- "players": {
- "max": 100,
- "online": 5,
- "sample": [
- {
- "name": "thinkofdeath",
- "id": "4566e69f-c907-48ee-8d71-d7ba5aa00d20"
- }
- ]
- },
- "description": {
- "text": "Hello world"
- },
- })";
- //send(cSocket, data, sizeof(data), 0);
- //cout << recvfrom(cSocket,(char*)data,sizeof(data),0,(sockaddr*)&client,(int*)sizeof(client));
- //cout << buf << endl;
- cout << "Preparing!\n";
- try {
- cout << "trying!\n";
- //cout << typeid(buf).name << " UwU";
- //ZeroMemory(buf, sizeof(buf));
- //memset(buf, 0, 4096);
- size_t bytesReceived = recv(cSocket, buf, 4096, 0);
- string buffer = buf;
- memset(buf, 0, 4096);
- cout << buf << endl;
- cout << bytesReceived << endl;
- //cout << buf << endl;
- /*if (!m_buffer.Write(&buf, sizeof(buf)))
- {
- cout << "Protocol error!";
- }*/
- //printf(buf);
- //cout << sizeof(bytesReceived) << endl;
- int output = 0;
- //cout << readVarInt(&output, (unsigned char*)buf, bytesReceived) << "egg\n";
- //cout << output << " Pack "<< endl;
- //char input;
- //writeString((char*)buf,(unsigned char*)data , sizeof(data));
- //send(cSocket, buf, sizeof(buf), 0);
- //
- //if (readVarInt(&output, (unsigned char*)buf, bytesReceived) == 0) {
- //printf("sending int\n");
- //WriteInt(cSocket, 0x00);
- //WriteString(cSocket, data);
- //printf("sent int?\n");
- //}
- //else if (readVarInt(&output, (unsigned char*)buf, bytesReceived) == 1) {
- //printf("sending JSON\n");
- // //printf("sent JSON\n");
- //}
- num++;
- //WriteInt(cSocket, 0x00);
- if (bytesReceived == 0)
- {
- cout << "Client left!\n";
- cout << "Client " << host <<" left\n" << endl;
- //system("pause");
- closesocket(cSocket);
- }
- //send(cSocket, ;
- //cout << "ok" << bytesReceived << "read?" << endl;
- //puts(buf);
- //printf("\n");
- //send(cSocket, data, sizeof(data), 0);
- //Client State request stage
- //1 = Declare Handshake
- //2 = Login Request
- //cout << buf << "data" << endl;
- //send(cSocket, buf, bytesReceived + 1, 0);
- }
- catch (...) {
- printf("Error: %s\n", "hi");
- }
- }
- return true;
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement