By John Blankenship To post comments on this article and find any associated files and/or downloads,
go to www.servomagazine.com/index.php/magazine/issue/2018/09.
The server must be running before any client tries to initiate
communication, so let’s start by looking at a very simple
server program (Figure 1).
The program starts by using an internal function to print
the server’s local IP address. It also displays the port number
used (it must be a port not being used by any other program
or computer on your LAN). The program then initializes
the server. Note that only the port number is required for
initialization because the IP address of the computer will
automatically be used.
After a short pause, the status of the server is printed. If
everything went well, the status should be listening.
The server program then tells the user to enter the
server’s IP and port number into the client program before
running it. These actions are shown in the screenshot in
Figure 2. The program then enters an endless loop that
waits for messages to come from a client.
print “Server’s IP: “,TCP_localIP()
localPort = 42001
print “Server’s Port: “,localPort
print tcps_Status() // should be listening
print “Enter the server’s IP and Port into”
print “the client program, then run it.”
// wait for message and send back GOT IT
rBuff = tcps_Read()
When one arrives, it is displayed and the message “Got
It” is sent back to the client socket. As you can see, setting
up a simple server is really easy if your chosen language has
the necessary functions.
If connected, another subroutine will be called to handle the
Setting Up the Client
Setting up the client program is almost as easy as
shown by the program in Figure 3. The Main program
establishes the values for the remote IP and port numbers
The Communicate WithServer routine handles everything
once the connection has been made. In this simple example,
the subroutine lets the user enter the text for the message
to be sent to the server. After sending the text, it waits for a
reply and displays it. An endless loop maintains this process.
(the address of the server). A subroutine is then called to
connect with the server.
Connecting to the server is handled by an internal
function that requires the IP and port number being
used by the server. In this example, the client waits for a
maximum of 5,000 µs until an error or connection occurs.
Make your machine move
MICRO LINEAR SERVOS
· 10mm-300mm stroke
· 25kg+ available force
· 6v-12v power supply
· 15g-100g net weight
SERVO 09/10.2018 65