Advertisement

How to post high scores?

Started by February 08, 2005 03:33 AM
0 comments, last by hplus0603 20 years ago
Hi everyone, I am trying to implement high score posting in a DLL I have created. I can get the following function to compile well enough, yet when I try to send the string (or more likely, on the receiving end), something does not work. I am using the HTTP POST method to do the sending. Here is the complete function. I hope someone is able to tell me what eventually to do in order to make it work. ---Function Code--- #include <cstring> #include <sstream> #include <windows.h> #include <winsock.h> using namespace std; bool PostScore(int Score, std::string Product, std::string PosterName, std::string ServerAddress) { WSADATA WSAData; if (WSAStartup(MAKEWORD(1,1), &WSAData)){ MessageBox(NULL, "Error: Could not initialise the WinSock library. Please obtain a new package.", "Error: Cannot Initialise WinSock!", MB_ICONERROR); return false; } SOCKET MySocket; // The Socket. IN_ADDR Host; // Server LPHOSTENT HostEntry; SOCKADDR_IN Server; Host.s_addr = inet_addr(ServerAddress.c_str()); if (Host.s_addr == 0) { MessageBox(NULL, "Programmer's Error: The Server Address could not be resolved or you do not have a connection to the internet. Make sure that you are connected. If the problem persists, contact the developer.", "Programmer's Error: Address cannot be resolved.", MB_ICONERROR); WSACleanup(); return false; } HostEntry = gethostbyname(ServerAddress.c_str()); if (!HostEntry) { MessageBox(NULL, "Programmer's Error: The Server Address could not be resolved or you do not have a connection to the internet. Make sure that you are connected. If the problem persists, contact the developer.", "Programmer's Error: Address cannot be resolved.", MB_ICONERROR); WSACleanup(); return false; } Server.sin_port = htons(80); Server.sin_family = AF_INET; Server.sin_addr = *((LPIN_ADDR)*HostEntry->h_addr_list); MySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (!MySocket) { MessageBox(NULL, "Could not create the socket used for sending the high score. This could happen if you are running many programs at once that need to use the internet. Please close down some of the programs and try again.", "Could not create socket!", MB_ICONERROR|MB_OK); WSACleanup(); return false; } int ConnectStatus = connect(MySocket, (LPSOCKADDR)&Server, sizeof(SOCKADDR_IN)); if (ConnectStatus == SOCKET_ERROR) { MessageBox(NULL, "The program could not connect to the server. If you are behind a firewall, please allow the program to connect to port 80 and then retry.", "Cannot Connect.", MB_ICONERROR|MB_OK); closesocket(MySocket); WSACleanup(); return 0; } ostringstream SendInfo; SendInfo << "POST /PostScore.php HTTP/1.1\r\nCache-Control: no-cache\r\nUser-Agent: TPScorePoster/1.1\r\nHost: "; SendInfo << ServerAddress; SendInfo << "\r\nContent-Type: application-www-form-urlencoded\r\nContent-Length: "; ostringstream Body; Body << "Product="; Body << Product; Body << "&Name="; Body << PosterName; Body << "&Score="; Body << Score; string Body2; Body2 = Body.str(); SendInfo << Body2.length() << "\r\n\r\n"; SendInfo << Body2; string SendString = SendInfo.str(); ofstream fout("HTML.txt"); fout << SendString << endl << "Received:" << endl; send(MySocket, SendString.c_str(), SendString.length(), 0); char Buffer[256]; while (1) { ConnectStatus = recv(MySocket, Buffer, sizeof(Buffer), 0); fout << Buffer << endl; if (strstr("<title>", Buffer)) { MessageBox(NULL, "Score Posted Successfully.", "Success!", MB_OK); closesocket(MySocket); WSACleanup(); return true; } else { MessageBox(NULL, "Score may be posted.", "Score Posted!", 0); break; } if (ConnectStatus == 0) break; } closesocket(MySocket); WSACleanup(); return true; } Please help me (or point me to other resources, preferably free ones, that work with C++) Yours sincerely, Peter. To check out the product I develop currently, check out http://www.tdlsoftware.com
Of the wanderings, little was said in the ages to come. Basically, he was forgotten save by those who knew him.
Quote:
something does not work


You need to be a little more precise. When you step through your program, what functions does it run? What are the return values? Does it hang somewhere? Does it not get you all the data?

On the receiving side, how are you debugging it? Are you using a debugger? Do you intercept the request and trace it through the server? Are you looking in the server error and request logs to see what it thinks it's getting?

Quote:
application-www-form-urlencoded


I believe the content-type is supposed to be "application/www-form-urlencoded".
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement