Socket, Problem with socket is that I cannot make more than one connection to a WAN IP
With two sockets you get two connections. Anyway, with sockets you can make a port scanner. With googling you'll find a plenty of examples. Here's one VB Port Scanner in VB but easily converted to C#. And here's one more link about "pinging a port" Ping IpAddress with Port number? :D
HTH
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
I took from the second link in my previous post a snippet " ReadOnly Property ScanPort(ByVal IP As System.Net.IPAddress, ByVal Port As Integer) As Boolean " and converted it to C# function:
public bool ScanPort(IPAddress IP, int Port)
{
TcpClient aSocket;
aSocket = new TcpClient();
try
{
aSocket.Connect(IP, Port);
}
catch(Exception ex)
{
// Something went wrong
return false;
}
if(aSocket.Connected)
{
// Got connected to Address+Port
aSocket.Close();
aSocket = null;
return true;
}
else
{
// Not connected
aSocket.Close();
aSocket = null;
return false;
}
}
and tested with
private void button1_Click(object sender, EventArgs e)
{
string IP;
int Port;
bool connected;
IP = textBox1.Text;
int.TryParse(textBox2.Text, out Port);
connected = ScanPort(IPAddress.Parse(IP), Port);
MessageBox.Show(IP + ":" + Port.ToString() + " is " + (connected ? "connected" : "not connected"));
}
The code doesn't work "as is" for your special case, but has the basic technique you need.
Since you have 16 IPs+ports, you could use Threading or a Background Worker. It depends on, how often (and how fast) you have to check that the connection(s) is alive.
HTH
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
Hi! Nice to hear that you got answer to your problem. Could you please mark the thread as solved. Thank you!
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203