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
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
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/
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 :)
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
Re: Reading and Parsing Text From a Text Document
Quote:
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
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
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