Library tutorials & articles

How to NNTP in C#

Connecting & Disconnecting

The first method of our Nntp client class is the Connect method. This method takes a server name that represents the remote NNTP server that will service our requests.

public void Connect(string server)
{
    string response;

    Connect(server, 119);
    response = Response();
    if (response.Substring(0, 3) != "200")
    {
        throw new NntpException(response);
    }
}

We call the Connect method of our base TcpClient class with the server name and port 119. Port 119 is the well-known port for NNTP servers. The server should respond with a status-code indicating that connection was successful. When you are finished calling methods to the Nntp client object, then you should call the Disconnect method to terminate the connection.

public void Disconnect()
{
    string message;
    string response;

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

The method will send a QUIT message to the server and the server should respond with a 205 status-code indicating that the it is disconnecting the socket.

When you first instantiate the Nntp object you should call the Connect method and when you are finished you should call the Disconnect method. In between, you can call three method, GetNewsgroups, GetNews and Post, to receive and send news to the NNTP server.

Comments

  1. 08 Apr 2006 at 22:54

    Great article - thanks. 

    However I'd like to post HTML to an NNTP server.  I'm assuming that I need to specify this in the Header info somehow but can't find any documentation on NNTP message headers.

    Could you shed some light here.

    Thanks again

     

  2. 20 Apr 2005 at 21:35

    The only problem I found with this article, was the missing "Supporting Functions" you included in your other programs.  Other than that, this is an awesome article!  Keep up the good work!

  3. 01 Oct 2004 at 08:59


    This saved me days of work ! Thanks, really excellent.

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of How to NNTP in C#.

Leave a comment

Sign in or Join us (it's free).

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

Want to stay in touch with what's going on? Follow us on twitter!