In C# to use a TcpClient or generally to connect to a socket how can I first check if a certain port is free on my machine?
more info: This is the code I use:
TcpClient c; //I want to check here if port is free. c = new TcpClient(ip, port);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since you’re using a
TcpClient, that means you’re checking open TCP ports. There are lots of good objects available in the System.Net.NetworkInformation namespace.Use the
IPGlobalPropertiesobject to get to an array ofTcpConnectionInformationobjects, which you can then interrogate about endpoint IP and port.