New topic: 

How to know a port is already listening?

<http://forums.realsoftware.com/viewtopic.php?t=22866>

       Page 1 of 1
   [ 4 posts ]                 Previous topic | Next topic         Author  
Message       gang           Post subject: How to know a port is already 
listening?Posted: Thu Jun 12, 2008 12:32 am                        
Joined: Wed Oct 24, 2007 2:31 am
Posts: 30              so we can avoid to listen on the same port ?
Thanks   
                            Top               npalardy           Post subject: 
Re: How to know a port is already listening?Posted: Thu Jun 12, 2008 1:04 am    
                    
Joined: Sat Dec 24, 2005 8:18 pm
Posts: 3542
Location: Canada, Alberta, Near Red Deer              Simply try to listen on 
the port
You get an error if something is already listening on it     
_________________
My web site Great White Software
RBLibrary.com REALbasic learning  
                            Top               roger           Post subject: Re: 
How to know a port is already listening?Posted: Thu Jun 12, 2008 4:54 pm        
                       
Joined: Tue Oct 25, 2005 1:57 pm
Posts: 191              npalardy wrote:Simply try to listen on the port
You get an error if something is already listening on it
I wouldn't be so sure. This may be true if another application is already 
listening on the port. But within the same application (RB) I found that 
multiple sockets can listen on the same port with no error. I just tried the 
following (I dragged 4 TCPSockets into a new window), on an XP Pro machine with 
RB2008r2.
Code:TCPSocket1.Port = 1024
TCPSocket2.Port = 1024

TCPSocket3.Address = "127.0.0.1"
TCPSocket3.Port = 1024
TCPSocket4.Address = "127.0.0.1"
TCPSocket4.Port = 1024

TCPSocket1.Listen
TCPSocket2.Listen

TCPSocket3.Connect
TCPSocket4.Connect

This creates 2 servers and 2 clients. The 'Connect' events of all 4 sockets 
fire when executing this code. None of the 'Error' events fire. Not only that. 
One of the clients connects to one socket while the other one connects to the 
other socket. I was able to send data between respective client and server 
sockets.
I'm not sure if this is intended behavior, and if it would behave the same on 
other platforms.   
                            Top               mechanoid           Post subject: 
Re: How to know a port is already listening?Posted: Tue Jul 22, 2008 7:39 am    
                    
Joined: Fri Sep 30, 2005 1:11 pm
Posts: 46              gang wrote:so we can avoid to listen on the same port ?
Thanks

This works on Windows.

Code:
Function portFree(pfPort As Integer) As Boolean
  
  // Taken from: http://www.boisseau.co.uk/code/  [ICMPSocket]
  
  #If TargetWin32 Then
  
  
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/bind_2.asp
  'http://www.vbip.com/winsock-api/listen-accept/listen-accept-01.asp
  'http://www.vbcity.com/forums/topic.asp?tid=28254
  
  Const AF_INET = 2
  Const SOCK_STREAM = 1
  Const IPPROTO_TCP = 6
  Const SOMAXCONN = &H7FFFFFFF
  
  Dim lngRetValue As Integer
  Dim bindResult As Integer
  Dim listenResult As Integer
  Dim closeResult As Integer
  Dim ret as Integer
  Dim iii AS Integer
  
  Declare Function inet_addr Lib "ws2_32.dll" ( addr as CString ) as Integer
  Declare Function inet_network Lib "ws2_32.dll" ( addr as CString ) as Integer
  Declare Function gethostbyname Lib "ws2_32.dll" ( addr as CString ) as Integer
  Declare Function htons Lib "ws2_32.dll" (hostshort As Integer) As Integer
  Declare Function socket Lib "ws2_32.dll" (af As Integer, s_type As Integer, 
Protocol As Integer) As Integer
  Declare Function bind Lib "ws2_32.dll" (s As Integer, name as Ptr, namelen As 
Integer) As Integer
  Declare Function listen Lib "ws2_32.dll" (s As Integer, backlog As Integer) 
As Integer
  Declare Function closesocket Lib "ws2_32.dll" (s As Integer) As Integer
  Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Integer
  
  Dim outAddr As MemoryBlock
  Dim hostent as MemoryBlock
  Dim sockaddr_in As New MemoryBlock(16) // Remember to use system endianness 
on this.  Win likes LITTLE endian.
  
  ret = inet_addr("127.0.0.1")
  
  If ret = -1 then
  // Problem with the address, maybe it's not an IP.
  // Try using gethostbyname to look it up from /etc/hosts or DNS
  
  hostent = NewMemoryBlock(4)
  hostent.Long(0) = gethostbyname("127.0.0.1")
  
  If hostent.Long(0) = 0 Then
    // Me.close
    Return False
  End If
  
  hostent = hostent.Ptr(0)
  
  If hostent = nil then
    // Me.Close
    Return False
  End If
  
  // The name lookup was successful.  Put the response into the sockaddr_in 
structure
  If TargetWin32 Then
    sockaddr_in.Short(0) = hostent.Short(2) // Address family
    sockaddr_in.Short(2) = htons(pfPort)
    sockaddr_in.Long(4) = hostent.Ptr( 4 ).Ptr( 0 ).Long( 0 ) // Actual address
  Else
    sockaddr_in.Short(0) = hostent.Long(8) // Address family
    sockaddr_in.Long(4) = hostent.Ptr( 16 ).Ptr( 0 ).Long( 0 ) // Actual address
  End If
  
  
  Else
  // We've got a valid IP.  Put it into a sockaddr_in structure:
  sockaddr_in.Short(0) = AF_INET // Address family
  sockaddr_in.Short(2) = htons(pfPort)
  sockaddr_in.Long(4) = ret
  end if
  
  outAddr = sockaddr_in
  
  lngRetValue = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
  
  bindResult = bind(lngRetValue, outAddr, outAddr.Size)
  
  // 10014
  // WSAEFAULT - Error 10014
  // Question/Problem: Bad Address.
  // Answer/Solution: The system detected an invalid address in attempting to 
use an argument of a call.
  // http://support.ipswitch.com/kb/WSK-19980714-EM04.htm
  // http://www.bookmark-master.com/socket-error-10014.html
  
  iii = WSAGetLastError()
  
  // Close the socket
  closeResult = closesocket(lngRetValue)
  
  If iii <> 0 Then
  
  Return False
  
  Else // iii = 0
  
  Return True
  
  End If
  
  #Else
  
  Return True
  
  #EndIf
End Function


   
                            Top           Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 4 posts ]     

-- 
Over 900 classes with 18000 functions in one REALbasic plug-in. 
The Monkeybread Software Realbasic Plugin v8.1. 

&lt;http://www.monkeybreadsoftware.de/realbasic/plugins.shtml&gt;

[email protected]

Reply via email to