|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38043 Always tries to connect to client on same outgoing port
Submitted: 2006-07-09 03:47 UTC Modified: 2006-07-09 07:56 UTC
From: aeolianmeson at blitzeclipse dot com Assigned:
Status: Not a bug Package: Sockets related
PHP Version: 5.1.4 OS: Fedora 4
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: aeolianmeson at blitzeclipse dot com
New email:
PHP Version: OS:


 [2006-07-09 03:47 UTC] aeolianmeson at blitzeclipse dot com
I am connecting to myself and then closing, to sniff out available ports on my system that I can use-- I'm sure you're acquainted with this method.

It should always produce a different port (nPort), since no connection is erected (because it's UDP), but it always returns that it is on the SAME port.

Dustin Oprea

Reproduce code:
$n = 10;
	$strIP = $nPort = -1;

	$Socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
	socket_connect($Socket, '', 8400);
	socket_getsockname($Socket, $strAddr, $nPort);

	print("> Available port: $strAddr:$nPort\n");

Expected result:
It produces ten lines of output, that should each contain one unique, available port number. 

Actual result:
Each line indicates the same port, even if a socket has been opened for listening on it.

The problem I am having, therefore, is that I sniff the next available port from this code, and set up a listener on it. I then do the same thing again in order to open another listener on a different port, but it always returns to me the same port number, and I can not not bind it, obviously.

Is this intended?


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2006-07-09 07:48 UTC]
And it is PHP problem because .?. ?
This is what getsockname() returns, socket_getsockname() is just a wrapper.
 [2006-07-09 07:56 UTC] aeolianmeson at blitzeclipse dot com
There have been problems in the past in the translation of system calls through PHP... I wasn't sure if that was the intended effect.

I've found that I can circumvent this inherent flaw, by opening up another socket before the first is closed. This will render a different port.

Is this then, the only way to find an available port with PHP?

PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Jan 24 21:03:34 2022 UTC