Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma warning(disable:4503)
- #include "Discord_Client.h"
- static const int MAX_LINE = 1024;
- int PORT;
- char *HOST;
- #ifdef _WIN32
- SOCKET sockfd;
- #else
- pthread_t thread;
- int sockfd;
- #endif
- Discord_Client::Discord_Client(int PORT, char *HOST) {
- this->PORT = PORT;
- this->HOST = HOST;
- }
- Discord_Client::~Discord_Client() {
- discord_disconnect();
- }
- void Discord_Client::discord_connect() {
- #ifdef _WIN32
- WSADATA wsa;
- if (WSAStartup(MAKEWORD(2, 0), &wsa) != 0) exit(1);
- #endif
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (static_cast<int>(sockfd) < 0) {
- perror("socket()");
- discord_disconnect();
- exit(1);
- }
- hostent *hp = gethostbyname(HOST);
- if (!hp) {
- cerr << "gethostbyname()" << endl;
- discord_disconnect();
- exit(1);
- }
- sockaddr_in sin;
- memset((char*)&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- memcpy((char*)&sin.sin_addr, hp->h_addr, hp->h_length);
- sin.sin_port = htons(PORT);
- memset(&(sin.sin_zero), 0, 8 * sizeof(char));
- if (connect(sockfd, (sockaddr*)&sin, sizeof(sin)) == -1) {
- perror("connect()");
- discord_disconnect();
- exit(1);
- }
- }
- void Discord_Client::discord_disconnect() {
- #ifdef _WIN32
- closesocket(sockfd);
- WSACleanup();
- #else
- close(sockfd);
- #endif
- }
- string Discord_Client::discord_get(string msg) {
- discord_send(("GET " + msg).c_str());
- char buffer[MAX_LINE + 1] = { 0 };
- if (recv(sockfd, buffer, MAX_LINE*sizeof(char), 0)<0) {
- cout << buffer;
- perror("recv()");
- discord_disconnect();
- exit(1);
- }
- cout << buffer;
- return (string)buffer;
- }
- void Discord_Client::discord_send(const char *msg) {
- send(sockfd, msg, strlen(msg), 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement