Page 1 of 1

Connect to Server Application

Posted: Sat Dec 06, 2014 3:23 am
by IdeasVacuum
I am trying to connect to an application (ZW3D CAD-CAM) which has a 'Server Mode'

Code: Select all

EnableExplicit

If(InitNetwork() = 0)

        MessageRequester("Problem", "Network init failed", #PB_MessageRequester_Ok)
        End
EndIf

ExamineIPAddresses(#PB_Network_IPv4)
              
Global    igConnectID.i
Global      igLocalIP.i = NextIPAddress()
Global      sgLocalIP.s = IPString(igLocalIP, #PB_Network_IPv4)
Global         sgHost.s = Hostname()
Global          sgMsg.s
Global igZwListenPort.i = 8000
Global   igZwSendPort.i = 9000
Global      igTimeOut.i = 20000

          igConnectID = OpenNetworkConnection(sgHost, igZwListenPort, igTimeOut, #PB_Network_TCP|#PB_Network_IPv4, sgLocalIP, igZwSendPort)
   If Not(igConnectID = 0)

               SendNetworkString(igConnectID, "zw3dremote -r local -f C:\TestFile.Z3)", #PB_Ascii)
          CloseNetworkConnection(igConnectID)
   Else
          MessageRequester("Problem", "Connection to Server failed", #PB_MessageRequester_Ok)
   EndIf

sgMsg = "IP: " + sgLocalIP + #CRLF$
sgMsg = sgMsg + "Host: " + sgHost + #CRLF$

MessageRequester("Info", sgMsg, #PB_MessageRequester_Ok)

End
...connection fails. I have verified that all vars are correct and Port 8000 is in Listening Mode when ZW3D is running.

From the ZW3D Help:
About ZW3D Server Mode
ZW3D contains the communications code to act as a server (referred to as ZW3D Server Mode). When ZW3D starts it can create a second communications thread that listens for external commands. The communication is done through TCP/IP sockets and is platform independent.
The ZW3D startup program "zw3d.exe" has the client side built into it. Commands can be sent to ZW3D by using "zw3dremote -r hostname command". This will work across any TCP/IP network. However, zw3d.exe is not required.
The communications client can be written in any language or environment that supports sockets. It is possible to send command line switches, macro commands, and regular ZW3D commands (~xxx, !xxx, $xxx, etc) through this interface. When sending a macro command, data is returned to the client.

Executing ZW3D Server Mode Commands
Running "zw3dremote -r local command" assumes that the command is a ZW3D command that starts with either '~', '!', '$', '&'. If it does not start with any of these symbols then a '~' will automatically be added to the beginning of the command.
To specify additional command line arguments use "zw3dremote -r local -cmd command -xxx ..." This is necessary in order to identify the command correctly among the various command line arguments. To run a macro use "zw3dremote -r local -macro macro_file". To run just a single macro command use "zw3d -r local macro=xxx". To load a file in a running instance of ZW3D use "zw3dremote -r local -f file.Z3".

ZW3D Server Mode Communications
By default ZW3D uses port 8000 to listen and port 9000 to respond. The port numbers can be set by the user. To set the ports use "zw3d -port 3000:4000". This will start ZW3D and ZW3D will listen on port 3000 and respond on port 4000. To send a command to this instance of ZW3D use "zw3dremote -r hostname:3000:4000 command". The hostname argument can be the word "local", a computer name as it is recognized on the network, or an IP address such as 192.168.1.1. If the word "local" is used for the hostname, a new instance of ZW3D will be started if it is unable to connect to an already running instance, or if no instances are currently running.

Running ZW3D in Server Mode
By default ZW3D starts in the server mode. To send a command to a copy of ZW3D that is already running on the local machine use "zw3dremote -r local command". If ZW3D is not running, a new instance of ZW3D will be executed. The server mode can be controlled while running ZW3D. Use "~CdServerStart" to start the server mode, "~CdServerStop" to stop the server mode, and "~CdServerReset" to reset and reinitialize the server mode. To get help on the ZW3D command line switches use "zw3dremote -h":

zw3dremote command line options:

/CNSCPRT Use console to print communications information
/F Load and activate file_name
(enclose file_name argument in matching single or double-quote
to handle spaces in filename)
/MACRO Load and run macro_name
(enclose macro_name argument in matching single or double-quote
to handle spaces in filename)
/NOSTDOUT Do not send output to STDOUT
/OUT file Write return data from a remote command to file
/REMOTE Send a command to an already running instance of ZW3D
/R Same as /REMOTE
/WAIT Wait for a command from a running instance of ZW3D
/W Same as /WAIT

Command syntax for /R and /REMOTE options:
zw3dremote /R hostname[:p] /out file command
hostname is the name of the computer that is running ZW3D
p is the optional port number to connect to (default = 8000)
command is the command to be executed by ZW3D
file will contain return data for command after the command is finished

Command syntax for /W and /WAIT options:
zw3dremote /WAIT p
p is the port number to wait for input on
Can anyone spot an error?
Edit: Add msg re IP and Hostname

Re: Connect to Server Application

Posted: Sat Dec 06, 2014 5:03 am
by TassyJim
I know they talk about TCP but when the say "By default ZW3D uses port 8000 to listen and port 9000 to respond", it sounds like UDP to me.

TCP wouldn't use different ports for each direction.

If you have zw3d.exe, I would fire up WireShark and look at a working connection.

Jim

Re: Connect to Server Application

Posted: Sat Dec 06, 2014 6:56 am
by IdeasVacuum
Phew! WireShark is not for the faint hearted.......... completely baffling interface for the network novice :shock:

Using netstat.exe:
Proto____Local Address_____Foreign Address_____State________PID
TCP_____MAIN:8000________MAIN:0____________LISTENING___4924
[zw3d.exe]

Re: Connect to Server Application

Posted: Sat Dec 06, 2014 7:45 am
by TassyJim
Networking is not my most comfortable area either.

I would try

Code: Select all

 igConnectID = OpenNetworkConnection(sgHost, igZwListenPort, igTimeOut, #PB_Network_TCP|#PB_Network_IPv4)
That should have you sending but not receiving.

I don't have much faith in igLocalIP.i being a valid value.

Jim

Re: Connect to Server Application

Posted: Sat Dec 06, 2014 3:09 pm
by IdeasVacuum
Yeah, when I debug igLocalIP.i it is my IP address, but the fact that the command enumerates addresses is a concern - if there is more than one valid IP, how would you know which should be used in this case?

I tried the send-only version of OpenNetworkConnection() originally (and again just now). igConnectID = 0 every time :?

Re: Connect to Server Application

Posted: Sat Dec 06, 2014 3:22 pm
by IdeasVacuum
Hmm, something I didn't notice in the ZW3D notes before:
ZW3D contains the communications code to act as a server
The ZW3D startup program "zw3d.exe" has the client side built into it
Confused dot com

Re: Connect to Server Application

Posted: Sat Dec 06, 2014 3:27 pm
by Thorium
I understand the documentation like that:
zw3dremote is a program you execute, which will connect to the server and send a command specified in the parameters. It's probably not a string you send to the server.

Re: Connect to Server Application

Posted: Sat Dec 06, 2014 4:37 pm
by IdeasVacuum
Hi Thorium

Just checked the ZW3D Program Files folder and you are right : zw3dremote is a separate exe.

Re: Connect to Server Application

Posted: Sat Dec 06, 2014 6:06 pm
by IdeasVacuum
Well, actually successfully executed the command in ZW3D via RunProgram(zw3dRemote.....)
...however, some commands will return information, so that's the next test :D

Re: Connect to Server Application

Posted: Sat Dec 06, 2014 9:33 pm
by IdeasVacuum
Pretty well there, sending and receiving via RunProgram(). Many thanks for your help guys.

Re: Connect to Server Application

Posted: Mon Dec 15, 2014 3:03 am
by RichAlgeni
Sorry I'm chiming in late, but I've been in sunny Michigan at a client site.

Whenever working with network programming, you can always use the Windows Telnet command to make sure a Server process is available for your client process. Telnet is a network protocol that will connect and send data just like your client socket connection would. Even 8 bit characters can be sent, albeit with some keyboard work. Making sure your server is available is at least half the battle!

Beyond that, communication is often bidirectional on one socket, but I have seen instances when two sockets are used. One for each way. If you do this, you will most likely need a server and a client on each end.