Vizard 8 » Reference » Networking » Networking basics
8.0

Networking Basics

Vizard's networking features enable sending data between computers.

 

Some applications of networking include:

Creating a network connection

To create a network connection to another computer, use the viz.addNetwork command. The command takes the name of the remote computer or the IP address as the argument (e.g., shark.ocean.edu, or 192.168.0.1). The following code will create a network object which will send data to the computer 'shark.ocean.edu':

myNetwork = viz.addNetwork('shark.ocean.edu')

Sending data

To send data over a network connection to a Vizard/Python application use the <network>.send() command. The send command takes any number of position or keyword arguments. Vizard serializes the arguments to a string and sends that to the remote computer. To serialize the data, Vizard uses the cPickle library that comes with Python. Any object that can be used with the cPickle library can be sent over the network. This means almost any type of data can be sent to the other computer.

 

Here is an example that sends both positional and keyword data over the network connection:

myNetwork.send(5,'hello',[1,2,3],foo=78,bar=['more','data'])

To send data to a non-Python application use the <network>.sendRaw command. This command takes a single string value that is sent as raw byte data to the network object:

myNetwork.sendRaw('Some raw data')

By default, Vizard sends/receives network data over UDP port 4950. You can change the port of a network object using the <network>.port() command. For example, to send data over port 4960, you would use the following code:

myNetwork.port(4960)

Receiving data

When a network message is received Vizard will trigger the viz.NETWORK_EVENT. In order to handle the data you simply need to register a callback for this event. Depending on how the data was sent, the callback function will be passed either a viz.NetworkEvent or viz.RawNetworkEvent object.

 

A viz.NetworkEvent object will be passed to the callback function if the data was:

The following code receives data sent from the previous myNetwork.send example:

def onNetwork(e):
    print('** Received message from',e.sender)
    print( e.data    )#Prints: (5, 'hello', [1, 2, 3])
    print( e.foo     )#Prints: 78
    print( e.bar     )#Prints: ['more', 'data']
   
viz.callback(viz.NETWORK_EVENT,onNetwork)

The viz.NetworkEvent object contains an attribute called data, which is a tuple containing all of the position arguments in the order they were passed. All the keyword arguments are transformed into direct attributes of the network object. In our send example, the keyword arguments foo and bar were used to send data. In our receiving example, we were able to directly access these values as attributes of the network event object.

 

A viz.RawNetworkEvent object will be passed to the callback function when a raw byte network packet is received. This is useful for receiving data from non-Python based applications. The raw_data attribute of the event structure is a Python bytes object that contains the data:

def onNetwork(e):
    if isinstance(e,viz.RawNetworkEvent):
        print(e.raw_data)

viz.callback(viz.NETWORK_EVENT,onNetwork)

In addition to data attributes both network event objects include information about the sending computer and the receiving port:

Event

Attributes

viz.NetworkEvent

sender: The name of the computer that sent the message.

address: The IP address of the computer that sent the message.

port: The port number the message was received on.

viz.RawNetworkEvent

address: The IP address of the computer that sent the message.

port: The port number the message was received on.

As mentioned previously, Vizard sends/receives network data over UDP port 4950 by default. You can tell Vizard to receive data on other ports as well. For example, if you want to also receive data over port 4960 you can use the following code:

viz.net.addPort(4960)

If you ONLY want to receive data on port 4960, then you can tell Vizard to stop receiving data on the default port of 4950 by using the following code:

viz.net.removePort(4950)

See also

In this section:

viznet

Multi User Environment

Networking Command Table

Cluster basics

Other sections:

Tutorial: Network communication

Example scripts:

Robo chase

Instant messenger