Skip to content

Instantly share code, notes, and snippets.

@kylemsguy
Created May 29, 2014 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylemsguy/0af5b29fb5b704c34046 to your computer and use it in GitHub Desktop.
Save kylemsguy/0af5b29fb5b704c34046 to your computer and use it in GitHub Desktop.
/*
The MIT License (MIT)
Copyright (c) 2014 Kyle Zhou
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <winsock.h>
#include <cstdlib>
#include <iostream>
#include <vector>
typedef struct command{
std::string param;
std::string value;
} COMMAND;
SOCKET s; // the socket handle
bool connectToHost(int portNo, char *IPAddress){
// start up WINSOCK v2
WSADATA wsadata;
int error = WSASTARTUP(0x0202, &wsadata);
if(error){
return false;
}
if(wssadata.wVersion != 0x0202){
WSACleanup(); // cleanup what has happened
return false;
}
SOCKADDR_IN target; // socket address info
target.sin_family = AF_INET;
target.sin_port = htons(portNo);
target.sin_addr.s_addr = inet_addr(IPADdress);
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // create the socket
if(s == INVALID_SOCKET){
return false; // socket creation failed
}
// let's try connecting...
if(connect(s, (SOCKADDR *) &target, sizeof(target)) == SOCKET_ERROR){
return false; // couldn't connect
} else {
return true; // Success!
}
}
void closeConnection(){
if(s)
closeSocket(s);
WSACleanup(); //clean up winsock
}
std::string sendCommand(bool get, std::string id, std::vector<COMMAND> &params){
// get if get true else set
// doing a very hacky way of making XML command; you <s>will</s> will want to use a proper parser
string commendToSend;
commandToSend.append('<'); // command is "<"
if(get){
commandToSend.append("GET ID=\""); // command is <GET ID="id
} else {
commandToSend.append("SET ID=\""); // command is <SET ID="id
}
commandToSend.append(id); // command is <SET ID="id
commandToSend.append("\" "); // command is <SET ID="id"
for(std::vector<COMMAND>::iterator it = params.begin(); it != params.end; ++it){
COMMAND cmd = *it;
commandToSend.append(cmd.param); // command is <SET ID="id...param
commandToSend.append("=\""); // command is <SET ID="id...param="
commandToSend.append(cmd.value); // command is <SET ID="id...param="value
commandToSend.append("\" "); // command is <SET ID="id...param"
}
commandToSend.append("/>");
// now send the string; simulating for debug
std::cout << commandToSend << std::endl;
std::string toReturn = "";
if(s){
// actual send string
s.send(commandToSend.c_str(), commandToSend.length());
} else {
return NULL;
}
// now listen for reply
return toReturn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment