Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Find the Port a Connection is Using

By Mark Greenbaum,

I would imagine that most of us in the SQL Server universe read the registry to validate or find the port(s) that SQL Server is listening on. I had a situation that was a bit different. I needed to determine on which port(s) connections were coming through from the client to the database server..

We have vendors that do work for my company via VPN. These vendors access the SQL Servers through specific ports in order to distinguish their connections from our internal (employee) connections. I had to provide a quick report on the vendor connections and for some reason completely missed the system view that has the connection information in it. So what did I do? I used Google and Bing to try to find an answer. No joy there. I guessed that this isn't a normal operation so I knew I had to post the question on SSC and had my answer in short order! Thanks Steve.

This is the script. It is short and simple but what I needed. The views have a wealth of information in them so more than just a port number for a connection is available. Hope it this helps someone else.

SELECT c.session_id, c.local_tcp_port, s.login_name, s.host_name, s.program_name
FROM sys.dm_exec_connections AS c INNER JOIN
sys.dm_exec_sessions AS s on c.session_id = s.session_id
WHERE c.local_tcp_port <> 1433

If you run this you will get some results, similar to the samples shown below.


session_id local_tcp_port login_name host_name program_name

88 11433 DomainXX\vendor1 D0004785 .Net SqlClient Data Provider
304 2048 DomainXX\vendor2 Laptop123 .Net SqlClient Data Provider
1022 2096 DomainXX\vendor3 MRE23098 Microsoft SQL Server Management Studio - Query


I save the output to a csv file since this is used internally to audit external connections. Since we use it (DBA group), it doesn't need to be a "formal" report but the query could be used in a data source for SSRS and the report could be "spiffed up" if it were to go to management.

A side benefit of needing this data is that it gave me another tool to use as part of my process for tracking down server performance issues. When I get a call about performance on one of the servers where vendors have direct access to the databases, I'll run this query first. While our vendors are quite good on the engineering side, their queries are somewhat "lacking" ;-)

I hope it this helps or gives you some ideas.

Total article views: 7391 | Views in the last 30 days: 0
 
Related Articles
ARTICLE

Vendor Selection

Today's editorial was originally published on Apr 29, 2007. It is being re-run as Steve is at SQL Se...

FORUM

Server Connection Timeout

Server Connection

BLOG

Working with SQL Server Third Party Vendors

I ran across Denny Cherry’s (@MrDenny) blog on Fixing Vendor Performance Problems in which he outlin...

FORUM

Connect two servers

Connect two servers

FORUM

connect Database server

connect Database server

Tags
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones