View Single Post
Old Mar 6th, 2008, 5:34 AM   #13
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
Re: Simple BSD Sockets Problem

Quote:
Originally Posted by Sane View Post
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.

c Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <netdb.h>
  7. #include <sys/types.h>
  8. #include <netinet/in.h>
  9. #include <sys/socket.h>
  10.  
  11. #define PORT 6667 // the port client will be connecting to
  12.  
  13. #define MAXDATASIZE 2000 // max number of bytes we can get at once
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17. int sockfd, numbytes;
  18. char buf[MAXDATASIZE];
  19. struct hostent *he;
  20. struct sockaddr_in their_addr; // connector's address information
  21.  
  22. he = gethostbyname("efnet.teleglobe.net");
  23. sockfd = socket(PF_INET, SOCK_STREAM, 0);
  24. if (sockfd < 0) {
  25. printf("error creating the socket\n");
  26. }
  27. their_addr.sin_family = AF_INET;
  28. their_addr.sin_port = htons(PORT);
  29. their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  30. memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
  31.  
  32.  
  33. if (connect(sockfd, (struct sockaddr *)&their_addr,
  34. sizeof their_addr) == -1) {
  35. perror("connect\n");
  36. exit(1);
  37. }
  38. else {
  39. printf("You got the connection to the server!\n");
  40.  
  41. fprintf(stderr,"After send, entering recv loop\n");
  42. numbytes = 0;
  43. do {
  44. numbytes = 0;
  45. memset(buf, 0, sizeof(buf));
  46. //fprintf(stderr,"In recv loop\n");
  47. numbytes=recv(sockfd, buf, sizeof(buf)-1, 0); //-1 b/c you will null terminate
  48. buf[numbytes] = '\0';
  49. printf("Received: %s",buf);
  50. } while (numbytes != 0);
  51.  
  52. //char command[] = "PING\r\n";
  53. char nickCommand[] = "NICK MyWayCoolBotNick\r\n";
  54. char userCommand[] = "USER ObjcBot 8 * :The Objective-C Bot\r\n";
  55. //system("telnet efnet.teleglobe.net 6667");
  56.  
  57. //send(sockfd, command, sizeof(command), 0);
  58. send(sockfd, nickCommand, sizeof(nickCommand), 0);
  59. send(sockfd, userCommand, sizeof(userCommand), 0);
  60.  
  61. printf("exited first loop!!!\n");
  62. do {
  63.  
  64. numbytes = 0;
  65. memset(buf, 0, sizeof(buf));
  66. //fprintf(stderr,"In recv loop\n");
  67. numbytes=recv(sockfd, buf, sizeof(buf)-1, 0); //-1 b/c you will null terminate
  68. buf[numbytes] = '\0';
  69. printf("Received: %s",buf);
  70. } while (numbytes !=0);
  71.  
  72.  
  73.  
  74. fflush(stdout);
  75. close(sockfd);
  76. }
  77. return 0;
  78. }

But the problem is that it is as if those last 2 commands were never sent. Any ideas?
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote