Sockets and Arraylists

  • Thread starter Thread starter Adrian McNally
  • Start date Start date
A

Adrian McNally

Hiya

I'm trying to get a list of news groups from a server into a array list by
using the following code, and then display the choosen group. When i compile
and run the code the only thing it displays is
"System.Collections.ArrayList". Can anyone gimme a hand, or tell me what i
have done wrong?

Thanks in advance.

using System;

using System.Collections;

using System.Net;

using System.Net.Sockets;

using System.IO;

using System.Text;

class nntp

{

static void Main (string[] args)

{

TcpClient nntp = new TcpClient();

ArrayList nGroups = new ArrayList();


nntp.Connect("news.microsoft.com", 119);

NetworkStream nStream = nntp.GetStream();

StreamReader reader = new StreamReader(nStream);

StreamWriter writer = new StreamWriter(nStream);

string s;

reader.ReadLine();

writer.WriteLine("List");

writer.Flush();

s = reader.ReadLine();


s = s.Substring (0, 3);

while (s != ".")

{

s = reader.ReadLine();

if (s != ".")

{

s = s.Substring (0, s.IndexOf(' '));

nGroups.Add(s);


}

}

Random rand = new Random();

int num = rand.Next (0, nGroups.Count);

object rnntp = nGroups[num];

Console.WriteLine("The random group is " + nGroups);

Console.ReadLine();

}

}
 
Hi Adrian,
in the following line of your code:
Console.WriteLine("The random group is " + nGroups);

you passed to ArrayList object to append to the string, this will call the
ToString() method on the ArrayList which by default will print the type of
the object which is why you see System.Collection.ArrayList.

You really want to say something like:
Console.WriteLine("The random group is " + nGroups);


where i is some valid index into the array.


Hope that helps
Mark R Dawson
http://www.markdawson.org




Adrian McNally said:
Hiya

I'm trying to get a list of news groups from a server into a array list by
using the following code, and then display the choosen group. When i compile
and run the code the only thing it displays is
"System.Collections.ArrayList". Can anyone gimme a hand, or tell me what i
have done wrong?

Thanks in advance.

using System;

using System.Collections;

using System.Net;

using System.Net.Sockets;

using System.IO;

using System.Text;

class nntp

{

static void Main (string[] args)

{

TcpClient nntp = new TcpClient();

ArrayList nGroups = new ArrayList();


nntp.Connect("news.microsoft.com", 119);

NetworkStream nStream = nntp.GetStream();

StreamReader reader = new StreamReader(nStream);

StreamWriter writer = new StreamWriter(nStream);

string s;

reader.ReadLine();

writer.WriteLine("List");

writer.Flush();

s = reader.ReadLine();


s = s.Substring (0, 3);

while (s != ".")

{

s = reader.ReadLine();

if (s != ".")

{

s = s.Substring (0, s.IndexOf(' '));

nGroups.Add(s);


}

}

Random rand = new Random();

int num = rand.Next (0, nGroups.Count);

object rnntp = nGroups[num];

Console.WriteLine("The random group is " + nGroups);

Console.ReadLine();

}

}
 
Thank you very much, with your help i've managed to get it work.


Mark R. Dawson said:
Hi Adrian,
in the following line of your code:
Console.WriteLine("The random group is " + nGroups);

you passed to ArrayList object to append to the string, this will call the
ToString() method on the ArrayList which by default will print the type of
the object which is why you see System.Collection.ArrayList.

You really want to say something like:
Console.WriteLine("The random group is " + nGroups);


where i is some valid index into the array.


Hope that helps
Mark R Dawson
http://www.markdawson.org




Adrian McNally said:
Hiya

I'm trying to get a list of news groups from a server into a array list
by
using the following code, and then display the choosen group. When i
compile
and run the code the only thing it displays is
"System.Collections.ArrayList". Can anyone gimme a hand, or tell me what
i
have done wrong?

Thanks in advance.

using System;

using System.Collections;

using System.Net;

using System.Net.Sockets;

using System.IO;

using System.Text;

class nntp

{

static void Main (string[] args)

{

TcpClient nntp = new TcpClient();

ArrayList nGroups = new ArrayList();


nntp.Connect("news.microsoft.com", 119);

NetworkStream nStream = nntp.GetStream();

StreamReader reader = new StreamReader(nStream);

StreamWriter writer = new StreamWriter(nStream);

string s;

reader.ReadLine();

writer.WriteLine("List");

writer.Flush();

s = reader.ReadLine();


s = s.Substring (0, 3);

while (s != ".")

{

s = reader.ReadLine();

if (s != ".")

{

s = s.Substring (0, s.IndexOf(' '));

nGroups.Add(s);


}

}

Random rand = new Random();

int num = rand.Next (0, nGroups.Count);

object rnntp = nGroups[num];

Console.WriteLine("The random group is " + nGroups);

Console.ReadLine();

}

}
 
Back
Top