Soulstorm
03-06-2008, 05:51 PM
Hello. This is my first post in these forums.
I am really getting frustrated about a problem I am having with bsd sockets. I am trying to write a simple command line application that simply connects to an IRC server. For this to happen, I enter these commands into the terminal (for windows users, this is the DOS command prompt, for OS X it's the terminal application).
telnet efnet.teleglobe.net
NICK MyWayCoolBotNick\r\n
USER ObjcBot 8 * :The Objective-C Bot\r\n
And this connects to the server. But this is done through terminal. How can I do this in C++ (or C)? I have built an application that connects to the server
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 6667 // the port client will be connecting to
#define MAXDATASIZE 2000 // max number of bytes we can get at once
int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; // connector's address information
he = gethostbyname("efnet.teleglobe.net");
sockfd = socket(PF_INET, SOCK_STREAM, 0);
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 {
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); //-1 b/c you will null terminate
buf[numbytes] = '\0';
printf("Received: %s",buf);
} while (numbytes != 0);
//char command[] = "PING\r\n";
char nickCommand[] = "NICK MyWayCoolBotNick\r\n";
char userCommand[] = "USER ObjcBot 8 * :The Objective-C Bot\r\n";
//system("telnet efnet.teleglobe.net 6667");
//send(sockfd, command, sizeof(command), 0);
send(sockfd, nickCommand, sizeof(nickCommand), 0);
send(sockfd, userCommand, sizeof(userCommand), 0);
printf("exited first loop!!!\n");
do {
numbytes = 0;
memset(buf, 0, sizeof(buf));
//fprintf(stderr,"In recv loop\n");
numbytes=recv(sockfd, buf, sizeof(buf)-1, 0); //-1 b/c you will null terminate
buf[numbytes] = '\0';
printf("Received: %s",buf);
} while (numbytes !=0);
fflush(stdout);
close(sockfd);
}
return 0;
}
However, although I manage to connect to the server, the commands NICK and USER are not being sent (or at least I make something wrong). This results in getting disconnected from the IRC server after a while, because these commands must be given in order to establish a permanent connection.
Can anyone help me?
I am really getting frustrated about a problem I am having with bsd sockets. I am trying to write a simple command line application that simply connects to an IRC server. For this to happen, I enter these commands into the terminal (for windows users, this is the DOS command prompt, for OS X it's the terminal application).
telnet efnet.teleglobe.net
NICK MyWayCoolBotNick\r\n
USER ObjcBot 8 * :The Objective-C Bot\r\n
And this connects to the server. But this is done through terminal. How can I do this in C++ (or C)? I have built an application that connects to the server
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 6667 // the port client will be connecting to
#define MAXDATASIZE 2000 // max number of bytes we can get at once
int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; // connector's address information
he = gethostbyname("efnet.teleglobe.net");
sockfd = socket(PF_INET, SOCK_STREAM, 0);
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 {
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); //-1 b/c you will null terminate
buf[numbytes] = '\0';
printf("Received: %s",buf);
} while (numbytes != 0);
//char command[] = "PING\r\n";
char nickCommand[] = "NICK MyWayCoolBotNick\r\n";
char userCommand[] = "USER ObjcBot 8 * :The Objective-C Bot\r\n";
//system("telnet efnet.teleglobe.net 6667");
//send(sockfd, command, sizeof(command), 0);
send(sockfd, nickCommand, sizeof(nickCommand), 0);
send(sockfd, userCommand, sizeof(userCommand), 0);
printf("exited first loop!!!\n");
do {
numbytes = 0;
memset(buf, 0, sizeof(buf));
//fprintf(stderr,"In recv loop\n");
numbytes=recv(sockfd, buf, sizeof(buf)-1, 0); //-1 b/c you will null terminate
buf[numbytes] = '\0';
printf("Received: %s",buf);
} while (numbytes !=0);
fflush(stdout);
close(sockfd);
}
return 0;
}
However, although I manage to connect to the server, the commands NICK and USER are not being sent (or at least I make something wrong). This results in getting disconnected from the IRC server after a while, because these commands must be given in order to establish a permanent connection.
Can anyone help me?