How to NNTP in C#

Getting the Newsgroups

The GetNewsgroups method, receives from the NNTP server all the forums that are supported by the server.

public ArrayList GetNewsgroups()
{
    string message;
    string response;

    ArrayList retval = new ArrayList();
    message = "LIST\r\n";
    Write(message);
    response = Response();
    if (response.Substring(0, 3) != "215")
    {
        throw new NntpException(response);
    }

    while (true)
    {
        response = Response();
        if (response == ".\r\n" ||
            response == ".\n")
        {
            return retval;
        }
        else
        {
            char[] seps = {' ' };
            string[] values = response.Split(seps);
            retval.Add(values[0]);
            continue;
        }
    }
}

The GetNewsgroups method begins by sending a LIST message to the NNTP server. The NNTP server will respond initially with the 215 status-code indicating that it successfully received the LIST message. Then the NNTP server will respond with a series of lines, each representing one forum on the NNTP server. After all the forums are sent, the NNTP server will send one line with a single period, indicating the end of the forum list. The list of forums is returned from the GetNewsgroups method as an ArrayList of strings.

From the list of forums, you can select one forum and receive from the GetNews method all the news for that forum. Call GetNews passing the name of the forum to receive the news postings.

public ArrayList GetNews(string newsgroup)
{
    string message;
    string response;

    ArrayList retval = new ArrayList();
    message = "GROUP " + newsgroup + "\r\n";
    Write(message);
    response = Response();
    if (response.Substring(0, 3) != "211")
    {
        throw new NntpException(response);
    }

    char[] seps = {' ' };
    string[] values = response.Split(seps);

    long start = Int32.Parse(values[2]);
    long end = Int32.Parse(values[3]);

    if (start+ 100 < end && end > 100)
    {
        start = end-100;
    }

    for (long i= start; i< end; i++)
    {
        message = "ARTICLE " + i + "\r\n";
        Write(message);
        response = Response();
        if (response.Substring(0, 3) == "423")
        {
            continue;
        }
        if (response.Substring(0, 3) != "220")
        {
            throw new NntpException(response);
        }

        string article = "";
        while (true)
        {
            response = Response();
            if (response == ".\r\n")
            {
                break;
            }

            if (response == ".\n")
            {
                break;
            }
            if (article.Length < 1024)
            {
                article += response;
            };
        }

        retval.Add(article);
    }

    return retval;
}

The GetNews method sends a GROUP message to the NNTP server. The NNTP server will responds with a status-code of 211, indicating success, the numbers of articles in the forum present the server, the lowest message number for an article in the forum and the highest message number for an article in the forum. The method then repeatedly sends an ARTICLE message requesting each article between the lowest and highest message numbers. The NNTP server responds with a 423 status-code if the article is not present on the server and a 220 status-code if the article is present. When we receive a 423 status-code, then we skip to the next message number. When we receive a 220 status-code, the status line is followed by the content of the message and terminated with the now familiar with line with only one period. As the articles are received then are placed into an ArrayList object and returned from the GetNews function once all messages are received.

You might also like...

Comments

About the author

Randy Charles Morin

Randy Charles Morin Canada

Randy's article are Copyright 1998-2003 Randy Charles Morin

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“An idiot with a computer is a faster, better idiot” - Rich Julius