Sockets Problem (once again)

Discussion in 'Mac Programming' started by Soulstorm, Jul 7, 2008.

  1. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    My purpose is to establish an IRC connection with an IRC server. I am really trying to figure out why the following code does not work...

    Code:
    - (IBAction)OKButtonPressed:(id)sender
    {
    	NSLog(@"OK button Pressed!");
    	
    	struct hostent *he;
        struct sockaddr_in their_addr; // connector's address information
    	
    	char buf[MAXDATASIZE];
    	he = gethostbyname("efnet.teleglobe.net");
        sockfd = socket(PF_INET, SOCK_STREAM, 0);
    	
    	NSData *userStringData = [[userCommandField stringValue]dataUsingEncoding:NSUTF8StringEncoding];
    	NSData *nickStringData = [[userNickField stringValue]dataUsingEncoding:NSUTF8StringEncoding];
    	int userStringLength = [userStringData length];
    	int nickStringLength = [nickStringData length];
    	
    	const char *userCommandArg = [userStringData bytes];
    	const char *nickCommandArg = [nickStringData bytes];	
    	
    	printf("%s\n%s\n", userCommandArg,nickCommandArg);
    	if (sockfd < 0) {
    		printf("error creating the socket\n");
        }
    	
    	their_addr.sin_family       = AF_INET;
        their_addr.sin_port         = htons(PORT);
        their_addr.sin_addr         = *((struct in_addr *)he->h_addr);
        memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
    	
    	if (connect(sockfd, (struct sockaddr *)&their_addr,
    				sizeof their_addr) == -1) {
    		perror("connect\n");
    		exit(1);
        }
    	
    	else {
    		
    		int sent = send(sockfd, nickCommandArg, nickStringLength, 0);
    		if ( sent <= 0 ) {
    			NSLog(@"could not send anything!");
    		}
    		sent = send(sockfd, userCommandArg, userStringLength, 0);
    		if ( sent <= 0 ) {
    			NSLog(@"could not send anything!");
    		}
    		printf("You got the connection to the server!\n");
    		
    		fprintf(stderr,"After send, entering recv loop\n");
    		numbytes = 0;
    		
    		do {
    			numbytes = 0;
    			memset(buf, 0, sizeof(buf));
    			//fprintf(stderr,"In recv loop\n");
    			numbytes=recv(sockfd, buf, sizeof(buf)-1, 0); 
    			buf[numbytes] = '\0';
    			printf("Received: %s",buf);
    		} while (numbytes != 0);
    		fflush(stdout);
    	}
    }
    
    Although I manage to establish the connection to the server with the connect() command and accept the initial messages from the server, the following commands (the ones with the NICK and USER) are not sent properly! The most strange thing is that when I use the following command:

    Code:
    char allCommands[] = "USER ObjcBot 8 * :The Objective-C Bot\r\nNICK MyWayCoolBotNick\r\n";
    //send(sockfd, allCommands, sizeof(allCommands), 0);
    it works! Can anyone enlighten me?
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Which parts of your real source code send the \r\n ?
    Also, you realise that the "working" command sends a spurious zero byte at the end of the commands, which could upset further communications with the server?
     
  3. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #3
    Did you try and flush the socket descriptor?

    You might not be filling the buffer enough to force a send (you can also set TCP No Delay On). The server might be timing out waiting for a command and then it drops the connection.

    try doing an fflush(sockfd) after each send.
     
  4. Soulstorm thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #4
    I also tried sending the commands with the \r\n with no luck at all. Secondly, I also tried sending the commands by deleting the last byte (just using the send() function, but using the length-1 as second argument). No luck.

    That's a pretty good idea, actually. I will make another project and try that. I solved the problem by using SmallSockets. Surprisingly enough, when sending the bytes, SmallSockets uses NSData bytes, and they don't include the \r\n at the end.
     

Share This Page