Problem connecting to nntp server and sending commands 
Author Message
 Problem connecting to nntp server and sending commands

Hi there!

I'm trying to play with some socket stuff (coming from the Windows world
where I used CSocket) and I'm having a spot of trouble. I'm connecting
to a news (nntp) server and it seems like I'm unable to send a command
and get a response back. Here's the output of my program:
-- cut here --


[Server OK, ready to send commands]
Sending: GROUP news.test
-- end cut --

What's really bugging me is the line after the "200" response. What is

When I telnet to news 119 and do things manually I don't see it, so it
must be a problem with my code.

Here's the code (nntp.c):
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int main(int argc, char *argv[]) {

   char *host;
   char *command;
   int sockfd;
   int len, result;
   struct sockaddr_in address;
   struct hostent *hostinfo;
   struct servent *servinfo;
   char buffer[256];
   fd_set inputs, testfds;
   struct timeval timeout;

   if (argc == 1)
     host = "news.ne.mediaone.net";
   else
     host = argv[1];

   hostinfo = gethostbyname(host);
   if (!hostinfo) {
      printf("No host: %s\n", host);
      exit(1);
   }

   servinfo = getservbyname("nntp", "tcp");

   if (!servinfo) {
      printf("No news service!\n");
      exit(1);
   }

   sockfd = socket(AF_INET, SOCK_STREAM, 0);

   address.sin_family = AF_INET;
   address.sin_port = servinfo->s_port;
   address.sin_addr = *(struct in_addr *)*hostinfo->h_addr_list;
   len = sizeof(address);

   result = connect(sockfd, (struct sockaddr *)&address, len);

   if (result == -1) {
      printf("Error connecting!\n");
      exit(1);
   }

   command = "GROUP news.test\n\r";

   FD_ZERO(&inputs);
   FD_SET(sockfd, &inputs);

   while (1) {

      int fd;
      char *code;

      testfds = inputs;
      timeout.tv_sec = 0;
      result = select(FD_SETSIZE, &testfds, (fd_set *)0, (fd_set *)0,
&timeout);
      if (result == -1) {
         printf("Error!\n");
         exit(1);
      }

      if (result > 0) {
         // activity on socket
         len = read(sockfd, buffer, sizeof(buffer));
         printf("%s\n", buffer);

         code = (char *)strtok((char *)buffer, " ");

         if (strstr(code, "200")) {
            printf("[Server OK, ready to send commands]\n");
            printf("Sending: %s\n", command);
            write(sockfd, "group news.test\n", 16);
         } else {
            printf("[Unknown response code : %s]\n", code);
         }
      }

   }
   close(sockfd);

   exit(0);

Quote:
}

Apologies if it's somewhat mangled. I'm posting from deja. ;(

If anyone can help or offer a head slap or two, it'd be most
appriciated.

--

News, articles and resources for web professionals and developers:
http://www.*-*-*.com/

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 18 Dec 2002 03:00:00 GMT  
 Problem connecting to nntp server and sending commands


Quote:
> Hi there!

> I'm trying to play with some socket stuff (coming from the Windows
world
> where I used CSocket) and I'm having a spot of trouble. I'm connecting
> to a news (nntp) server and it seems like I'm unable to send a command
> and get a response back. Here's the output of my program:

Ah yes, the joys of asking silly questions right before I figure it out
myself. ;) I'm still not sure where the garbage characters came from,
but I've fixed my problem and now the code works fine. If anyone is
interested, here's the code that DOES work (adn gets the number of
articles in news.test):

-- cut here --

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int main(int argc, char *argv[]) {

   char *host;
   char command[512];
   int sockfd;
   int len, result;
   struct sockaddr_in address;
   struct hostent *hostinfo;
   struct servent *servinfo;
   char buffer[256];
   fd_set inputs, testfds;
   struct timeval timeout;
   char *code;
   int cmdsent = 0;

   if (argc == 1)
     host = "news.ne.mediaone.net";
   else
     host = argv[1];

   hostinfo = gethostbyname(host);
   if (!hostinfo) {
      printf("No host: %s\n", host);
      exit(1);
   }

   servinfo = getservbyname("nntp", "tcp");

   if (!servinfo) {
      printf("No news service!\n");
      exit(1);
   }

   sockfd = socket(AF_INET, SOCK_STREAM, 0);
   address.sin_family = AF_INET;
   address.sin_port = servinfo->s_port;
   address.sin_addr = *(struct in_addr *)*hostinfo->h_addr_list;
   len = sizeof(address);

   result = connect(sockfd, (struct sockaddr *)&address, len);

   if (result == -1) {
      printf("Error connecting!\n");
      exit(1);
   }

   //command = "GROUP news.test\n\r";
   strcpy(command, "group news.test\r\n");

   FD_ZERO(&inputs);
   FD_SET(sockfd, &inputs);

   while (1) {

      int fd;

      testfds = inputs;
      timeout.tv_sec = 0;
      result = select(FD_SETSIZE, &testfds, (fd_set *)0, (fd_set *)0,
&timeout);
      if (result == -1) {
         printf("Error!\n");
         exit(1);
      }

      if (result > 0) {
         // activity on socket
         len = read(sockfd, buffer, sizeof(buffer));
         printf("%s\n", buffer);

         code = (char *)strtok((char *)buffer, " ");

         if (strstr(code, "200")) {
            printf("[Server OK, ready to send commands]\n");
         } else {
            printf("[Unknown response code : %s]\n", code);
         }
      }
  }
   close(sockfd);

   exit(0);

Quote:
}

-- end cut --

Again apologies for the poor formatting (dejanews).

--

News, articles and resources for web professionals and developers:
http://www.kst.com

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 18 Dec 2002 03:00:00 GMT  
 Problem connecting to nntp server and sending commands

Quote:

>Ah yes, the joys of asking silly questions right before I figure it out
>myself. ;) I'm still not sure where the garbage characters came from,
>but I've fixed my problem and now the code works fine. If anyone is
>interested, here's the code that DOES work (adn gets the number of
>articles in news.test):

The problem is that you still have garbage in your code.

Quote:
>-- cut here --

>#include <sys/socket.h>
>#include <sys/types.h>
>#include <sys/time.h>
>#include <netinet/in.h>
>#include <netdb.h>
>#include <stdio.h>
>#include <unistd.h>
>#include <fcntl.h>
>#include <sys/ioctl.h>

The only standard header is <stdio.h>

All the others are specific to your system or compiler. If you want a better
answer, please ask a newsgroup related with your compiler and / or system.

--
-hs-
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FCLC-FAQ: http://www.isty-info.uvsq.fr/~rumeau/FAQ-FCLC/FAQ-FCLC.html



Thu, 19 Dec 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Problem sending commands to FTP server

2. Threading problem - Object is not connected to the server

3. Problem:OLEDB connect SQL Server in IP

4. Problems when connecting to DB2 Server

5. Problem of connecting from ISAPI dll to SQL Server using ODBC

6. Problem connecting to SQL Server 6.5 from a Visual C++ Application

7. Problem connecting to SQL Server 6.5 from a Visual C++ Application

8. Connecting to server problem with CSocket

9. an nntp control or CSocket sample using nntp

10. C#-based NNTP server example?

11. Help Needed in Reading Mails from POP3 and NNTP server

12. public NNTP server for microsoft.public.vc.database ?

 

 
Powered by phpBB® Forum Software