• Welcome to Valhalla Legends Archive.
 

Problem with Properties

Started by BaDDBLooD, January 29, 2006, 04:08 PM

Previous topic - Next topic

BaDDBLooD

I am trying to make a simple program to connect to a SOCKS Server.



using System;
using System.Net;
using System.Net.Sockets;
using System.IO;

/// <summary>
/// Summary description for Class1.
/// </summary>
class Proxy
{
public string Server
{
get
{
return Server;
}
set
{
Server = value;
}
}

public int Port
{
get
{
return Port;
}
set
{
Port = value;
}
}

public string Type
{
get
{
return Type;
}
set
{
Type = value;
}
}

private const string FILE_NAME = "Proxies.txt";
/// <summary>
/// The main entry point for the application.
/// </summary>

public static void Main(String[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist.", FILE_NAME);
}
else
{
Console.WriteLine("Proxies.txt Found, Loading...");
StreamReader sr = File.OpenText(FILE_NAME);
string input;
while ((input = sr.ReadLine()) != null)
{
string [] split = input.Split(':', '@');
this.Server = split[0];
this.Port = Convert.ToInt16(split[1]);
this.Type = split[2];

try
{
Socket s = ConnectSocket(this.Server, this.Port, this.Type);
}

catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

Console.WriteLine ("End Of {0}", FILE_NAME);
sr.Close();
}

Console.ReadLine();
}

public static Socket ConnectSocket(string Server, int Port, string Type)
{
Socket s = null;
IPHostEntry hostEntry = null;
       
hostEntry = Dns.Resolve(Server);

foreach(IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, Port);
Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

Console.WriteLine("Establishing Connection To {0} on Port {1} Using {2} Protocol...", address, Port, Type);
tempSocket.Connect(ipe);

if(tempSocket.Connected == true)
{
Console.WriteLine("Connection Established!");
s = tempSocket;
break;
}
else
{
Console.WriteLine("Connection Failed!");
continue;
}
}

return s;
}
}



I Tried experimenting with Properties and the 'this' Keyword but i seem to be getting a error whenever i use 'this'.

'Keyword this is not valid in a static property, static method, or static field initializer'

I'm also not sure on the best way to retrieve the Address, Port, Version of SOCKS from my test document.

Any Help, Suggestions, Comments is GREATLY Appreciated.

- Joel
There are only two kinds of people who are really fascinating: people who know absolutely everything, and people who know absolutely nothing.

MyndFyre

Quote from: BaDDBLooD on January 29, 2006, 04:08 PM

class Proxy
{
public string Server
{
set
{
Server = value;
}
}

This is not appropriate C# syntax.  You're thinking of Visual Basic.  What will happen is that you'll generate an OutOfMemoryException when you generate infinite recursion.

Within "Proxy", you need to create member variables called "fields" that are private to the class.  Example:

private string m_server;

(I use the m_ prefix to indicate "member" and s_ to indicate "static member").

Then, your property would look more like this:

public string Server
{
     get { return m_server; }
     set
     {
            if (value == null) throw new ArgumentNullException();
            m_server = value;
     }
}

This also demonstrates the reason for using properties instead of just public fields: you can do value checking when someone is trying to change it.

Quote from: BaDDBLooD on January 29, 2006, 04:08 PM

public static void Main(String[] args)
{
// ... chopped
this.Server = split[0];
this.Port = Convert.ToInt16(split[1]);
this.Type = split[2];
}

You said:
Quote'Keyword this is not valid in a static property, static method, or static field initializer'
was your error?  In a static property, method, or field initializer.  Well, Main() is a static method.

You have to create an instance of your Server object.  Main() doesn't make one automatically.

Server server = new Server();

Then you can mess with "server"'s properties until the cows come home.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

BaDDBLooD

Quote from: MyndFyre on January 29, 2006, 09:04 PM
Quote from: BaDDBLooD on January 29, 2006, 04:08 PM

class Proxy
{
public string Server
{
set
{
Server = value;
}
}

This is not appropriate C# syntax.  You're thinking of Visual Basic.  What will happen is that you'll generate an OutOfMemoryException when you generate infinite recursion.

Within "Proxy", you need to create member variables called "fields" that are private to the class.  Example:

private string m_server;

(I use the m_ prefix to indicate "member" and s_ to indicate "static member").

Then, your property would look more like this:

public string Server
{
     get { return m_server; }
     set
     {
            if (value == null) throw new ArgumentNullException();
            m_server = value;
     }
}

This also demonstrates the reason for using properties instead of just public fields: you can do value checking when someone is trying to change it.

Quote from: BaDDBLooD on January 29, 2006, 04:08 PM

public static void Main(String[] args)
{
// ... chopped
this.Server = split[0];
this.Port = Convert.ToInt16(split[1]);
this.Type = split[2];
}

You said:
Quote'Keyword this is not valid in a static property, static method, or static field initializer'
was your error?  In a static property, method, or field initializer.  Well, Main() is a static method.

You have to create an instance of your Server object.  Main() doesn't make one automatically.

Server server = new Server();

Then you can mess with "server"'s properties until the cows come home.

Thanks Rob, i don't know where i would be without your endless supply of knowledge.

Have you had any experience with Regular Expressions? I'm learning about it for use in reading and checking the addresses found in my text document.  It's quite the confusing topic, but if i can understand it i think it will be very helpfull.  I'm sure it's just that i'm tired and my brain hurts, maybe it'll sink in better tomorrow. 
There are only two kinds of people who are really fascinating: people who know absolutely everything, and people who know absolutely nothing.

MyndFyre

Quote from: BaDDBLooD on January 29, 2006, 10:59 PM
Have you had any experience with Regular Expressions? I'm learning about it for use in reading and checking the addresses found in my text document.  It's quite the confusing topic, but if i can understand it i think it will be very helpfull.  I'm sure it's just that i'm tired and my brain hurts, maybe it'll sink in better tomorrow. 

That is something that I haven't been able to get my mind around.  Approaching them in an OO fashion seems silly to me, yet that's what Microsoft did.  *shrug*
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.