Documente Academic
Documente Profesional
Documente Cultură
The following commands can be used in programs which allow users on two different computers
to communicate with each other over a TCP/IP network.
1. Net.LocalAddress
This function returns the TCP/IP numeric address of the computer the program is running on.
The numeric address is of the form xxx.yyy.zzz.www where each 3 digit segment is a number
from 0 to 255. This number can be outputted or assigned to a variable. For example:
var netAddress : string /* Declares a string variable to store the TCP/IP address of the computer that
will be connected to */
var chatPort := 5055 % Declares an integer variable and assigns 5055 to it. This is the chat port
var netStream :int /* Stores an integer which identifies the network “stream” or path connecting the two computers */
netStream:= Net.W aitForConnection (chatPort, netAddress) /* Waits for a connection to be made from another
computer . The TCP/IP address of the connecting
computer is assigned to the netAddress parameter */
var netStream : int % Variable which identifies the "chat stream". Value >0 if a connection is made
var netAddress : string % TCP/IP address of the computer to connect to
var chatPort := 5055 % Declares an integer variable and assigns 5055 to it. This is the chat port.
put "Enter the TCP/IP address to connect to"
get netAddress
netStream := Net.OpenConnection (netAddress, chatPort) /* Attempts to connect to the computer. Note the other
computer must have run the Net.WaitForConnection
command.*/
if netStream <= 0 then
put "Unable to connect to ", netAddress
else
put "Connected to ", netAddress
end if
4. Net.LineAvailable (netStream)
This function tells the program that there is a line of data sent from the other computer. It is known as a
boolean function because it can only have two possible values - true or false. It returns true as soon as
data is sent from your partner’s computer. The computer you are connected to is identified using the
netStream parameter. Remember that this was assigned when the Net.OpenConnection or
Net.WaitForConnection functions. Study the code example below:
var sentenceReceive : string % Stores the data received from the other computer.
if Net.LineAvailable (netStream) = true then /* Checks to see if there is a line of text waiting to be sent from the other
computer */
get : netStream, sentenceReceive:* /* Remember the purpose of the get command. The :netStream tells the
computer the data is received from the other computer instead of from
the keyboard. */
put sentenceReceive % Outputs the data from the other computer on the monitor
end if
5. hasch
The “has character” function. This is boolean function returns true if the user has typed a character into
the keyboard. This function is used to check if the user has entered data into the computer using the
keyboard. Study the code example below:
Basic Assignment
1. You and a partner are to create the software to allow two people to communicate with each other using
the network. Save this software in folder called CHAT in your folder. Note that both partners must save
all the required files in their folder.
Hint 1: Use the code examples described in the previous section as a basis for your programs.
Hint 2: You will require a loop to have the computer continually “listening” for information sent from
the other computer.
2. Differentiate between the messages you send with the ones you receive using colours or by placing a
character as a prompt before the text (ie. >> for data received and > as a prompt for the user). Re-
save your programs as Client.t and Server.t
Advanced Assignments
1. Improve your Keys3.t program so that the user is given the choice of running the program from the
computer the program is loaded on or from a different computer. If they choose to run it from a
different computer, allow the spaceship to be run over the network. Save your program as
AdvancedNetwork1.t
2. Improve your Collision4.t program so that one of the objects is controlled from a different computer.
Give the user the choice of which object is to be controlled from the different computer. Make sure the
results of the game are displayed on both computers. Save your program as AdvancedNetwork2.t