I have a Connection class that I use as the base of my port scanning code. I implemented code to set a timeout period for a socket by creating a separate thread which waits for x amount of ms. then promptly calls back on the connect function (from which it was created). The connect function returns a value of either 0 or the port that was successfully connected to.
The problem I have is that the code works great in XP but it seems as if in Vista, it just ignores the timeout code all together. I tried commenting out the timeout code and running it on an XP machine to make sure -- and without it, it takes just as long as it does on Vista (which is around 1 port per second per thread). (I'm guessing the socket timeout period is set to 1 second.)
I've provided the code below:
CODE
private class Connection
{
string ip;
int port;
int cTimeout;
Socket s;
bool bto;
bool df;
public Connection(string ipAddr, int portNum, int timeOut)
{
ip = ipAddr;
port = portNum;
cTimeout = timeOut;
}
private void toPeriod()
{
Thread.Sleep(cTimeout);
if (s.Connected) bto = true; else df = true;
s.Close();
this.Connect();
}
public int Connect()
{
if (bto) return port;
if (df) return 0;
try
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Thread t = new Thread(new ThreadStart(toPeriod));
t.Start();
s.Connect(ip, port);
t.Abort();
if (s.Connected) return port;
s.Close();
return 0;
}
catch
{
return 0;
}
}
}
I think either Vista doesn't like how the code is calling back on the function to return a value or somehow the socket/thread aren't terminating like they should in Vista.
So my question is, anyone know for sure why this doesn't work in Vista like it does XP? Also, if anyone knows the answer, what can I do to get it working properly in Vista? This is a personal project but this problem is bugging me.
Thanks in advance!