Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 148335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:56:25+00:00 2026-05-11T08:56:25+00:00

In C# to use a TcpClient or generally to connect to a socket how

  • 0

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); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:56:26+00:00Added an answer on May 11, 2026 at 8:56 am

    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 IPGlobalProperties object to get to an array of TcpConnectionInformation objects, which you can then interrogate about endpoint IP and port.


     int port = 456; //<--- This is your value  bool isAvailable = true;   // Evaluate current system tcp connections. This is the same information provided  // by the netstat command line application, just in .Net strongly-typed object  // form.  We will look through the list, and if our port we would like to use  // in our TcpClient is occupied, we will set isAvailable to false.  IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();  TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();   foreach (TcpConnectionInformation tcpi in tcpConnInfoArray)  {    if (tcpi.LocalEndPoint.Port==port)    {      isAvailable = false;      break;    }  }   // At this point, if isAvailable is true, we can proceed accordingly. 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

use this website a lot but first time posting. My program creates a number
I want to use the TcpClient and TcpListener to send an mp3 file over
I know this might have been asked a thousand times before, but I can't
I have a TCPclient written on Ruby. I want to use it in background.
I have a Delphi application with a Indy TCPServer and TCPClient. I use the
How can I send data to a specific port and ip? I need to
I understand that a TcpClient is a wrapper around the socket class, and I
Our software needs to connect to an server and we do that with use
I'm doing async network in C#.NET with the TcpClient and TcpListener classes. I use
I'm using this code to return some string from a tcpclient but when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.