|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 
|
Re: Simple BSD Sockets Problem
Quote:
Originally Posted by Sane
Usually that happens when you haven't sent the correct message, you haven't properly terminated the message, or you haven't sent a message when it was expecting one. My gut feeling is with the latter. During the time it's waiting, it could be waiting for you to send a valid reply or acknowledgement. I have no experience with interfacing to an IRC server, so I can't suggest anything specific.
Edit: I'm fairly confident with my assumption. NOTICE AUTH ** No Ident response. It seems as though it was waiting for a response. Presumably, imformation about your identity. But you don't appear to send it anything. Consequently, it waits a few seconds, and then assumes you timed out and closes the connection. That is my assumption.
|
You were right. It waited for me to give 2 commands, the NICK and USER commands. I tried connecting to efnet.teleglobe.net using telnet (on OS X terminal, which is the equivalent of the dos command prompt) and then I entered the NICK and USER commands.
telnet efnet.teleglobe.net 6667
NICK NICK MyWayCoolBotNick\r\n
USER ObjcBot 8 * :The Objective-C Bot\r\n
I manages to connect and receive information about the server.
However, I did that using the terminal. I don't know how can I send that using C. The only thing I manage to do is to open a telnet communication with the server with the connect() command. How can I send the other 2 commands? With the send() command? That's what I am trying in the code below.
#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; }
But the problem is that it is as if those last 2 commands were never sent. Any ideas?
|