Click here to Skip to main content
15,887,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! So, I am creating a C++ application that saves takes some metrics from the computer, and then upload them to a server (made with PHP) in order for the user to access the data later at some point.

This code that i created for this job, is posted bellow.

The string with the message, is taken with postman. Now the issue is that everything seems to execute well in the method, but i don't get any data on to my database. This is certainly NOT a PHP issue, because by hand it works. Any ideas what it might cause the problem ?

What I have tried:

This is is the code that i came up, that sends the http post request:

BOOL SendPostRequest(string server, string message)
{
	struct sockaddr_in address;
	unsigned long ulAddr;
	SOCKET sock;
	int connectResult;
	int messageSize = strlen(message.c_str());

	sock = socket(AF_INET, SOCK_STREAM, 0);

	if (sock == INVALID_SOCKET)
	{
		closesocket(sock);
		return FALSE;
	}

	address.sin_family = AF_INET;
	address.sin_port = htons(80);

	ulAddr = inet_addr(server.c_str());

	memcpy(&address.sin_addr, &ulAddr, sizeof ulAddr);

	connectResult = connect(sock, reinterpret_cast<sockaddr*>(&address), sizeof address);

	if (connectResult != 0)
	{
		closesocket(sock);
		return FALSE;
	}

	send(sock, message.c_str(), messageSize, 0);

	closesocket(sock);

	return TRUE;
}


And i call/use it like this:

string message = "POST /submit.php?Url=www.example.com&Username=Revolution&Password=samplepassword HTTP/1.1"
"Host: localhost"
"Cache-Control: no-cache";
BOOL result = SendPostRequest("192.168.1.1", message);
Posted
Updated 2-Apr-18 7:37am
v2
Comments
Richard MacCutchan 2-Apr-18 15:36pm    
What response do you get from the post request, what information gets processed in the PHP code? You need to trace the path all the way through both client and server.
Richard MacCutchan 2-Apr-18 15:41pm    
int messageSize = strlen(message.c_str());
Why are you using strlen here, and not message.length?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900