bp2008 / IPScanner

IP address scanner written in C#
MIT License
19 stars 4 forks source link

downloaded code and it doesnt display the designer. #2

Open pyro0ownz opened 4 years ago

pyro0ownz commented 4 years ago

Downloaded the code and was trying to see how you got connections going, as i'm trying to make a proxy scanner by looking at your connection code, designer doesn't display on visual studio 2017. It would be nice if your able to upload basic code on using something like this. I just need to know if the port specified is open on all of the ips on the subnet. It goes to connect to an ip and a port but it hangs then throws an exception instead of ignoring the exception and just going to the next ip until it hits a connection and adds it to the text box.

try { string ip = this.dec1.Text.ToString() + "." + this.dec2.Text.ToString() + "." + this.dec3.Text.ToString() + "." + this.dec4.Text.ToString(); int port = int.Parse(this.port.Text); int request = int.Parse(dec4.Text); while (request < 256) { using (TcpClient connection = new TcpClientWithTimeout(ip, port, int.Parse(this.Interval.Text)).Connect()) { if (connection.Connected) { Found.Items.Add(ip + ":" + port); connection.Close(); request++; } else if(connection.ReceiveTimeout > 0 ) { dec4.Text = request.ToString(); connection.Close(); request++; } else { connection.Close(); MessageBox.Show("broken"); } } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); }

bp2008 commented 4 years ago

That is the way the sockets (and TcpClient) work in .NET. If the connection fails, it throws a specific exception.

The Connect() call will not return until the connection either succeeds or fails, so you need to do this on a background thread or else the app will not respond.

pyro0ownz commented 4 years ago

thats exactly what was happening. I had made some modifications but i did figure something out. When i use a single instance and check a proxy and port it actually connects and adds it to the box. when i run it like this it just blows right past it. So i think i need to use multi threads and it might actually return a connection if its live i take it? I have no idea on how to use threads.

//initialize Socket[] connect = new Socket[100]; // Initalization for (int i = 0; i < 100; i++) { connect[i] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); }

        try
        {
            while ( int.Parse(dec4.Text) < 256)
            {
                for (int i = 0; i < 255; i++)
                {
                    connect[i].ConnectAsync(ip, port).Wait(time);
                    if (connect[i].Connected)
                    {
                        Found.Items.Add(ip + ":" + port);
                        connect[i].Close();
                        if (request < 255)
                            request++;
                        this.dec4.Text = request.ToString();

                    }
                    else if (!connect[i].Connected)
                    {
                        connect[i].Close();
                        if (request < 255)
                            request++;
                        this.dec4.Text = request.ToString();
                    }
                    else
                    {
                        connect[i].Close();
                        MessageBox.Show("broken");
                        return;
                    }
                }

            }
        }

        catch
        {

        }
bp2008 commented 4 years ago

Well it sounds like you have a lot of learning to do. Threads are very tricky to learn, but will be incredibly important for what you are trying to do. Trying to do it all on the UI thread like that will just make your app hang for minutes before you get your result. Whereas with proper threading you could be done with the scan in a couple seconds and have the UI stay responsive the whole time.

You could also use the async/await stuff but that is altogether a different way of doing background work, and you should probably learn how threads work first.

pyro0ownz commented 4 years ago

pretty much normally it was easier in vb6 because winsock did everything for you but had its problems. Trying to convert things into c# is a pain. where would you suggest starting at? in class we never had a need for threading but when you get into the real world and work with connections it thus becomes imperative to learn the real deal stuff.

bp2008 commented 4 years ago

Sorry, I'm not a teacher of this stuff and I don't know where to point you.