C# socket problem

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

C# socket problem

Post by Dredric1 on Sat Jan 02, 2010 11:24 pm
([msg=32711]see C# socket problem[/msg])

i am programing a Port-Scanner

and i want it to be quick but i have to wait a few minutes for each connection before
it tells me if there was a successful connection is there any way it can timeout quicker
so that there isn't a delay that way it will check and if there is no immediate connection
it moves on to the next one.

here is the code so far:
Code: Select all
for (int i = 0; i < 65536; i++)
{
try
            {
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(server, port);

               Console.Write("Connected < Port: "+i.toString()+" [ex] > :: ");
                string l = Console.Readline();
                if (l == "ex") { socket.Disconnect(true); }
            }
            catch { }
}
the code for the void exception between the ear brackets
Code: Select all
public static }void(exception x = new exception(null)){

ok my brackets dont realy look like ears.
if you didnt get that it means there is nothing(no brains) between your ears.
User avatar
Dredric1
Poster
Poster
 
Posts: 188
Joined: Fri Jan 09, 2009 7:29 pm
Blog: View Blog (0)


Re: C# socket problem

Post by thetan on Sun Jan 03, 2010 2:29 am
([msg=32728]see Re: C# socket problem[/msg])

I've never taken the time to learn C# as i've always been perfectly fine with C/C++.

However, the most common work around for this is to use multiple sockets. In C this can be achieved with setsockopt() defined in sys/socket.h , this is not a portable solution though.
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: C# socket problem

Post by BhaaL on Sun Jan 03, 2010 5:59 am
([msg=32730]see Re: C# socket problem[/msg])

The try/catch approach to this problem is very slow, and might yield false positives.

A lot of operations on the .Net Framework follow the asynchronous pattern, and provide either BeginXxx/EndXxx methods, or a related XxxAsync method that runs in background.
Keep a (thread-safe) storage that knows all open ports, and insert stuff into that storage as you get your results asynchronously.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: C# socket problem

Post by Dredric1 on Sun Jan 03, 2010 8:11 pm
([msg=32773]see Re: C# socket problem[/msg])

BhaaL wrote:The try/catch approach to this problem is very slow, and might yield false positives.

A lot of operations on the .Net Framework follow the asynchronous pattern, and provide either BeginXxx/EndXxx methods, or a related XxxAsync method that runs in background.
Keep a (thread-safe) storage that knows all open ports, and insert stuff into that storage as you get your results asynchronously.


i don't really understand those things
is there any way to make it faster with the knowledge i have now

and what is better than try/catch
the code for the void exception between the ear brackets
Code: Select all
public static }void(exception x = new exception(null)){

ok my brackets dont realy look like ears.
if you didnt get that it means there is nothing(no brains) between your ears.
User avatar
Dredric1
Poster
Poster
 
Posts: 188
Joined: Fri Jan 09, 2009 7:29 pm
Blog: View Blog (0)


Re: C# socket problem

Post by thetan on Sun Jan 03, 2010 8:49 pm
([msg=32780]see Re: C# socket problem[/msg])

Dredric1 wrote:i don't really understand those things
is there any way to make it faster with the knowledge i have now

C# threading
C# .net asynchronous
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: C# socket problem

Post by Dredric1 on Mon Jan 04, 2010 12:43 am
([msg=32789]see Re: C# socket problem[/msg])

ya threading is a good idea
but when it comes down to it
its not actually more efficient
just more of the same efficiency i being done at the same time
the code for the void exception between the ear brackets
Code: Select all
public static }void(exception x = new exception(null)){

ok my brackets dont realy look like ears.
if you didnt get that it means there is nothing(no brains) between your ears.
User avatar
Dredric1
Poster
Poster
 
Posts: 188
Joined: Fri Jan 09, 2009 7:29 pm
Blog: View Blog (0)


Re: C# socket problem

Post by ReMiiX on Mon Jan 04, 2010 12:59 am
([msg=32791]see Re: C# socket problem[/msg])

*edit* I thought you were asking for something else. I deleted my post.
ReMiiX
New User
New User
 
Posts: 16
Joined: Sun Aug 09, 2009 3:36 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests