Networking




Connection between 2 computers to exchange messages.

Consists of






Applications

Some uses of networking, in relation to computer graphics, are:




Networking Multiple Hosts

Communication between more than 2 hosts normally involves many pair-wise connections.


Peer-to-peer model






Networking Multiple Hosts




Server-based model


Server can be a simple reflector, a database, or a whole simulation in itself






Sockets

An Internet Protocol (IP) network connection uses sockets.

A socket connection involves:



Programming libraries can look up the IP address from a host name (e.g. www.buffalo.edu).

The port number and protocol are normally decided on in advance for a particular application.

(It is also possible to use dynamically generated port numbers.)






Network Data

A network connection allows one computer to send bytes of data to, or receive bytes of data from, another computer.

Everything beyond the exchange of raw data is higher-level interpretation of the data, handled by the application or a library.

Note that different computers can interpret a stream of bytes differently, depending on things such as "native byte ordering".

Functions to convert to & from "network byte order" help avoid problems.

Sending everything as text strings is an easy alternative.






UDP vs TCP

The two major protocols are TCP/IP and UDP/IP

TCP/IP:

UDP/IP:

For applications like real-time graphics, UDP is often preferred for its speed and simplicity. But a mix of TCP and UDP can also be useful.






Python Sockets

Sockets are implemented in the socket module.

Main functions for UDP:

s = socket(AF_INET, SOCK_DGRAM) Create a UDP socket
s.bind(('',portnum)) Listen to a specific port
s.sendto(data, (host, portnum)) Send data to remote host
data, addr = s.recvfrom(maxPacketSize) Receive data; return value is tuple of data and sender
s.setblocking(0) Make the socket non-blocking. Trying to receive when no data is available will generate an exception.





Examples






Web-based Networking

Web tools such as PHP, Shockwave, and Python CGI scripts can do socket-based networking.

This can be used to communicate from a web page to a graphical program.

Example: send.php






Web-based Networking

Python's urllib or urllib2 can be used to fetch web data.

e.g. textures or sounds

    try:
        webdata = urllib2.urlopen(url).read()
    except:
        return None
    webdataAsFile = StringIO.StringIO(webdata)
    tex = Texture2D(webdataAsFile)

Example: texURL.py



Creative Commons License
This document is by Dave Pape, and is released under a Creative Commons License.