Thread: irc socket code

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    irc socket code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <winsock2.h>
    
    char Heap[1000];
    
    int main(int argc, char *argv[])
    {
        int d;
        SOCKET s;
        struct sockaddr_in A;
        WSADATA ws;
        char Buff[1000];
        char aA[13] = "NICK Evander\n";
        char aB[53] = "USER none \"none.com\" \"irc.expression-chat.net\" :none\n";
        char aC[4] = "PING";
        char aD[28] = "PONG :........ing peice of ........\n";
        char aE[12] = "JOIN #help\n";
        char aG[17] = "USERHOST Evander\n";
        
        
            d=WSAStartup(0x101,&ws);
    		s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    		A.sin_family=AF_INET;
    		A.sin_port=htons(6667);
    		A.sin_addr.s_addr= inet_addr("208.53.146.211");
    		d=connect(s,(struct sockaddr*)&A,sizeof(A));
    		
    		send(s,aA,sizeof(aA),0);
    		recv(s, Heap, sizeof(Heap), 0);
    		printf("Send: %s", aA);
    		
    		send(s,aB,sizeof(aB),0);
    		recv(s, Heap, sizeof(Heap), 0);
    		printf("Send: %s",aB);
    		
    		//send(s,aD,sizeof(aD),0);
    		//recv(s, Heap, sizeof(Heap), 0);
    		//printf("Send: %s",aD);
    		
    		send(s,aE,sizeof(aE),0);
    		recv(s, Heap, sizeof(Heap), 0);
    		printf("Send: %s",aE);
    		
    		//send(s,aG,sizeof(aG),0);
    		//recv(s, Heap, sizeof(Heap), 0); //this is here for some reason
    		
    		int E = 1;
            int B;
            while (E != 0)
            {
             E = recv(s, Heap, sizeof(Heap), 0);
             if (E >= 0)
             { 
    		printf("Recv %s\n", Heap);
    		B = memcmp(Heap, aC, 4);
    		 if (B == 0)
    		  {
                   send(s,aD,sizeof(aD),0);
                   printf("Send %s", aD);
                   HeapClear();             
                                      }
              else HeapClear();
             }
            }
      
      system("PAUSE");
      closesocket(s);
      WSACleanup();	
      return 0;
    }
    int HeapClear()
    {
       int i;
       
       for(i=0; i <= 1000; ++i)
       {
       Heap[i] = 0;
       }
       i = 0;
    }

    current version is stable,

    to implement:

    Thread for command input
    Rethink of HeapClear (using memset)
    Possable switch to using windows api


    Help needed in:
    Malloc packet handling in memory using pointers
    Packet construction ideas
    sizeof handling
    user input constraints
    incomming packet parsing
    Last edited by lost_in_logic; 08-16-2005 at 02:35 AM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Eep.

    Code:
    char aC[4] = "PING";
    ohmygosh it's a buffer overflow! Or a compilation error. Just don't specify the size of it if you don't need to. char aC[] = "PING"; Compiler will figure out things nicely for you. Also, try giving things more meaningful variable names (hypocritical comment). I mean with everything having to be initialized at the top, you'll get lost once you scroll down in your code. So about this Heap variable. I don't know, I think you're throwing around buzzwords, but if you really want your data on the heap, you can do the old DMA and:

    Code:
    char* recvBuf = malloc(1024 * sizeof(char));
    Remembering to free() it at the end. You also might rearrange your loop structure to look like:

    Code:
    while(iRet = recv(s, recvBuf, 1024, 0)) { bleh(); }
    And instead of using a function like memcmp, you could just NULL terminate the recv'd string:

    Code:
    recvBuf[iRet] = 0;
    And you can use regular string functions, this would ease your uh, "incomming packet parsing".
    memset or ZeroMemory or whatever stupid function you'd like could be implemented with a quick:

    Code:
    memset(str, 0, 1024);
    And I really don't know what the hell you mean by Packet construction ideas, sizeof handling, and user input constraints. Maybe I'm just not too familiar with the grand IRC. To switch to the win32 API, you could just use standard winsock2 functions and you could try Async operations, handling messages, and maybe making a small GUI. Making a thread for "command input" seems somewhat unnecessary and costly, unless you are really doing some crazy stuff with what people are feeding your bot. Check out this small example on how to use threads.

    Anyways, feel free to elaborate on your problem? I'm just a bit clueless
    Last edited by Tonto; 08-16-2005 at 03:06 AM.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    2
    Rather harshing on me arent you, you must relise of course im just getting passed my newbie tutorials on C.. i notice serious problems with my code thus is why i came to this fourm, if you somehow get off by flaming people trying to learn then you probably should not respond to my posts.

    however my meaning to packet construction is placing 4 byte intervals to construct packets, if you notice the protocol is made up of 4 byte strings PING, PONG, NICK, etc. im looking for a method of using pointers through malloc (yes i know my strings are screwed thus is why im looking for alternate methods of dealing with that without screwing up my packets with tons of null charictors) for instance i wanna talk in a room PRIVMSG would be sent then 0x20 (space) then the room name followed by a space, and the message. i dont quite know how to achive this yet cause im still new to dealing with memory. any pointers (aside from flaming) are welcome.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Be right back, I'm going to get myself off on "flaming" you. God I love it.

    Anyways, sorry for being a big meanie. I just don't know the IRC protocol too well, as previously stated. I still don't see the significance of the number 4, all the command do seem to be 4 char's long, but I think that rather than hack'ishly assuming that, we should probably use "safer" regular string handling functions like: strtok(), strcmp(), and strstr(), they just seem more natural. As I said in my big fiery post, you should null terminate your recv'd data (even though it is continuously zero'd and initialized zero'd I think). These such things would be vey helpful at parsing whatever data the crazy people in the IRC throw at your bot. To PRIVMSG your chat room, you might use sprintf() to format your send()'t string to the IRC server properly. It works much like printf() but instead of printing to stdout, it "prints" to a specified memory buffer (which you could malloc() if you wanted!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with multithreaded sockets program
    By ceclauson in forum C Programming
    Replies: 3
    Last Post: 12-13-2008, 04:15 PM
  2. Shouldn't this code work?
    By DeepFyre in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2004, 01:24 PM
  3. Socket Programming Introduction Source Code
    By Lynux-Penguin in forum Linux Programming
    Replies: 2
    Last Post: 04-30-2002, 05:29 PM
  4. Replies: 4
    Last Post: 01-16-2002, 12:04 AM
  5. Code for Client/Server Socket
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2002, 09:30 AM