Sunteți pe pagina 1din 3

Computer and Information Science (TIK 2O)

Network Programming Command Reference and Assignment

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:

put Net.LocalAddress % Outputs the TCP/IP address of the computer

2. Net.WaitForConnection (chatPort, netAddress)


Tells the computer to wait for (ie. to listen) for a connection to be made from another computer
using the port specified by the chatPort parameter (use 5055 for the chat port parameter). When a
program from another computer connects to the user's computer, this function returns a value
greater than 0. The value generated from this is usually assigned to a variable. The TCP/IP
address of the connecting computer is assigned in the netAddress string parameter. Study the
code example below:

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 */

3. Net.OpenConnection (netAddress, chatPort)


Attempts to open a connection to the computer specified by netAddress parameter using the port specified
by the port parameter. There must be a program listening for the connection to be made (ie. the user using
the other computer must have run the Net.WaitForConnection command). If successful, the
Net.OpenConnection function returns a value greater than 0 which is usually assigned to a variable. Study
the code example below:

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

4.a) Net.CharAvailable (netStream)


Same as Net.LineAvailable except it waits for a single character.

4.b) Net.TokenAvailable (netStream)


Same as Net.TokenAvailable except it waits for a single token.

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:

var sentenceSent : string % Stores the data typed by the user


if hasch = true then % Checks to see if a key has been pressed
get sentenceSend : * % Allows the user to input a sentence
put : netStream, sentenceSent /* Outputs the user’s sentence to the other computer. The :netStream tells the
computer the data is to be sent to the other computer instead of to the monitor*/
end if

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.

Your assignment will require the creation of two programs:


1) The client program which will make itself available to be connected to. It will then allow the
person using the program to type text to be sent to the other computer and will listen for and
display data received from the other computer. Save your program as Client.t
2) The server program which will connect to the client computer. It will then, like the client, allow
the person using the program to type text to be sent to the other computer and will listen for and
display data received from the other computer. Save your program as Server.t

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

S-ar putea să vă placă și