article

Dynamically displaying messages from newsgroups using ASP.Net

Email
Submitted on: 1/1/2015 11:30:00 AM
By: Anoj Kumar (from psc cd)  
Level: Advanced
User Rating: By 5 Users
Compatibility: VB.NET, ASP.NET
Views: 1442
 
     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.

 
				
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>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.