Tips   >   Internet   >   TCP

TCP

Omnis has a whole group of TCP commands which you can use.

TCPAccept
TCPAddr2Name
TCPBind
TCPBlock
TCPClose
TCPConnect
TCPGetMyAddr
TCPGetMyPort
TCPGetRemoteAddr
TCPListen
TCPName2Addr
TCPPing
TCPReceive
TCPSend
TCPSocket

You can look up information on each of these command in the F1 Help.

This section contains tips, demos, and sample code on some of the Omnis TCP commands, things you can do, and things you want to watch out for.

TCPName2Addr

Use the TCPName2Addr Omnis command to convert a domain name to an IP address.

TCPName2Addr (hostname) Returns address

; Note: IPAddr will be an error code number if the hostname could not be looked up.

; Test with the vencor.ca domain.
Calculate HostName as 'vencor.ca'

TCPName2Addr (HostName) Returns IPAddr
If isnumber(IPAddr)
   OK message TCPName2Addr (Icon) {Unable to get a valid IP address for the host name '[HostName]'}
Else
   OK message TCPName2Addr (Icon) {The IP address for the host name '[HostName]' is '[IPAddr]'}
End If

Note: This command fails if the address of a Domain Name Server has not been defined in your computer. Not all host IP Addresses may be known to the Domain Name Server. If the Domain Name Server is busy or unavailable, the command times out and returns an error. Defining often-used servers to a local hostŐs file or using a caching Domain Name Server increases performance of this command.

I tested to see what this method does if you feed it an IP address rather than a domain name. If the IP address is a valid format, the method simply returns the IP address. If the IP address is not a valid format, e.g. one of the numbers is greater than 255, the method returns and error.

; Test with the IP address of the vencor.ca domain from the previous code snip.
Calculate HostName as IPAddr

TCPName2Addr (HostName) Returns IPAddr
If isnumber(IPAddr)
   OK message TCPName2Addr (Icon) {Unable to get a valid IP address for the host name '[HostName]'}
Else
   OK message TCPName2Addr (Icon) {The IP address for the host name '[HostName]' is '[IPAddr]'}
End If


; Test with an valid IP address that likely doesn't exist.
Calculate HostName as '192.255.254.253'

TCPName2Addr (HostName) Returns IPAddr
If isnumber(IPAddr)
   OK message TCPName2Addr (Icon) {Unable to get a valid IP address for the host name '[HostName]'}
Else
   OK message TCPName2Addr (Icon) {The IP address for the host name '[HostName]' is '[IPAddr]'}
End If


; Test with an invalid IP address.
Calculate HostName as '192.168.1.258'

TCPName2Addr (HostName) Returns IPAddr
If isnumber(IPAddr)
   OK message TCPName2Addr (Icon) {Unable to get a valid IP address for the host name '[HostName]'}
Else
   OK message TCPName2Addr (Icon) {The IP address for the host name '[HostName]' is '[IPAddr]'}
End If

Click the Run Demo button in the StudioTips Browser window to run the sample code.

You can use the TCPAddr2Name Omnis command to convert an IP address to a domain name. I have not always been successful in converting a valid IP address to the domain name.

TCPPing

Use the TCPPing Omnis command to ping a server.

TCPPing (hostname[,size,timeout]) Returns milliseconds

; Test with the vencor.ca domain.
Calculate HostName as 'vencor.ca'

; (Hostname[,Size,Timeout])
TCPPing (HostName) Returns Milliseconds
If Milliseconds<0
   OK message TCPPing (Icon) {TCPPing to [HostName] failed.}
Else
   OK message TCPPing (Icon) {TCPPing to [HostName] succeeded.}
End If

; Test with an invalid domain name.
Calculate HostName as 'vencor.xyz'

TCPPing (HostName) Returns Milliseconds
If Milliseconds<0
   OK message TCPPing (Icon) {TCPPing to [HostName] failed.}
Else
   OK message TCPPing (Icon) {TCPPing to [HostName] succeeded.}
End If

The problem I have run into with using TCPPing is that numerous servers ignore TCP ping on purpose. e.g. Try pinging apple.com

You can not assume that a server is going to respond when you send it a TCPPing.

I was using TCPPing to test communication with a host before attempting to do an SMTPSend, and FTPConnect, or $logon to a database.

If the client computer is not connected to their local area network or the network is not connected to the internet a lot of time can be wasted attempting to send an email or $logon to a database. To test for a connection to the internet you could use a server which you know responds to TCPPing, and in case that server happens to be down, you could try a series of 2 or 3 servers which you know to respond to TCPPing.

; Test with the a host server that we know does not respond to TCPPing.
Calculate HostName as 'apple.com'

; (Hostname[,Size,Timeout])
; Cut the default size and timeout to reduce the TCPPing wait time.
Calculate Size as 256/4
Calculate Timeout as 3000/10
TCPPing (HostName,Size,Timeout) Returns Milliseconds
If Milliseconds<0
   
   ; Try another known domain.
   Calculate HostName as 'scsinternet.com'
   TCPPing (HostName,Size,Timeout) Returns Milliseconds
   If Milliseconds<0
      
      ; Test with the vencor.ca domain.
      Calculate HostName as 'vencor.ca'
      TCPPing (HostName,Size,Timeout) Returns Milliseconds
      
   End If
End If
If Milliseconds<0
   OK message TCPPing (Icon) {TCPPing to [HostName] failed.}
Else
   OK message TCPPing (Icon) {TCPPing to [HostName] succeeded.}
End If

Click the Run Demo button in the StudioTips Browser window to run the sample code.

Whois

You can use the TCP commands to get whois information on a domain.

; Samplecode to query the whois database with omnis
; based on the examples in the Omnis helpfile RRG / 08/03/14

; the whois server : more here : http://www.addspice2life.com/2008/02/whois-servers-list.html or http://www.mit.edu/afs/athena/contrib/potluck/Net-Services/whois-servers.list
; internic.net
; http://www.netdemon.net/tutorials/whois.txt
; http://www.iana.org/domains/root/db/index.html

Calculate WhoisDomain as 'whois.internic.net'
Calculate Port as 43 ;; Port for the whois server
Calculate TestDomain as 'omniscentral.com'
Calculate SendMssg as con(TestDomain,kCr,kLf) ;; append querystring for whois server

TCPConnect (WhoisDomain,Port) Returns Socket
If Socket<0
   OK message {Unable to TCPConnect with '[WhoisDomain]' on port [Port]}
Else
   
   ; Connected
   TCPSend (Socket,SendMssg) Returns CharCount
   TCPReceive (Socket,ReceiveMssg)
   TCPClose (Socket)
   
   Calculate ShortMssg as mid(ReceiveMssg,pos('Domain Name:',ReceiveMssg))
   
   OK message {Whois result for '[TestDomain]' is////[ShortMssg]}
   
End If