C++ console irc client not connecting

These are old archives. They are kept for historic purposes only.
Post Reply
iarkey

C++ console irc client not connecting

Post by iarkey »

I'm coding a channel bot in C++, This is what I have so far. it connect to server but doesn't join channel instead says "451 you have not registered"
any ideas what I'm doing wrong?

Code: Select all

   #pragma comment(lib, "wsock32.lib ")
   #include <windows.h>
   #include <iostream>
   #include <stdio.h>
   #include <string.h>

      int main(){
      char buf1[1200];
	  char nick[] = "iarkeytest";
      char text1[4096];
	  char buf[4096];
	  int n;

      WSAData wsdata;
      WORD wsver=MAKEWORD(2, 0);
      int nret=WSAStartup(wsver, &wsdata);
      if(nret != 0){
      std::cout<<"Startup failed, error code: "<<WSAGetLastError();
      WSACleanup();
      return -1;
      }
       
      printf("Init success\n");
      SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
      if(kSock == INVALID_SOCKET){
      printf("Socket init failed");
      return -1;
      }
       
      printf("Socket initialized\n");
      sockaddr_in sin;
      sin.sin_port=htons(6667);
      sin.sin_addr.s_addr=inet_addr("127.0.0.1");
      sin.sin_family=AF_INET;
      if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){
      std::cout<<"Connect failed, error: "<<WSAGetLastError();
      WSACleanup();
      return -1;
      }
       
      printf("Connection successful!\n\n");
     
	  n = recv(kSock, buf1, sizeof(buf1)-1, 0);
      if ( n > 0 ) {
      buf1[n] = '\0';
      printf("%s\n",buf1);
      } else {
		  printf("No Data\n");
      }

      memset(text1,0,255);
      sprintf(text1, "NICK %s\r\nUSER %s 0 * :%s\r\n\0", nick, nick, nick);
      send(kSock, text1, strlen(text1), 0);
      printf(">>Client: %s\n",text1);

	   Sleep(100);

      memset(text1,0,255);
	  sprintf(text1,"JOIN #lobby\r\n");
      send(kSock, text1, sizeof(text1), 0);
      printf(">>Client: %s\n",text1);

      memset(text1,0,255);
      sprintf(text1,"PRIVMSG #lobby :Test Message\r\n");
      send(kSock,text1,sizeof(text1),0);
      printf(">>Client: %s\n",text1);


   while (1) {
   memset(buf,0,255);
   recv( kSock,buf,255,0);
	
   if ( strstr(buf,"PING") != 0 ) {
   printf("Server sent PING\n");
   send(kSock,"PONG :\r\n",6,0);
   printf("Replying with PONG\n");
   }
	
 memset(buf,0,255);
   }
}
Stealth
Head of Support
Posts: 2085
Joined: Tue Jun 15, 2004 8:50 pm
Location: Chino Hills, CA, US
Contact:

Re: C++ console irc client not connecting

Post by Stealth »

Make sure you are sending NICK and USER properly (hint: make it output anything sent/received to a debug log).

Unrelated, but maybe important: Why do you have \0 at the end of USER? All IRC messages always end with \r\n, never \0
Stealth
Head of Support
Posts: 2085
Joined: Tue Jun 15, 2004 8:50 pm
Location: Chino Hills, CA, US
Contact:

Re: C++ console irc client not connecting

Post by Stealth »

Oh, and...

When you send PONG, you must include anything received with PING! If the server sends "PING :junk" you must reply "PONG :junk". Not sending the correct reply when connecting may be your problem as it is holding up the user registration process.

Also clean up your code. It is very hard to read, which makes it very hard to debug.

EDIT: Here, I took the liberty of properly tabbing your code. Looks a lot better, doesn't it?

Code: Select all

#pragma comment(lib, "wsock32.lib ")
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <string.h>

int main(){
    char buf1[1200];
    char nick[] = "iarkeytest";
    char text1[4096];
    char buf[4096];
    int n;

    WSAData wsdata;
    WORD wsver=MAKEWORD(2, 0);
    int nret=WSAStartup(wsver, &wsdata);
    if(nret != 0){
        std::cout<<"Startup failed, error code: "<<WSAGetLastError();
        WSACleanup();
        return -1;
    }
           
    printf("Init success\n");
    SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
    if(kSock == INVALID_SOCKET){
        printf("Socket init failed");
        return -1;
    }
           
    printf("Socket initialized\n");
    sockaddr_in sin;
    sin.sin_port=htons(6667);
    sin.sin_addr.s_addr=inet_addr("127.0.0.1");
    sin.sin_family=AF_INET;
    if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){
        std::cout<<"Connect failed, error: "<<WSAGetLastError();
        WSACleanup();
        return -1;
    }
           
    printf("Connection successful!\n\n");
         
    n = recv(kSock, buf1, sizeof(buf1)-1, 0);
    if ( n > 0 ) {
        buf1[n] = '\0';
        printf("%s\n",buf1);
    } else {
        printf("No Data\n");
    }

    memset(text1,0,255);
    sprintf(text1, "NICK %s\r\nUSER %s 0 * :%s\r\n\0", nick, nick, nick);
    send(kSock, text1, strlen(text1), 0);
    printf(">>Client: %s\n",text1);

    Sleep(100);

    memset(text1,0,255);
    sprintf(text1,"JOIN #lobby\r\n");
    send(kSock, text1, sizeof(text1), 0);
    printf(">>Client: %s\n",text1);

    memset(text1,0,255);
    sprintf(text1,"PRIVMSG #lobby :Test Message\r\n");
    send(kSock,text1,sizeof(text1),0);
    printf(">>Client: %s\n",text1);


    while (1) {
        memset(buf,0,255);
        recv( kSock,buf,255,0);
       
        if ( strstr(buf,"PING") != 0 ) {
            printf("Server sent PING\n");
            send(kSock,"PONG :\r\n",6,0);
            printf("Replying with PONG\n");
        }
       
        memset(buf,0,255);
    }
}
iarkey

Re: C++ console irc client not connecting

Post by iarkey »

Yeah, I can fix ping bit. But by logging it I receive the ping after the not registered error. I've removed the \0 but still no luck :shock:
Jobe
Official supporter
Posts: 1180
Joined: Wed May 03, 2006 7:09 pm
Location: United Kingdom

Re: C++ console irc client not connecting

Post by Jobe »

I would suggest you wait until you receive the numeric reply 005 before you send any command other then NICK, USER, PASS and PONG. That way, unless you receive 005, you know you have not registered as a user and wont then send anything that results in the "not registered" message.
Your IP: Image
Your Country: Image
iarkey

Re: C++ console irc client not connecting

Post by iarkey »

Ahh thanks guys, it was to do with not sending junk data after ping. It can now join a channel but PRIVMSG #channel :message doesn't do anything, no errors returned. If i type in on mirc /privmsg #channel #message nothing happens.
Post Reply