Sockets and Arraylists

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();

}

}
 
G

Guest

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();

}

}
 
A

Adrian McNally

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();

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top