It is not working & having a problem as.... "The connection could not made
because the target Machine Actively refussed it"
using System;
using System.IO;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections.Specialized;
namespace QuoteServernsp
{
/// <summary>
/// Summary description for QuoteServer.
/// </summary>
public class QuoteServer
{
private TcpListener listener;
private int port;
private string filename;
private StringCollection quotes;
private Random random;
private Thread listenerThread;
public QuoteServer():this("quotes.txt"){}
public QuoteServer(string filename):this(filename,7890){}
public QuoteServer(string filename, int port)
{
this.filename=filename;
this.port=port;
}
protected void ReadQuotes()
{
quotes=new StringCollection();
Stream stream=File.OpenRead(filename);
StreamReader streamReader=new StreamReader(stream);
string quote;
while((quote=streamReader.ReadLine())!=null)
{
quotes.Add(quote);
}
streamReader.Close();
stream.Close();
random=new Random();
}
protected string GetRandomQuoteOfTheDay()
{
int index=random.Next(0,quotes.Count);
return quotes[index];
}
public void Start()
{
ReadQuotes();
Console.WriteLine("inside QuoteServer.Start()");
listenerThread=new Thread( new ThreadStart(this.Listener));
listenerThread.Start();
}
protected void Listener()
{
try
{
IPAddress ipAddress=Dns.Resolve("localhost").AddressList[0];
listener=new TcpListener(ipAddress, port);
listener.Start();
//while(true)
//{
Socket socket=listener.AcceptSocket();
string message=GetRandomQuoteOfTheDay();
UnicodeEncoding encoder=new UnicodeEncoding();
byte[] buffer=encoder.GetBytes(message);
Console.WriteLine("inside QuoteServer.Listener()");
Console.WriteLine(buffer);
socket.Send(buffer, buffer.Length,0);
socket.Close();
//}
}
catch(SocketException e)
{
Console.WriteLine(e.Message);
}
}
public void Stop()
{
if(listenerThread.IsAlive)
{
listenerThread.Abort();
listener.Stop();
}
}
public void Suspend()
{
listenerThread.Suspend();
}
public void Resume()
{
listenerThread.Resume();
}
public void RefreshQuotes()
{
ReadQuotes();
}
}
}
Yahoo! India Matrimony: Find your life partneronline.
[Non-text portions of this message have been removed]
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/AspClassicAnyQuestionIsOk/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/