-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRCSocket.cpp
More file actions
130 lines (108 loc) · 3 KB
/
IRCSocket.cpp
File metadata and controls
130 lines (108 loc) · 3 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (C) 2011 Fredi Machado <https://github.com/fredimachado>
* IRCClient is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* http://www.gnu.org/licenses/lgpl.html
*/
#include <cstring>
#include <fcntl.h>
#include "IRCSocket.h"
#define MAXDATASIZE 4096
bool IRCSocket::Init()
{
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData))
{
std::cout << "Unable to initialize Winsock." << std::endl;
return false;
}
#endif
if ((_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
std::cout << "Socket error." << std::endl;
#ifdef _WIN32
WSACleanup();
#endif
return false;
}
int on = 1;
if (setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (char const*)&on, sizeof(on)) == -1)
{
std::cout << "Invalid socket." << std::endl;
#ifdef _WIN32
WSACleanup();
#endif
return false;
}
#ifdef _WIN32
u_long mode = 0;
ioctlsocket(_socket, FIONBIO, &mode);
#else
fcntl(_socket, F_SETFL, O_NONBLOCK);
fcntl(_socket, F_SETFL, O_ASYNC);
#endif
return true;
}
bool IRCSocket::Connect(char const* host, int port)
{
hostent* he;
if (!(he = gethostbyname(host)))
{
std::cout << "Could not resolve host: " << host << std::endl;
#ifdef _WIN32
WSACleanup();
#endif
return false;
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr = *((const in_addr*)he->h_addr);
memset(&(addr.sin_zero), '\0', 8);
if (connect(_socket, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR)
{
std::cout << "Could not connect to: " << host << std::endl;
closesocket(_socket);
return false;
}
_connected = true;
return true;
}
void IRCSocket::Disconnect()
{
if (_connected)
{
#ifdef _WIN32
shutdown(_socket, 2);
#endif
closesocket(_socket);
_connected = false;
}
}
bool IRCSocket::SendData(char const* data)
{
if (_connected)
if (send(_socket, data, strlen(data), 0) == -1)
return false;
return true;
}
std::string IRCSocket::ReceiveData()
{
char buffer[MAXDATASIZE];
memset(buffer, 0, MAXDATASIZE);
int bytes = recv(_socket, buffer, MAXDATASIZE - 1, 0);
if (bytes > 0)
return std::string(buffer);
else
Disconnect();
return "";
}