I have this code, and I have this problems: Error 1 error C2110: '+' : cannot add two pointers 2 IntelliSense: expression must have integral or unscoped enum type
I am trying to GET data to the test.php file, with the m_AccountID variable data, I tryed many things, but I got this two errors.
Any idea to fix it?:)
void ConnectEx::SendDataPHP(){
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
//cout << "WSAStartup failed.\n";
//system("pause");
return;
}
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname("www.mysite.com");
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
//cout << "Connecting...\n";
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
//cout << "Could not connect";
//system("pause");
return;
}
//cout << "Connected.\n";
send(Socket, "GET /api/test.php?username=" +m_AccountID+ "HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n", strlen("GET /api/test.php?username="+m_AccountID+ "HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n"), 0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
//cout << buffer[i];
i += 1;
}
}
closesocket(Socket);
WSACleanup();
//system("pause");
return;
}
class ConnectEx
{
public:
void SendDataPHP();
.
.
.
.
char m_AccountID[11];
.
.
.
}; extern ConnectEx gConnectEx;
Just concatenate it to the string:
send( Socket ,
std::string( "GET /api/test.php?username=" + m_AccountID + " HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n" ).c_str( ) ,
strlen("GET /api/test.php?username=" + m_AccountID + " HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n") , 0);
Also if you just repeat the string afterwards, save it to a variable first so you can reuse that. And i wouldn't recommend using std::strlen
since it's old.
Rather do it like this:
std::string m_AccountID;
std::string data;
m_AccountID = "12345";
data = "GET /api/test.php?username=" + m_AccountID + " HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n"
send( Socket , data.c_str( ) , data.size( ) , 0 );
char m_AccountID[11];
should be std::string m_AccountID;
Your code should be looking like this now:
void ConnectEx::SendDataPHP(){
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
//cout << "WSAStartup failed.\n";
//system("pause");
return;
}
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname("www.mysite.com");
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
//cout << "Connecting...\n";
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
//cout << "Could not connect";
//system("pause");
return;
}
//cout << "Connected.\n";
send(Socket, std::string( "GET /api/test.php?username=" +m_AccountID+ "HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n" ).c_str( ) , strlen("GET /api/test.php?username="+m_AccountID+ "HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n"), 0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
//cout << buffer[i];
i += 1;
}
}
closesocket(Socket);
WSACleanup();
//system("pause");
return;
}
class ConnectEx
{
public:
void SendDataPHP();
.
.
.
.
std::string m_AccountID;
.
.
.
}; extern ConnectEx gConnectEx;
Make sure you also #include <string>
Now I made it, I used strcat to merge the variable than I put this into the send and its working. Thanks for the help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.