I'm trying to make an IRC bot in C++ (As a networking test) But I've got a bit of trouble.
Here's my code:
#include "CIRCCon.h"
using namespace std;
ENetHost * client;
ENetAddress address;
ENetEvent event;
ENetPeer *peer;
int cLen=0;
CIRCCon::CIRCCon()
{
if (enet_initialize () != 0)
{
fprintf (stderr, "An error occurred while initializing ENet.\n");
}
}
// Good Programmer don't comment this
CIRCCon::~CIRCCon(){ enet_deinitialize();}
void CIRCCon::connectServer(char* ip,int port)
{
client = enet_host_create (NULL /* create a client host */,
1 /* only allow 1 outgoing connection */,
57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);
if (client == NULL)
{
fprintf (stderr,
"An error occurred while trying to create an ENet client host.\n");
exit (EXIT_FAILURE);
}
/* Connect to some.server.net:1234. */
enet_address_set_host (& address, ip);
address.port = port;
/* Initiate the connection, allocating the two channels 0 and 1. */
peer = enet_host_connect (client, & address, 2);
if (peer == NULL)
{
fprintf (stderr,
"No available peers for initiating an ENet connection.\n");
exit (EXIT_FAILURE);
}
/* Wait up to 5 seconds for the connection attempt to succeed. */
if (enet_host_service (client, & event, 10000) > 0 &&
event.type == ENET_EVENT_TYPE_CONNECT)
{
printf("Connected to %s:%d\n",ip,port);
}
else
{
/* Either the 5 seconds are up or a disconnect event was */
/* received. Reset the peer in the event the 5 seconds */
/* had run out without any significant event. */
enet_peer_reset (peer);
printf("Connection to %s:%d failed\n",ip,port);
}
}
void CIRCCon::startServer(int port)
{
// Connect to a server.
/* con.sin_family = AF_INET;
con.sin_addr.s_addr = inet_addr( "0.0.0.0" );
con.sin_port = htons( port );
if ( bind( m_socket, (SOCKADDR*) &con, sizeof(con) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return;
}
// Listen on the socket.
if ( listen( m_socket, 1 ) == SOCKET_ERROR )
printf( "Error listening on socket.\n");
*/
}
void CIRCCon::waitForClient()
{
}
int CIRCCon::sendData(string data)
{
/* Create a reliable packet of size 7 containing "packet\0" */
ENetPacket * packet = enet_packet_create (data.c_str(),
strlen (data.c_str()) + 1,
ENET_PACKET_FLAG_RELIABLE);
/* Send the packet to the peer over channel id 0. */
/* One could also broadcast the packet by */
/* enet_host_broadcast (host, 0, packet); */
enet_peer_send (peer, 0, packet);
/* One could just use enet_host_service() instead. */
enet_host_flush (client);
printf("[SND] %s\n",data.c_str());
return 0;
}
void CIRCCon::ilisten()
{
ENetEvent event;
/* Wait up to 1000 milliseconds for an event. */
while (enet_host_service (client, & event, 1000) > 0)
{
switch (event.type)
{
case ENET_EVENT_TYPE_RECEIVE:
printf ("A packet of length %u containing %s was received from %s on channel %u.\n",
event.packet -> dataLength,
event.packet -> data,
event.peer -> data,
event.channelID);
/* Clean up the packet now that we're done using it. */
enet_packet_destroy (event.packet);
break;
}
}
}
int CIRCCon::recvData(char *recvbuf,int size)
{
return 0;
}
void CIRCCon::closeConnection()
{
enet_host_destroy(client);
}
And here's my init code:
void runclient(char *ip, char *fpath)
{
w.connectServer(ip,6667);
printf("Connected to server...\n");
w.sendData("PASS NOPASS\n\r");
w.sendData("NICK SBot\n\r");
w.sendData("USER SBot USING PHP IRC\n\r");
w.sendData("JOIN #test\n\r");
w.ilisten();
printf("Connection ended......\n");
}
I'm getting this output in my console:
Quote:Connection to 127.0.0.1:6667 failed
Connected to server...
[SND] PASS NOPASS
[SND] NICK SBot
[SND] USER SBot USING PHP IRC
[SND] JOIN #test
Connection ended......
Can anyone help? :|
Edit: make a scrolling box with [ source ] tags.
[Edited by - hplus0603 on August 12, 2008 2:16:25 AM]