SpikedSoftware

Tag: yenc

Usenet ReadLine()

by Dean Thomas on Feb.12, 2008, under Uncategorized

Ever tried writing a Usenet client before? Even a simple little one that just downloads an image from your favourite group.

Well, it can be a pain. The spec is simple enough, everything is explained well in the 2 RFCs. But a couple of things weren’t so clear when i started.

First off, the curse of the double dot. For most Usenet calls (Body, Head, etc) its way of notifying you that it has finished sending you data, is to send you a ‘.’. Which is great, nice simple way to let me know that you have finished sending me the information.

However, when you receive a file from usenet, you tend to do it line by line, so you can check for such an end of message. But what if your line of data starts with a ‘.’ because, well it just might. It just adds another ‘.’, so it now becomes ‘..’ which is fine, because i know it’s not the end of the message, and i can just carry on as normal. Do NOT forget to remove one of those ‘.’ otherwise your file will appear as ‘corrupt’ even though it may only be one byte at the end.

Secondly, the encoding. I spent ages trying to figure out why my files were completely different to what they should be, and why my results werent matching up with what say Outlook express could return me. For those of you that didn’t know, Outlook express is a pretty neat Usenet client, terrible as an email client however.

Anywho, usenet files are posted as iso-8859-1, so any file saving that gets done, MUST use that encoding, or again corruption. Imagine not removing a . and saving with the wrong encoding, thats a fun debugging session.

Well, i have attached a sample function that will read a line response from Usenet, and return it as a string (iso-8859-1). It handles the double dot, at the end of the function as you can see.

I Hope this helps someone and saves them more problems than i had…

D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public string ReadLine()
{
    ArrayList returnedBytes = new ArrayList();
    byte[] previousByte = new byte[1];
    byte[] thisByte = new byte[1];
    NetworkStream reader = ServerConnection.GetStream();
 
    while (true)
    {
        //Read a byte, check it
        int received = reader.Read(thisByte, 0, 1);
 
        //Check the byte, let's see what it is...
        if (received == 1)
        {
            bool addByte = false;
            bool endOfLine = false;
 
            //Check if we have a previous byte
            switch ((char)previousByte[0])
            {
                case '\r'://Ignore this byte, it's probably a \n
                    addByte = true;
                    break;
                default:
                    //No previous byte
                    addByte = true;
                    break;
            }
 
            //Check for New Line
            if ((char)thisByte[0] == '\n' && (char)previousByte[0] == '\r')
            {
                endOfLine = true;
            }
 
            //Set the previous byte
            previousByte[0] = thisByte[0];
 
            //Add this byte?
            if (addByte)
                returnedBytes.Add(thisByte[0]);
 
            //Is this the end of the line?
            if (endOfLine)
                break;
 
        }
        else
        {
            break;
        }
 
    }
 
    Encoding usenetEncoding = Encoding.GetEncoding("iso-8859-1");
    string finalLine = usenetEncoding.GetString((byte[])returnedBytes.ToArray());
 
    if (finalLine.StartsWith(".."))
        finalLine = finalLine.Remove(0, 1);
 
    return finalLine;
}
Leave a Comment :, , , , , , , , more...

Looking for something?

Use the form below to search the site:

Custom Search

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...