README.md

December 5, 2022 ยท View on GitHub

Dynamically displaying messages from newsgroups using ASP.Net

Description

The code sample given below demonstrates how to communicate with a news server using NNTP.

The sample is not a complete implementation of NNTP client, but rather a demonstratio of how to communicate with a NNTP server via System.Net.Sockets.TcpClient . For more details on NNTP communication please refer RFC0977.

More Info

Submitted On
ByAnoj Kumar
LevelAdvanced
User Rating4.6 (23 globes from 5 users)
CompatibilityVB.NET, ASP.NET
CategoryMiscellaneous
World.Net (C#, VB.net)
Archive File

Source Code

Dynamically displaying messages from newsgroups using ASP.Net


Summary

The code sample given below demonstrates how to communicate with a news server using NNTP.

The sample is not a complete implementation of NNTP client, but rather a demonstratio of how to communicate with a NNTP server via System.Net.Sockets.TcpClient . For more details on NNTP communication please refer RFC0977.

Code

   <%@ Page Language="VB" Debug="true" %>
   <%@ import Namespace="System.Net.Sockets" %>
   <%@ import Namespace="System.IO" %>
   <script runat="server">
  Sub Page_Load(sender As Object, e As EventArgs)
   Dim tcpClient As New System.Net.Sockets.TcpClient()
   tcpClient.Connect("news.microsoft.com", 119)
   Dim networkStream As NetworkStream = tcpClient.GetStream()
   Dim responseArr() as string
   Dim bytes(tcpClient.ReceiveBufferSize) As Byte
   networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
   ' send GROUP command to get the staus and number of messages available
   ' the format returned will be
   '211 104 10011 10125 microsoft.public.dotnet.framework.aspnet group selected
   '(there are 104 articles on file, from 10011 to 10125)
   Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("GROUP " & _
     DropDownList1.items(DropDownList1.selectedindex).value & ControlChars.CrLf)
   networkStream.write(sendBytes,0,sendBytes.length)
   bytes.clear(bytes,0,bytes.length)
   networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
   responseArr=split(Encoding.ASCII.GetString(bytes))
   'we are interested in 3rd index as it contains the last number of message
   Dim fetchFrom as long=ctype(responseArr(3),long)
   fetchFrom =fetchFrom-6 ' subtract it from 10 as we r interested in latest 6 messages
   Dim i as integer
   for i=1 to 6
   sendbytes=Encoding.ASCII.GetBytes("ARTICLE " & fetchFrom & ControlChars.CrLf )
   networkStream.write(sendBytes,0,sendBytes.length)
   bytes.clear(bytes,0,bytes.length)
   networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
   Dim Message as String
   Dim Header as String
   Dim Body as String
   Dim curLine as String
   Message=Encoding.ASCII.GetString(bytes)
   if left(Message,3)="220" then ' if status is OK=220 then
	   Header=left(Message,instr(1,Message,ControlChars.CrLf & ControlChars.CrLf))' get the header
  	 Body=mid(Message,instr(1,Message,ControlChars.CrLf & ControlChars.CrLf))' get the body part
  	 Dim sr as new Stringreader(Header)
  	 curLine=sr.readLine()
  	 while not curLine is nothing
  	 	if instr(1,curLine,"From:")=1 or instr(1,curLine,"Sender:")=1 & _
        or instr(1,curLine,"Date:")=1 or instr(1,curLine,"Message-ID:") then
  	  	AddToTable(curLine,true)
  	 	end if
  	 	curLine=sr.readLine()
  	 end while
  	 AddToTable(Body,false)
  else
  	 AddToTable("Failed to receive message (" & fetchfrom &")",true)
  end if
   fetchfrom += 1
   next
   tcpClient.Close()
   End Sub
   Sub AddToTable(ss as String,Header as Boolean)
   Dim tr as new TableRow()
   Dim td as new TableCell()
   me.table1.rows.add(tr)
   tr.cells.add(td)
   if header then
   td.BackColor=System.Drawing.Color.Silver
   td.text=ss
   else
   td.text="<PRE>" & Server.HtmlEncode(ss) & "</PRE>"
   end if
   End SuB
</script>
   <html>
   <head>
   </head>
   <body>
   <form runat="server">
   <p>
   <table style="WIDTH: 508px; HEIGHT: 28px" border="0">
   <tbody>
   <tr>
   <td>
   Select a Newsgroup</td>
   <td>
   <asp:DropDownList id="DropDownList1" runat="server" Width="274px">
   <asp:ListItem Value="microsoft.public.dotnet.framework.aspnet">
       microsoft.public.dotnet.framework.aspnet</asp:ListItem>
   <asp:ListItem Value="microsoft.public.dotnet.languages.vb">
       microsoft.public.dotnet.languages.vb</asp:ListItem>
   <asp:ListItem Value="microsoft.public.xml">microsoft.public.xml</asp:ListItem>
   </asp:DropDownList>
   &nbsp;
   <input type="submit" value="Submit" /></td>
   </tr>
   </tbody>
   </table>
   </p>
   <p>
   <br />
   <asp:Label id="lab1" runat="server"></asp:Label>
   <br />
   <asp:Table id="Table1" runat="server" BorderColor="Gray" BorderWidth="1px" CellSpacing="0"
   CellPadding="3" BorderStyle="Solid" GridLines="Both"></asp:Table>
   </p>
   </form>
   </body>
   </html>