I'm fairly new to C# and Socket programming, although I have many years of programming experience in other languages.
Overview: Our production servers receive messages on specific TCP ports. During a system upgrade, our interface team is responsible for turning off the messages to the ports. They perform this task via their interface engine software. Our team does not have access to this software. Unfortunately, sometimes the interface team informs us that the interfaces are down when they aren't, and application messages still arrive after we start the system upgrade. This causes big problems. So, we want to develop a tool to display listening and active TCP connections on specific ports and provide the capability to shutdown the associated socket. I understand how to use the Shutdown() and Close() methods for a socket that I created, but have no idea how to close the socket created by another process.
"Currports" is commercial software that will accomplish what we need, but I was informed that there is no way we would be allowed to install it in our production environment.
My recommendation was simply to use netstat and Powershell to identify the ports and then just notify our interface team to do their job. However, my boss wanted a more elegant solution. Also, the interface team may have already left for the day and we would have no way to stop the interfaces without access to the interface engine software.
Notes/Questions:
- I can run my software as Administrator or as the same user that runs the application interface listeners.
- What C# Classes and Methods are available that will allow me to shutdown and close a socket created by another process? Will the OS allow me to do this, presuming I'm running as Administrator or the same user that created the socket?
- Even if I can shutdown the socket for listening ports, what about active connections? I know the defined ports that are listening for incoming messages; however, once a connection is established, wouldn't it be on a different port? How would I know if an established TCP connection is associated with the interface application I'm concerned with?
Thanks for any assistance. Please don't beat me up too bad, I'm trying as best as I can with my limited knowledge of C# and network programming.
Code provided below that shows C# code to return Lists of TCP Listeners and Active Connections. I want to take the results of these methods and be able to shutdown, close, or kill the associated socket. Yes, I know I'm in over my head but this is the assignment my boss gave me. Note: based on additional research since I posted this, it looks like my only option may be to kill the process associated with the socket. If so, how can I do that via C#?
namespace TCPLibrary
{
public class TCPWrapper
{
// Return a List of active TCP Listeners
public List<String> ShowActiveTcpListeners()
{
List<String> tcpActiveListen = new List<String>();
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endPoints = properties.GetActiveTcpListeners();
foreach (IPEndPoint e in endPoints)
{
tcpActiveListen.Add(e.ToString());
}
return tcpActiveListen;
}
// Return a List of active TCP Connections
public List<String> ShowActiveTcpConnections()
{
List<String> tcpActiveConn = new List<String>();
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
foreach (TcpConnectionInformation c in connections)
{
//tcpActiveConn.Add("Local Endpoint: " + c.LocalEndPoint.ToString() + " Remote Endpoint: " + c.RemoteEndPoint.ToString() + " State: " + c.State);
tcpActiveConn.Add("Local Endpoint: " + c.LocalEndPoint.ToString() + " State: " + c.State);
}
return tcpActiveConn;
}
}
}