First, we have to introduce the basis for network communication before we can use the XLOGO primitives.
Two computers (or more) can communicate through a network if they both have ethernet cards. Each computer
is identified by a personal address called an IP address. This IP address consists of four integers, each between 0 and
255 and separated by a dot. For example, The IP address of the first computer in the illustration is
192.168.1.1
Because it’s not easy to remember these numbers, it’s also possible to identify each computer by a more usual name.
As can be seen in the illustration, we can communicate to the right computer with its IP address: 192.168.1.2, or
with its name: turtle
For the moment, I’ll add just one more thing. The local computer on which you are working is located by the
address: 127.0.0.1. Its general name is localhost. We will see this later in practice.
XLOGO has 4 primitives that allow it to communicate over a network: listentcp, executetcp, chattcp
and send. In all future examples, we will take the case of the two computers in the previous figure.
This primitive listentcp is the basis for all network communication. It doesn’t need an argument. When you
execute this primitive on a computer, the computer will listen for instructions sent from other computers on the
network.
this primitive allows execution of instructions by a computer on the network.
word1 is the called IP address or computer name, the list2 contains instructions to execute.
Example: I’m on computer hare, I want to draw a square with a side of 100 on the other computer. Thus,
on the computer turtle, I have to launch the command listentcp. Then, on the computer hare, I
write:
executetcp "192.168.1.2 [repeat 4[fd 100 rt 90]]
or
executetcp "turtle [repeat 4[fd 100 rt 90]]
Allows chat between two computers on a network. On each computer, it displays a chat window.
word1 is the called IP address or computer name, list2 contains the sentence to display.
Example: hare wants to talk with turtle.
First turtle executes listentcp so it is waiting for instructions from network computers. Then hare writes:
chattcp "192.168.1.2 [hello turtle].
Chat windows will open on both computers, allowing them to talk with each other.
Send data towards a computer on the network and return his answer.
word1 is the called IP address or computer name, list2 contains the data to send. When Xlogo is launched on the
other computer, il will answer OK. It is possible with this primitive to communicate with a robot through its
network interface. Then, the answer of the robot could be different.
Example: turtle wants to send to hare the sentence "3.14159 is quite pi".
First hare executes listentcp so it is waiting for the other computer to communicate. Then, turtle writes:
print sendtcp "hare [3.14159 is quite pi].
A little hint: Launch two instances of XLOGO on the same computer.
- In the first window, execute listentcp.
- In the second one, write executetcp "127.0.0.1 [fd 100 rt 90]
You can move the turtle in the other window! (heh, heh, it’s possible because 127.0.0.1 designates your local address, so
it’s your own computer...)