Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 4th, 2008, 10:04 AM   #11
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 234
Rep Power: 3 Soulstorm is on a distinguished road
Re: Simple BSD Sockets Problem

I managed to accept everything an IRC server has to say to me. This is my code:
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.  
  54. send(sockfd, command, sizeof(command), 0);
  55.  
  56. do {
  57. numbytes = 0;
  58. memset(buf, 0, sizeof(buf));
  59. //fprintf(stderr,"In recv loop\n");
  60. numbytes=recv(sockfd, buf, sizeof(buf)-1, 0); //-1 b/c you will null terminate
  61. buf[numbytes] = '\0';
  62. printf("Received: %s",buf);
  63. } while (numbytes !=0);
  64.  
  65.  
  66.  
  67. fflush(stdout);
  68. close(sockfd);
  69. }
  70. return 0;
  71. }

And this is What I get:
You got the connection to the server!
After send, entering recv loop
Received: NOTICE AUTH :*** Processing connection to efnet.teleglobe.net
Received: NOTICE AUTH :*** Looking up your hostname...
NOTICE AUTH :*** Checking Ident
NOTICE AUTH :*** Found your hostname
Received: NOTICE AUTH :*** No Ident response
Received: ERROR :Closing Link: 127.0.0.1 (Connection timed out)
Received: Received:

The error about "no ident response" comes up after waiting a few seconds. I am behind a router, but I have port forwarded the port 113 (which is used for ident) but with no effect whatsoever.
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Mar 4th, 2008, 12:27 PM   #12
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Simple BSD Sockets Problem

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.

Last edited by Sane; Mar 4th, 2008 at 12:38 PM.
Sane is offline   Reply With Quote
Old Mar 6th, 2008, 5:34 AM   #13
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 234
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
Old Mar 7th, 2008, 12:45 AM   #14
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Re: Simple BSD Sockets Problem

All telnet does is give you a direct TCP connection to the given server on the given port. You can then type out which characters you want to send and hit enter. It then reads back whatever it gets sent and writes it to screen.

What you really should do is write a both a client and a sever TCP program and run them together. Then you will get a firmer grasp of how to use send and recv properly. Also you aren't checking the returns of send, you should always check the return value.
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old Mar 7th, 2008, 2:54 AM   #15
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 234
Rep Power: 3 Soulstorm is on a distinguished road
Re: Simple BSD Sockets Problem

OK. I will write this dumb server.

However, I got it to work. Seems that if I give the NICK and USER command together with one send(), they will work. Otherwise, they won't. However, the strange thing is that in both cases, the return value of the send() command is 0!

I really can't understand this. I will try and write the dumb server to see what will happen.

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. char nickCommand[] = "NICK MyWayCoolBotNick\r\n";
  15. char userCommand[] = "USER ObjcBot 8 * :The Objective-C Bot\r\n";
  16.  
  17. char allCommands[] = "NICK MyWayCoolBotNick\r\nUSER ObjcBot 8 * :The Objective-C Bot\r\n";
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21. int sockfd, numbytes;
  22. char buf[MAXDATASIZE];
  23. struct hostent *he;
  24. struct sockaddr_in their_addr; // connector's address information
  25.  
  26. he = gethostbyname("efnet.teleglobe.net");
  27. sockfd = socket(PF_INET, SOCK_STREAM, 0);
  28. if (sockfd < 0) {
  29. printf("error creating the socket\n");
  30. }
  31. their_addr.sin_family = AF_INET;
  32. their_addr.sin_port = htons(PORT);
  33. their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  34. memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
  35.  
  36.  
  37. if (connect(sockfd, (struct sockaddr *)&their_addr,
  38. sizeof their_addr) == -1) {
  39. perror("connect\n");
  40. exit(1);
  41. }
  42. else {
  43. //send(sockfd, nickCommand, sizeof(nickCommand), 0);
  44.  
  45. int bytesSent = 0;
  46. if ( bytesSent = send(sockfd, userCommand, sizeof(userCommand), 0) <= 0 )
  47. printf("could not send NICK message to the server\n");
  48. else
  49. printf("bytes sent: %i\n", bytesSent);
  50.  
  51. if ( send(sockfd, nickCommand, sizeof(nickCommand), 0) <= 0 )
  52. printf("could not send USER message to the server\n");
  53. else
  54. printf("bytes sent: %i\n", bytesSent);
  55.  
  56.  
  57. /*if (bytesSent = send(sockfd, allCommands, sizeof(allCommands), 0) <=0 ) {
  58. perror("send");
  59. }
  60. else
  61. printf("bytes sent: %i\n", bytesSent);
  62. printf("You got the connection to the server!\n");
  63. */
  64. fprintf(stderr,"After send, entering recv loop\n");
  65. numbytes = 0;
  66.  
  67.  
  68. do {
  69. numbytes = 0;
  70. memset(buf, 0, sizeof(buf));
  71. //fprintf(stderr,"In recv loop\n");
  72. numbytes=recv(sockfd, buf, sizeof(buf)-1, 0); //-1 b/c you will null terminate
  73. buf[numbytes] = '\0';
  74. printf("Received: %s",buf);
  75. } while (numbytes != 0);
  76.  
  77. //char command[] = "PING\r\n";
  78.  
  79. //system("telnet efnet.teleglobe.net 6667");
  80.  
  81. //send(sockfd, command, sizeof(command), 0);
  82.  
  83. printf("exited first loop!!!\n");
  84. do {
  85.  
  86. numbytes = 0;
  87. memset(buf, 0, sizeof(buf));
  88. //fprintf(stderr,"In recv loop\n");
  89. numbytes=recv(sockfd, buf, sizeof(buf)-1, 0); //-1 b/c you will null terminate
  90. buf[numbytes] = '\0';
  91. printf("Received: %s",buf);
  92. } while (numbytes !=0);
  93.  
  94.  
  95.  
  96. fflush(stdout);
  97. close(sockfd);
  98. }
  99. return 0;
  100. }
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Mar 7th, 2008, 3:16 PM   #16
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
Re: Simple BSD Sockets Problem

Your separate commands send the USER command then the NICK command, but your all in one sends NICK then USER. I don't know if that would cause the problem, but it would be worth checking out.

Also I would be tempted to use sizeof(userCommand) - 1 as the length to send, otherwise you are sending an extra zero byte which might be confusing the other end.
The Dark is offline   Reply With Quote
Old Mar 7th, 2008, 5:02 PM   #17
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 234
Rep Power: 3 Soulstorm is on a distinguished road
Re: Simple BSD Sockets Problem

You got me. The last character of the commands was the problem. I deleted the last character, and everything else worked fine. Thanks a lot.
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple blackjack script problem. Need help. jokr004 C++ 6 Feb 10th, 2006 11:44 AM
EXTREMELY simple problem d_heyzie C++ 15 Feb 2nd, 2006 11:32 PM
sockets, problem with select() Wizard1988 C++ 0 Jan 15th, 2006 4:09 PM
Simple Perl / MySQL Problem.. pls help! domquemo Perl 0 Jan 11th, 2006 4:08 AM
repaint() problem in very simple program dotred Java 1 Mar 17th, 2005 5:17 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC