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.

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.

“Beware of bugs in the above code; I have only proved it correct, not tried it.” - Donald Knuth