Author | Message | Time |
---|---|---|
BaDDBLooD | I am trying to make a simple program to connect to a SOCKS Server. [code] 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; } } [/code] 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 | January 29, 2006, 10:08 PM |
Myndfyr | [quote author=BaDDBLooD link=topic=14052.msg143711#msg143711 date=1138572521] [code] class Proxy { public string Server { set { Server = value; } } [/code] [/quote] 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: [code] private string m_server; [/code] (I use the m_ prefix to indicate "member" and s_ to indicate "static member"). Then, your property would look more like this: [code] public string Server { get { return m_server; } set { if (value == null) throw new ArgumentNullException(); m_server = value; } } [/code] 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 author=BaDDBLooD link=topic=14052.msg143711#msg143711 date=1138572521] [code] public static void Main(String[] args) { // ... chopped this.Server = split[0]; this.Port = Convert.ToInt16(split[1]); this.Type = split[2]; } [/code] [/quote] You said: [quote]'Keyword this is not valid in a static property, static method, or static field initializer'[/quote] 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. [code] Server server = new Server(); [/code] Then you can mess with "server"'s properties until the cows come home. | January 30, 2006, 3:04 AM |
BaDDBLooD | [quote author=MyndFyre link=topic=14052.msg143749#msg143749 date=1138590262] [quote author=BaDDBLooD link=topic=14052.msg143711#msg143711 date=1138572521] [code] class Proxy { public string Server { set { Server = value; } } [/code] [/quote] 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: [code] private string m_server; [/code] (I use the m_ prefix to indicate "member" and s_ to indicate "static member"). Then, your property would look more like this: [code] public string Server { get { return m_server; } set { if (value == null) throw new ArgumentNullException(); m_server = value; } } [/code] 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 author=BaDDBLooD link=topic=14052.msg143711#msg143711 date=1138572521] [code] public static void Main(String[] args) { // ... chopped this.Server = split[0]; this.Port = Convert.ToInt16(split[1]); this.Type = split[2]; } [/code] [/quote] You said: [quote]'Keyword this is not valid in a static property, static method, or static field initializer'[/quote] 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. [code] Server server = new Server(); [/code] Then you can mess with "server"'s properties until the cows come home. [/quote] 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. | January 30, 2006, 4:59 AM |
Myndfyr | [quote author=BaDDBLooD link=topic=14052.msg143763#msg143763 date=1138597171] 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. [/quote] 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* | January 30, 2006, 5:21 AM |