|
-
Jan 29th, 2006, 02:11 PM
#1
Thread Starter
Hyperactive Member
Reading and Parsing Text From a Text Document
Code:
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
/// <summary>
/// Summary description for Class1.
/// </summary>
class Proxy
{
private const string FILE_NAME = "MyFile.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);
return;
}
else
{
StreamReader sr = File.OpenText(FILE_NAME);
string input;
while ((input = sr.ReadLine()) != null)
{
try
{
Socket s = ConnectSocket(server, port);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
Console.WriteLine ("End Of {0}", FILE_NAME);
sr.Close();
}
Console.ReadLine();
}
private static Socket ConnectSocket(string server, int port)
{
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} ...", address, port);
tempSocket.Connect(ipe);
if(tempSocket.Connected == true)
{
Console.WriteLine("Connection Established!");
s = tempSocket;
break;
}
else
{
Console.WriteLine("Connection Failed!");
continue;
}
}
return s;
}
}
I have my Addresses in the Text Document formatted a specific way.
Example: 255.255.255.255:1080@SOCKS4
I'm trying to get all three different pieces of information and store them. I also want to make sure all pieces of information have been entered, as well as check for any formatting errors, maybe tell the user what line it was on etc..
However, i really have no idea how to do this... i was experimenting with some input.split but that just did not work out how i was hoping...
Thanks 
- Joel
Last edited by BaDDBLooD; Jan 29th, 2006 at 02:24 PM.
-
Jan 29th, 2006, 04:45 PM
#2
Thread Starter
Hyperactive Member
Re: Reading and Parsing Text From a Text Document
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;
}
}
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
Any Help, Suggestions, Comments is Appreciated GREATLY.
- Joel
-
Jan 29th, 2006, 06:58 PM
#3
Sleep mode
Re: Reading and Parsing Text From a Text Document
First off , don't use "this" keyword when referring to static member class . Required reading about OOP .
Second , for your IP parsing , you better off using regex (for validation and parsing) . It's more accurate and hell faster .Again needs some background . Here's a start : http://www.regular-expressions.info/
-
Jan 29th, 2006, 09:41 PM
#4
Sleep mode
-
Jan 29th, 2006, 11:52 PM
#5
Thread Starter
Hyperactive Member
Re: Reading and Parsing Text From a Text Document
Wow, this is really confusing lol. If i can learn it, however, it looks really sweet ass
-
Jan 30th, 2006, 12:18 AM
#6
Frenzied Member
Re: Reading and Parsing Text From a Text Document
Regex will work for you, but so will other things. It depends on how far down the rabbit hole you want to go 
If you need something quick, and simple to understand, you may just want to use .Substring.
Code:
string data = "255.255.255.255:1080@SOCKS4";
int colon = data.IndexOf(":");
int ampersand = data.IndexOf("@");
string ip = data.Substring(0, colon);
string port = data.Substring(colon + 1, ampersand - colon - 1);
string s = data.Substring(ampersand + 1);
Debug.WriteLine(ip);
Debug.WriteLine(port);
Debug.WriteLine(s);
This obviously assumes a couple things, like the hard-coded delimiters, and I'm not saying its the *best* solution, but it is a solution.
Mike
-
Jan 30th, 2006, 12:25 AM
#7
Sleep mode
 Originally Posted by BaDDBLooD
Wow, this is really confusing lol. If i can learn it, however, it looks really sweet ass 
I know but it's so worth it . I'm learning it slowly . It's damn crypted lang ..lol
-
Jan 30th, 2006, 12:41 AM
#8
Frenzied Member
Re: Reading and Parsing Text From a Text Document
 Originally Posted by Pirate
It's damn crypted lang ..lol
No doubt. It's cryptic and probably deserves the term "language" - although some people won't agree with that.
I use regex just infrequently enough to be completely frustrated whenever I need to do something. I can't remember the syntax since I don't use it regularly, so I end up reading tutorials and googling for email regexes or whatever every single time. Just one more time sucker.
Mike
-
Jan 30th, 2006, 07:30 AM
#9
Thread Starter
Hyperactive Member
Re: Reading and Parsing Text From a Text Document
Does anyone know what the regex for 255.255.255.255:1080@SOCKS4 would be? I'm having trouble figuring out exactly what i should use to do what i want, let alone the cryptic watchamcallit to do it, haha.
This is what i got so far:
\d\d\d.\d\d\d.\d\d\d.\d\d\d.\d\d\d\d.\w\w\w\w\w\d
It's not exactly... that efficient haha
Last edited by BaDDBLooD; Jan 30th, 2006 at 09:42 AM.
-
Jan 30th, 2006, 01:12 PM
#10
Sleep mode
You download this tool to test your regex code against the text you're validating : http://www.codeproject.com/dotnet/expresso.asp .
-
Jan 30th, 2006, 09:47 PM
#11
Thread Starter
Hyperactive Member
Re: Reading and Parsing Text From a Text Document
This is what i have came up with so far
Code:
public static void Main()
{
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)
{
Console.WriteLine("Validating Address...");
Regex r = new Regex(@"\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b", RegexOptions.Compiled);
Match m = r.Match(input);
if (m.Success == true)
{
Console.WriteLine("{0} Is Valid!", input);
try
{
Socket s = ConnectSocket(m.Value, 1080, 4);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
else
{
Console.WriteLine("{0} is Wrong Format", m.Value);
}
}
Console.WriteLine ("End Of {0}", FILE_NAME);
sr.Close();
}
Console.ReadLine();
}
I found a Regex Expression for validating a 0-255.0-255.0-255.0-255 IPv4 Ip Address. However, i still have to do the Port/Socks Version.
I wasn't sure if i could do exactly what i wanted to do with regex so i figured i'd ask.
I wanted to check if the string was formatted like 255.255.255.255:1080@SOCKS4/5 then also split it
so i could just do like m.Value(2) or something to that effect.
Is that possible?
Thanks
- Joel
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|