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
|
//////////Irc.h/////////////
#ifndef __IRC__
#define __IRC__
#include <winsock2.h>
#include <string>
class irc
{
public:
void connectToHost(char *server, int port);
void sendMessage(const char *msg);
std::string receivedMessage();
private:
WSAData wsaData;
SOCKET connection;
sockaddr_in service;
bool connected;
};
#endif
////////////////irc.cpp///////////////
#include "irc.h"
#include <iostream>
void irc::connectToHost(char *server, int port)
{
LPHOSTENT host;
if(WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
return;
}
host = gethostbyname(server);
connection = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
service.sin_family = AF_INET;
service.sin_addr = *((LPIN_ADDR)*host->h_addr_list);
service.sin_port = htons(port);
if(connect(connection, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR) {
std::cout << "Unable to connect. error: " << WSAGetLastError() << std::endl;
system("pause");
return;
}
std::cout << "Connected!" << std::endl;
connected = true;
}
void irc::sendMessage(const char *msg)
{
if(connected) {
send(connection, msg, sizeof(msg), 0);
}
}
std::string irc::receivedMessage()
{
char msg[1024];
int bytes = recv(connection, msg, sizeof(msg), 0);
if(bytes > 0) {
return msg;
}
return "";
}
/////////////////client.cpp/////////////////
#include "irc.h"
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
system("title Irc Client");
irc* Irc = new irc;
Irc->connectToHost("multiplay.uk.quakenet.org", 6667);
Irc->sendMessage("USER F_Fallen * * :F_Fallen\r\n");
Irc->sendMessage("NICK F_Fallen\r\n");
while(true) {
string received = Irc->receivedMessage();
if(strncmp(received.c_str(), "PING", 4) == 0) {
char buffer[512];
for(int i = 0; i < received.length(); i++)
buffer[i] = received[i];
buffer[1] = 'O';
Irc->sendMessage(buffer);
cout << "Received ping!" << endl;
}
cout << received << endl;
}
cout << endl;
system("pause");
return 1;
}
|