Created
December 26, 2017 15:11
-
-
Save kaecy/25d060b41ffeb8b89d19f39dd1e38b82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string> | |
#include <winsock2.h> | |
#include <ws2tcpip.h> | |
using std::string; | |
class Sender { | |
char* address; | |
char* port; | |
public: | |
Sender(char* address, char* port) { | |
this->address = address; | |
this->port = port; | |
} | |
void send(char* message) | |
{ | |
ADDRINFO hints={0}, *result; | |
SOCKET client; | |
hints.ai_family = AF_UNSPEC; | |
hints.ai_socktype = SOCK_STREAM; | |
hints.ai_protocol = IPPROTO_TCP; | |
int addrinfoerror = getaddrinfo(address, port, &hints, &result); | |
if (addrinfoerror) | |
{ | |
printf("getaddrinfo returned error."); | |
return; | |
} | |
client = socket(result->ai_family, result->ai_socktype, result->ai_protocol); | |
int connectresult = connect(client, result->ai_addr, result->ai_addrlen); | |
if (connectresult == SOCKET_ERROR) | |
{ | |
printf("socket connect error."); | |
return; | |
} | |
::send(client, message, strlen(message), 0); | |
closesocket(client); | |
} | |
}; | |
int main(int argc, char* argv[]) { | |
WSAData wsData; | |
if (WSAStartup(MAKEWORD(2,0), &wsData)) | |
return 1; | |
Sender("127.0.0.1", "502").send("hello!"); | |
WSACleanup(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment