[Connect API] No response from the game

I can connect to the game, but it wont respond to any request.
Can someone tell me what is wrong in my code?

#pragma comment(lib, “ws2_32.lib”)

#include
#include <WS2tcpip.h>
#include <nlohmann/json.hpp>

#define IFC_UDP_PORT 15000

using json = nlohmann::json;

class client
{
private:
std::string m_device;
std::string m_ipv4;
unsigned int m_port;

public:
std::string device() const
{
return m_device;
}

std::string ipv4() const
{
return m_ipv4;
}

unsigned int port() const
{
return m_port;
}

client(std::string device, std::string ipv4, unsigned int port)
{
m_device = device;
m_ipv4 = ipv4;
m_port = port;
}

client()
{

}

static bool create(std::string plain, client& buffer)
{
json j = json::parse(plain);

  if (!j.contains("Addresses") || !j.contains("DeviceID") || !j.contains("Port"))
  	return false;

  std::string	device = j["DeviceID"].get<std::string>();
  std::string	ipv4 = j["Addresses"][0].get<std::string>();
  unsigned int port = j["Port"].get<unsigned int>();

  buffer = client(device, ipv4, port);

  return true;

}
};

int main()
{

WSADATA wsa_data;
WORD wsa_version = MAKEWORD(2, 2);

std::cout << “Starting up WSA.” << std::endl;

if (WSAStartup(wsa_version, &wsa_data) != 0)
{
std::cerr << “An error occured while starting up WSA.”;
return EXIT_FAILURE;
}

SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);

if (sock == INVALID_SOCKET)
{
std::cerr << “An error occured while creating socket.”;
return EXIT_FAILURE;
}

sockaddr_in hint;
hint.sin_addr.S_un.S_addr = ADDR_ANY;
hint.sin_family = AF_INET;
hint.sin_port = htons(IFC_UDP_PORT);

std::cout << “Binding socket.” << std::endl;

if(bind(sock, (LPSOCKADDR)&hint, sizeof(hint)) == SOCKET_ERROR)
{
std::cerr << “An error occured while binding socket.”;
return EXIT_FAILURE;
}

char buffer[1024];
ZeroMemory(buffer, sizeof(buffer));

int bytesIn = recv(sock, buffer, sizeof(buffer), 0);

client c;

if(!client::create(buffer, c))
{
std::cout << “Invalid message.”;
return EXIT_FAILURE;
}

sock = socket(AF_INET, SOCK_STREAM, 0);

if (sock == INVALID_SOCKET)
{
std::cerr << “An error occured while creating socket.”;
return EXIT_FAILURE;
}

std::cout << "Connecting to " << c.device() << ‘.’ << std::endl;

ZeroMemory(&hint, sizeof(hint));

hint.sin_family = AF_INET;
hint.sin_port = htons(c.port());
inet_pton(AF_INET, c.ipv4().c_str(), &hint.sin_addr);

if(connect(sock, (LPSOCKADDR)&hint, sizeof(hint)) == SOCKET_ERROR)
{
std::cout << “Connection failed. #” << WSAGetLastError();
return EXIT_FAILURE;
}

std::cout << “Connected.” << std::endl;

std::string example_command = “{ “Command”: “Airplane.GetState”, “Parameters”: }”;
int bytesOut = send(sock, example_command.c_str(), example_command.length(), 0);

if(bytesOut == example_command.length())
{
ZeroMemory(&buffer, sizeof(buffer));
bytesIn = recv(sock, buffer, sizeof(buffer), 0);

  if (bytesIn > 0)
  	std::cout << buffer << std::endl;

}

std::cout << “Cleaning up WSA.” << std::endl;

if (WSACleanup() != 0)
{
std::cerr << “An error occured while cleaning up WSA.”;
return EXIT_FAILURE;
}

}

2 Likes

So if I’m understanding correctly you are able to get a connection but whenever you try and get a state it doesn’t work?

If so, have you tried sending a “-1” then a Boolean value of false to get the manifest?