*Network*

Back to the start
Back to the libraries overview


Overview (Network)
Accept (Network Server)
HostName (Network Server)
New1 (Network Server)
New2 (Network Server)
Pending (Network Server)
Start (Network Server)
Stop (Network Server)
Close (Network Client)
Connect (Network Client)
DataAvailable (Network Client)
GetIP (Network Client)
GetIP2 (Network Client)
GetStream (Network Client)
New1 (Network Client)
Value (Network Client)

Overview (Network) Top

The Network library allows two or more computers (desktops or devices) to communicate over a network.
It uses the TCP protocol.
One computer is the server and the other computers are the clients.
The communication is a synchronous communication, which means that the program will block until the request completes.
The Network library includes two objects: Server and Client objects.
The Server object listens for incoming connections. When a client is waiting for a connection, calling Server.Accept will create a connection.
Server.Accept also creates a Client object. The communication is done using the two (or more) Client objects.
The Client object connects to a server using the Connect method.
This method requires the IP and port number of the server.
You can use GetIP method to find the IP of a known host.
When a connection is created, the data I/O operations are done with the Client.GetStream method which creates a new BinaryFile object (stream).
Using the BinaryFile object you read and write to the stream like a file stream.
One difference is that on a network stream each operation will be blocked until it is completed.
For example if you send a byte using BinaryFile.WriteByte this operation will wait until the other side reads this byte using BinaryFile.ReadByte.
Activesync can be used as the network provider. However the device must be the client and the desktop must wait for the connection before the device tries to connect.
The host name of the desktop when using Activesync is PPP_PEER.
Two important properties are available to prevent the program from blocking:
Server.Pending which returns true if a client is waiting for the connection,
and Client.DataAvailable which returns true if there is any data in the stream.


Another, more complex example is available on Basic4ppc site.
Example:
'****** Server side *******
'stream is a BinaryFile object, server is a Server object and client is a Client object.

Sub Globals

End Sub

Sub App_Start
server.New1(50000) 'Listens on port 50000 (all ip's available).
server.Start
client.New1
client.Value = server.Accept 'This line will block waiting for the other client to connect (or timeout).
msgbox("Server connected.")
stream.New1(client.GetStream,false) 'Creates a BinaryFile object using the GetStream method.
stream.WriteString("This message will be sent to the client.")
End Sub


'******Client side*******
'stream is a BinaryFile object and client is a Client object.
'If you are using Activesync to make the connection then the device should be the client and the server code should be running on the desktop before running the client code.
Sub Globals

End Sub

Sub App_Start
client.New1
client.Connect(client.GetIP2("PPP_PEER"),50000) 'Use Activesync IP.
msgbox("Client connected.")
stream.New1(client.GetStream,false)
msgbox(stream.ReadString)
client.Close
End Sub

Accept (Network Server) Top

Accepts a waiting connection and returns a new Client object.
This method will block until a connection is made.
You can check the Pending property for waiting connections.
Syntax: Accept

Example:
client.Value = server.Accept


HostName (Network Server) Top

Returns the name of the server.
You can use this name from the client to find the server's IP address.
Syntax: HostName


New1 (Network Server) Top

Initializes a Server object.
The server will listen on all available IP addresses on the specific port.
Syntax: New1 (Port As Int32)

Example:
server.New1 (50000)


New2 (Network Server) Top

Initializes a Server object.
The server will listen to the specific IP address and the specific port.
Syntax: New2 (IP As String, Port As Int32)

Example:
server.New2 ("192.168.1.100",50000)


Pending (Network Server) Top

Returns true if one or more clients are waiting for a connection.
Using the Pending property you could avoid the server application from blocking while waiting for the connection.
Syntax: Pending

Example:
Sub App_Start
server.New1(50000)
server.Start
Timer1.Enabled = true
End Sub

Sub Timer1_Tick
if server.Pending = true then
Timer1.Enabled = false
client.Value = server.Accept
... 'Do the communication here
end if
End Sub


Start (Network Server) Top

Starts listening for incoming connections.
Use Stop to stop listening.
Syntax: Start


Stop (Network Server) Top

Stops listening for connections.
Syntax: Stop


Close (Network Client) Top

Closes the connection.
Syntax: Close


Connect (Network Client) Top

Connects to a server.
This method will block until the connection is established.
Syntax: Connect (IP As String, Port As Int32)

IP - Formatted as "xxx.xxx.xxx.xxx"
Port - The same port number the server listens to.
You can find the IP address of a host using GetIP method.

Example:
Client.Connect ("192.168.1.100",50000)


DataAvailable (Network Client) Top

Returns true if there is data waiting on the stream.
You can use this method to avoid blocking when reading from the stream.
Syntax: DataAvailable

Example:
If client.DataAvailable = true then
stream.ReadBytes( ...
End If


GetIP (Network Client) Top

Returns an array of strings representing all the IP addresses of the specified host.
Syntax: GetIP (Host As String) As String()

Example:
'Show all available IP addresses.
Dim IP(0) As String
IP() = Client ("NETWORKNAME")
For i = 0 To ArrayLen (IP()) - 1
Msgbox (IP (i))
Next


GetIP2 (Network Client) Top

Returns the first IP address of the host.
GetIP2 is useful when there is only one address.
Syntax: GetIP2 (Host As String) As String

Example:
client.Connect (client.GetIP2 ("PPP_PEER"), 50000) 'PPP_PEER is the host when working with Activesync.


GetStream (Network Client) Top

Returns the stream (a BinaryFile object) that allows sending and receiving data from a network communication.
Syntax: GetStream

Example:
binary.New1 (client.GetStream, false)

New1 (Network Client) Top

Initializes a Client object.
Syntax: New1


Value (Network Client) Top

Gets or sets a reference to the Client object.
Syntax: Value
The Value property is useful to get a Client object from the Server object after the connection establishment.

Example:
client.New1
client.Value = server.Accept