• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to find out if a TCP port is in use?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to find out if a TCP port is in use?


  • Subject: Re: How to find out if a TCP port is in use?
  • From: Julian <email@hidden>
  • Date: Fri, 17 Mar 2006 19:16:00 -0300

Hi Stephan!

Here is an example of use:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>

typedef struct sockaddr *sad; /* A necesary dummy typedef */

#define PORT 80

int main()
{
	int sock; /* Socket that will be bind */
	struct sockaddr_in sin; /* Address Structure */

	/* Create the socket */
	/* PF_INET is the option for make a TCP socket.
		 You can try "man socket" for more info */
	sock = socket( PF_INET, SOCK_STREAM, 0 );

	/* The socket creation failed */
	if ( 0 > sock ) {
		perror( "socket" );
		return ( -1 );
	}

	/* Address */
	sin.sin_family = AF_INET;
	sin.sin_port = htons( PORT ); /* htons() convert the number
                                                      to big endian */
	sin.sin_addr.s_addr = INADDR_ANY;

	/* We bind the socket to the port PORT to check if
           the port is in use */
	if ( 0 > bind( sock, (sad)&sin, sizeof( sin ) ) ) {
		/* Bind failed, now we can check if the address is
                 in use */

		if ( EADDRINUSE == errno ) {
			/* We put the code necesary to manage this case */
			printf( "The TCP port %d is in use.\n", PORT );
		}

		else
		/* If the error were other than EADDRINUSE, we print it */
		perror( "bind" );
	}

	/* If we arrive to this point, the port weren't in use and
          we have it attached to our program. We can close
          the socket or use it */
	close( sock ); /* Close the socket */

	return 0;
}

Note that if you run this program without root privilegies, you can
not check if a port < 1024 is in use.

I hope it can help you. If you have any question about it, please, writeme.

Regards, Julian


On 3/17/06, Finlay Dobbie <email@hidden> wrote:
> On 17/03/06, Stephan Ruggiero <email@hidden> wrote:
> > Hi,
> > thank you, I think this should do the trick.
> >
> > lsof -i :abc
> > gives me every process listening on or connected to the port abc.
> >
> > If other user`s processes are needed, you can just run lsof with sudo.
>
> Well, I wouldn't use this approach if you want to determine this
> programmatically.
>
>  -- Finlay
>  _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list      (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>


--
A QuBoy can't clone himself, but he can teleport his mind anywhere!
Para no ser un recuerdo, hay que ser un reloco.
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >How to find out if a TCP port is in use? (From: Stephan Ruggiero <email@hidden>)
 >Re: How to find out if a TCP port is in use? (From: Stephan Ruggiero <email@hidden>)
 >Re: How to find out if a TCP port is in use? (From: Stephan Ruggiero <email@hidden>)
 >Re: How to find out if a TCP port is in use? (From: "Finlay Dobbie" <email@hidden>)

  • Prev by Date: outline view using NSTreeController -- expand all items
  • Next by Date: NSTextView - firstResponder notification?
  • Previous by thread: Re: How to find out if a TCP port is in use?
  • Next by thread: Re: How to find out if a TCP port is in use?
  • Index(es):
    • Date
    • Thread