I've basically got some code in PHP that simply connects to my C++ server that's listening on a port, sends a message, and then closes the socket.
I'm trying to read what I'm sending but the thing is....if I connect too many times the C++ server eventually spews out an exception:
Unhandled exception at 0x00B6C99B in Server.exe: 0xC0000005: Access violation reading location 0xFEEEFEEE.
Basically on page load in the PHP script it connects the socket (TCP) to the C++ server, sends a message, and then closes the socket. So if I hold down refresh by the time it's done this about 34 times (number varies) I get an exception.
In the stack I can see that some variables have: "udp=??? format=???", which is extremely weird because these are all ready defined, no idea why there's a question mark.
And also the data I send from PHP is:
$message = "hello";
And in the locals in Visual Studio the data turns out to be:
"helloðºðºðºðºðºðºîþ««««««««îþ"
Some other variables have "Error reading characters of string" while others have: "Unable to read memory"
Here's my code:
double webListen = tcplisten(cst::WEB_PORT, 100, 1);
setnagle(webListen, true);
std::list<double> webUsers;
if (webListen <= 0) {
cout << "Failed to listen on port " << cst::WEB_PORT <<endl;
}
cout << endl << "Server listening on port " << cst::WEB_PORT << endl;
while(1){
double WebUser = tcpaccept(webListen, 1);
if(WebUser > -1){
cout<< "NEW WEB: " << WebUser << endl;
webUsers.push_back(WebUser);
}
std::list<double>::iterator it = webUsers.begin();
while (it != webUsers.end())
{
cout << "SOCK: " << *it << endl;
//USE A TRY CATCH!!!
//try{
double Size = receivemessage(*it, 1024, 2); // Messages coming from web users (whether it be from website or someone using api)
if(Size == -1){
}
else if(Size == 0){
cout << "WEB GONE: " << *it << endl;
webUsers.erase(it++);
}
else if(Size > 0){
cout << "SIZE: " << Size << endl;
}
}
}
The exception happens when it tries to access the UDP variable in this function:
int CSocket::receivemessage(int len, CBuffer*destination)
{
if(sockid<0)return -1;
int size = -1;
char* buff = NULL;
if(udp)
{
size = 8195;
buff = new char[size];
size = recvfrom(sockid, buff, size, 0, (SOCKADDR *)&SenderAddr, &SenderAddrSize);
} else
{
if(format == 0 && !len)
{
unsigned short length;
if(recv(sockid, (char*)&length, 2, 0) == SOCKET_ERROR)return -1;
buff = new char[length];
size = recv(sockid, buff, length, 0);
} else if(format == 1 && !len)
{
size = 65536;
buff = new char[size];
size = receivetext(buff, size);
} else if(format == 2 || len > 0)
{
buff = new char[len];
size = recv(sockid, buff, len, 0);
}
}
if(size > 0)
{
destination->clear();
destination->addBuffer(buff, size);
}
if(buff != NULL)delete buff;
return size;
}
But I mean variables such as udp and format are all ready defined... Is it the CSocket object that could be corrupt? I mean why is it that this only happens after refreshing the PHP page so many times? What makes it corrupt like this?