1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
int main(int argc, char *argv[])
{
string request;
string response = "";
int resp_leng;
char buffer[BUFFERSIZE];
struct sockaddr_in serveraddr;
int sock;
WSADATA wsaData;
char *ipaddress = "173.203.216.184";
int port = 80;
//This sets up the request function as Mace required. The 'Authorization' is our user name and password converted to Basic64
request="GET /Date of the Data I need\r\n";
request+="Host: Server ID\r\n";
request+="Authorization: Basic My Password (not telling)\r\n";
//init winsock
WSAStartup(MAKEWORD(1, 0), &wsaData);;
//open socket
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
//connect
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(ipaddress);
serveraddr.sin_port = htons((unsigned short) port);
connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr));
//send request
send(sock, request.c_str(), request.length(), 0);
//get response
resp_leng = BUFFERSIZE;
//MAKE THIS LINE WORK!
//setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&n,sizeof(int));
while (resp_leng == BUFFERSIZE)
{
resp_leng = recv(sock, (char*)&buffer, BUFFERSIZE, 0); //Here is the error, unless the mace server isn't recieving the request
if (resp_leng>0)
response += string(buffer).substr(0,resp_leng);
//note: download lag is not handled in this code
}
system("pause");
//display response
cout << response << endl;
printf("/nResponse Is:%s", response);
system("pause");
//disconnect
closesocket(sock);
//cleanup
WSACleanup();
return 0;
}
|