Thread: Connecting to IRC server and joing a #channel

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    1

    Connecting to IRC server and joing a #channel

    I am trying to write a small plugin for a game server. It should allow the game server to effectively join an irc server, and then a channel. It's joining the server, and responding to ping after setting it's own nick, but after I receive all the junk messages the irc server wants to send, it seems to freeze up on JOIN, or rather, when it tries to receive the response back from the irc server, and it doesn't join the channel.

    Can anyone offer some help? I am a real IRC newbie tbh...


    Code:
    DWORD WINAPI MainThread( LPVOID lpParam ){
    
    char buf1[4096];
    char nick[] = "rebot";
    char text1[4096];
    int n;
    
     
    WORD wsver=MAKEWORD(2, 0);
    
    int nret=WSAStartup(wsver, &wsaData);
    if(nret != 0){
    	printf("Startup failed, error code: %d\n",WSAGetLastError());
    	WSACleanup();
    	return false;
    }
    
    printf("Init success\n");
    SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
    
    
    if(kSock == INVALID_SOCKET){
    	printf("Socket init failed");
    	return false;
    }
    
    printf("Socket initialized\n");
    
    sockaddr_in sin;
    
    sin.sin_port=htons(6668);
    
    sin.sin_addr.s_addr=inet_addr("85.25.143.169");
    
    sin.sin_family=AF_INET;
    
    if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){
    	printf("Startup failed, error code: %d\n",WSAGetLastError());
    	WSACleanup();
    	return false;
    }
    printf("Connection successful!\n\n");
    
    n = recv(kSock, buf1, 4096, 0);
    if ( n > 0 ) {
    	printf(">>Server: %s\n",buf1);
    	}
    else {
    	printf(">>Server: No Data\n");
    }
    
    
    
    sprintf(text1, "NICK rebot2\r\nUSER rebot2 0 0 :rebot2\r\n");
    send(kSock, text1, strlen(text1), 0);
    printf(">>Client: %s\n",text1);
    
    n = recv(kSock, buf1, 4096, 0);
    if ( n > 0 ) {
    	printf(">>Server: %s\n",buf1);
    	if (strstr(buf1,"PING")){
    		printf("I got a ping, cool!\n");
    		char* myStringPtr = buf1;
    		myStringPtr+=6;
    				char * pch;
    		pch = strtok (myStringPtr,"	 =\n");
    		std::vector<std::string> str_Vector;
    		while (pch != NULL){
    		std::string strData = pch;
    		str_Vector.push_back(strData);
    		pch = strtok (NULL, "	 =\n");
    		}
    		sprintf(text1,"PONG :%s\r\n",str_Vector.at(0).c_str());
    		send(kSock, text1, sizeof(text1), 0);
    		printf(">>Client: %s\n",text1);
    	}
    	}
    else {
    	printf(">>Server: No Data\n");
    }
    
    n = recv(kSock, buf1, 4096, 0);
    if ( n > 0 ) {
    	printf(">>Server: %s\n",buf1);
    	}
    else {
    	printf(">>Server: No Data\n");
    }
    
    
    n = recv(kSock, buf1, 4096, 0);
    if ( n > 0 ) {
    	printf(">>Server: %s\n",buf1);
    	}
    else {
    	printf(">>Server: No Data\n");
    }
    
    n = recv(kSock, buf1, 4096, 0);
    if ( n > 0 ) {
    	printf(">>Server: %s\n",buf1);
    	}
    else {
    	printf(">>Server: No Data\n");
    }
    
    n = recv(kSock, buf1, 4096, 0);
    if ( n > 0 ) {
    	printf(">>Server: %s\n",buf1);
    	}
    else {
    	printf(">>Server: No Data\n");
    }
    
    
    
    sprintf(text1,"JOIN #lobby\r\n");
    send(kSock, text1, sizeof(text1), 0);
    printf(">>Client: %s\n",text1);
    n = recv(kSock, buf1, 4096, 0);
    if ( n > 0 ) {
    	printf(">>Server: %s\n",buf1);
    	}
    else {
    	printf(">>Server: No Data\n");
    }
    
    
    
    /*
    sprintf(text1,"PRIVMSG #lobby :Test Message\r\n");
    send(kSock,text1,sizeof(text1),0);
    printf(">>Client: %s\n",text1);
    
    n = recv(kSock, buf1, 4096, 0);
    if ( n > 0 ) {
    	printf(">>Server: %s\n",buf1);
    	}
    else {
    	printf(">>Server: No Data\n");
    }
    
    
    
    */
    /*
     // ping and pong, will obviously be moved...
    while (1) {
    recv( kSock,buf,255,0);
    if (strstr(buf,"PING")) {
    	printf("Server sent PING\n");
    	send(kSock,"PONG :\r\n",128,0);
    	printf("Replying with PONG\n");
    }
    else{
    	printf(">>Server: %s\n",buf);
    }
    }
    */
    return 1;
    }

    http://www.game-maps.net/staff/reborn/rebot2.JPG

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    RFC 1459

    Your code is not indented at all. Indent it and post it again.

    recv blocks by default. If you try receiving data when there isn't any, it just hangs, waiting for it.

    I've written a few ircbots in my day and the protocol is still rather clear in memory; when you get pinged, you need to PONG with the same data, not just no data or random data. Some servers might not care about this too much, but some will, so follow the RFC.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed