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...
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:
it works! Can anyone enlighten me?
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?