Everything else works fine, but for some reason, it only receives once. (note, the recv function starts at line 49) function is at
code:
/*
* File: Bot.cpp
* Author: fellixombc
*
* Created on May 20, 2010, 6:25 PM
*/
#include "bot.h"
/** Constructs the Bot
* @param set the username
* @param set the default channel
*/
Bot::Bot(string param, string param1) {
botname = param;
channel = param1;
}
/** Connects to desired server and port
* @param set the server
* @param set the server's port
*/
void Bot::Connect(char* server, int port) {
host = gethostbyname(server);
client = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = *((unsigned long*)host->h_addr);
if(connect(client, (sockaddr*)(&address), sizeof(address)) == -1) {
cout << "Error..." << endl;
Disconnect();
}
cout << "Connected!" << endl;
Send("NICK " + botname + "\r\n");
Send("User " + botname + " " + botname + "1 " + botname + "2 :C++ Irc Bot \r\n");
}
/** Constructs the Bot
* @param points the recv'd text to the input
*/
void Bot::Recv(string& buffer) {
while(size == 0) {
ioctl(client, FIONREAD, &size);
}
char* buf = new char[size+1];
bytes = recv(client, buf, size, 0);
cout << bytes << endl;
buffer = buf;
delete[] buf;
/*
if(buffer.find("/MOTD") != -1) {
Send("JOIN " + channel + " \r\n");
}*/
}
/** Sends a message to the server
*/
void Bot::Send(string msg) {
send(client, msg.c_str(), sizeof(msg), 0);
cout << msg;
}
/** Closes the socket
*/
void Bot::Disconnect() {
close(client);
cout << "Disconnected." << endl;
}
How it is being used:
/*
* File: main.cpp
* Author: Fellixombc
* Purpose: Socket example
*
* Created on May 18, 2010, 8:40 PM
*/
#include <iostream>
#include "bot.h"
using namespace std;
int main() {
string buffer;
Bot bot("Bob", "#bobbot");
bot.Connect("godirc.com", 6667);
while(1) {
bot.Recv(buffer);
cout << buffer;
}
return 0;
}
Any idea?