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 61
|
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#pragma comment (lib, "Ws2_32.lib")
#include <iostream>
#include <string.h>
#include <sstream>
#include <WinSock2.h>
#include <WS2tcpip.h>
//#include "stdafx.h"
using namespace std;
int main()
{
WSAData wsaData;
WORD DllVersion = MAKEWORD(2, 1);
if (WSAStartup(DllVersion, &wsaData) != 0) {
cout << "Winsock Connection Failed!" << endl;
WSACleanup();
exit(1);
}
//string getInput = "";
SOCKADDR_IN addr;
int addrLen = sizeof(addr);
IN_ADDR ipvalue;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(8282);
addr.sin_family = AF_INET;
SOCKET connection = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (connect(connection, (SOCKADDR*)&addr, addrLen) == 0) {
cout << "Connected!" << endl;
string getInput = "GET /parameter/hd1/spk/volume%3f HTTP/1.1\r\nHost: 127.0.0.1:8282\r\nContent-Type: text/plain\r\n\r\n";
send(connection, getInput.c_str(), strlen(getInput.c_str()), 0);
cout << getInput << endl;
int resp_leng;
#define BUFFERSIZE 1024
char buffer[BUFFERSIZE];
std::string response;
response = "";
resp_leng = BUFFERSIZE;
while (resp_leng == BUFFERSIZE)
{
resp_leng = recv(connection, (char*)&buffer, BUFFERSIZE, 0);
if (resp_leng > 0)
response += std::string(buffer).substr(0, resp_leng);
}
closesocket(connection);
cout << response << endl;
getline(cin, getInput);
WSACleanup();
exit(0);
}
else {
cout << "Error Connecting to Host" << endl;
WSACleanup();
exit(1);
}
return 0;
}
|