0

I am currently making an IRC bot in C++, and i am getting an error from trying to bind the socket, error 10049: WSAEADDRNOTAVAIL. I am new to sockets in C++, so I was wondering what I did wrong. I am running Windows Vista Home Premium and compiling with Bloodshed Dev-C++, here is my code:

#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

char* buffer;
char* server = "irc.scrapirc.com";
int   port =   6667;
char* nick =   "r-bot";
char* user =   "r-bot irc.rbrtdllnbck.com RB :r-bot";
char* pass =   "";
char* email =  "admin@rbrtdllnbck.com";
char* chan =   "#!@#";
char* about =  "";
char* deny =   "NO!";
char** ex;
char* get_host(char* serv);
int iResult;
bool ajok = true;
bool running = true;
WSADATA wsaData;
DWORD dwError;
struct hostent *remoteHost;
struct in_addr addr;
SOCKET s;

void pause()
{
     system ("PAUSE");
}

char** explode ( char* topic, char* del  )
{
     int current = 1;
     char* tok;
     char** output;
     tok = strtok ( topic, del );
     output[0] = tok;
     while ( tok != NULL )
     {
          tok = strtok ( NULL, del );
          output[current] = tok;
          current++;
     }
}

void start_socket()
{
     iResult = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
     if (iResult != 0) 
     {
         printf("Error at WSAStartup(): %ld\n", WSAGetLastError());
         pause();
         WSACleanup ();
         exit ( 1 );
     }
     s = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );
     if ( s == INVALID_SOCKET )
     {
         printf("Error at socket(): %ld\n", WSAGetLastError());
         pause();
         WSACleanup();
         exit ( 2 );
     }
     sockaddr_in service;
     service.sin_family = AF_INET;
     service.sin_addr.s_addr = inet_addr(get_host(server));
     service.sin_port = htons(port);
     if (bind( s, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR)
     {
         printf("Error at bind(): %ld\n", WSAGetLastError());
         pause();
         closesocket( s );
         WSACleanup();
         exit ( 3 );
     }
}

char* get_host (char* serv)
{
     remoteHost = gethostbyname(serv);
     addr.s_addr = *(u_long *) remoteHost->h_addr_list[0];
     return inet_ntoa(addr);
}

void msg ( char* msg )
{
     send ( s, strcat ( msg, "\r\n" ), sizeof ( msg ), 0 );
}

void pm ( char* recip, char* say )
{
     msg ( strcat ( strcat ( strcat ( "PRIVMSG ", recip ), " :" ), say ) );
}

char* get()
{
     char* output;
     recv ( s, output, 2048, 0 );
     return output;
}

int main ()
{
    start_socket();
    connect ( s, (SOCKADDR*) &addr, sizeof ( addr ) );
    if ( pass != "" )
    {
         msg ( strcat ( "PASS ", pass ) );
    }
    msg ( strcat ( "NICK ", nick ) );
    msg ( strcat ( "USER ", user ) );
    msg ( strcat ( "JOIN ", chan ) );
    pm ( chan, "Hello" );
    while ( running )
    {
          buffer = get();
          cout << buffer;
          ex = explode ( buffer, " " );
          
          
    }
    WSACleanup();
    return 0;
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Salem
0

How about beginning with learning about memory allocation, and use of pointers?

explode(), pm(), msg() and get() are all a disaster area.

Eg.
output[0] = tok;
where does this write tok?
You didn't allocate any space for it.

> msg ( strcat ( "PASS ", pass ) );
PASS is a string constant, so modifying it is not allowed.

I could go on, but the errors are systemic.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.