A network is a collection of computers connected together somehow.
Example: ethernet, wireless, modem, wide area networks

Example: the hardware address of my laptop is 00:30:65:01:02:54
Example: the internet

Example: the IP address of my laptop is 130.194.225.100
Example: a packet sent from mandarin.csse.monash.edu.au to www.anu.edu.au passes through six gateways in approximately 50 milliseconds.
# traceroute www.anu.edu.au traceroute to www.anu.edu.au (150.203.99.8), 30 hops max, 38 byte packets 1 caul-gw.monash.edu.au (130.194.227.254) 4.219 ms 3.191 ms 3.243 ms 2 monash-gw-28.net.monash.edu.au (130.194.28.1) 4.516 ms 4.043 ms 4.250 ms 3 vic-gw.vrn.edu.au (203.21.130.33) 13.524 ms 19.739 ms 20.501 ms 4 vic-act.atm.net.aarnet.edu.au (192.12.76.34) 164.509 ms 168.172 ms 184.508 ms 5 anu-anuhub.carno.net.au (203.22.212.34) 207.311 ms 130.980 ms 131.975 ms 6 hanhuba.anu.edu.au (150.203.205.3) 154.367 ms 137.398 ms 146.712 ms 7 www.anu.edu.au (150.203.99.8) 161.976 ms 184.624 ms 187.591 ms
Examples: mandarin.csse.monash.edu.au, www.monash.edu.au
The vast majority of network applications today use TCP/IP.
Examples: HTTP (Hyper-Text Transfer Protocol), FTP (File Tranfer Protocol), SSH (Secure SHell)
| Application (eg HTTP) |
| Transfer (TCP) |
| Network (IP) |
| Data Link (eg ethernet) |
(this is not a stack in the sense of something you can push things onto and pop things off of!)
Network applications can usually be divided into two different types: servers and clients.
Examples: Apache and IIS give access to web pages, SSHD (Secure SHell Daemon) lets you run a shell on the machine it is running on.
Examples: Netscape connects to web servers and fetches web pages from them, SSH lets you connect to an SSHD and start a remote shell.
Examples: Apache and IIS generally listen to port 80, SSH generally listens to port 22
(these ideas of "connections" and "ports" are just a protocol, an agreement between client and server, in reality it's still all just packets)
For clarity, these examples are written in Python and don't handle errors. The corresponding C code is very similar. Writing network code in a low level language such as C can be very dangerous! Bored teenagers can use mistakes such as writing data beyond the end of an array to gain control of your computer.
(using sockets in C will be covered in more detail in Trevor Dix's talk on Unix Processes and Sockets)
from socket import *
server_name = 'www.csse.monash.edu.au'
server_port = 80
# Work out IP address corresponding to the server's domain name
server_address = gethostbyname(server_name)
# Create a socket
my_socket = socket(AF_INET,SOCK_STREAM)
# Connect the socket to the server
my_socket.connect((server_address, server_port))
# Make some file objects from the socket object
# The equivalent C function is "fdopen"
# (Alternatively use send and recv on the socket itself,
# but this can be tricky)
incoming_stream = my_socket.makefile("rt")
outgoing_stream = my_socket.makefile("wt")
# Now we can communicate with the server we just connected to
# We should be connected to the School's web server,
# so lets send an HTTP request...
outgoing_stream.write("GET /index.html\r\n\r\n")
outgoing_stream.flush()
# ... and print out the web-page it gives us in reply
print incoming_stream.read()
# Finally, clean up
incoming_stream.close()
outgoing_stream.close()
my_socket.close()
import sys, os from socket import * port = 10000 # Create a socket to listen for connections listening_socket = socket(AF_INET,SOCK_STREAM) # Tell the socket which port it is meant to be listening to # Note: only root can bind to ports below 1024 # Make sure you choose a port that some other application isn't using! listening_socket.bind(('', port)) # Tell the socket to listen for connections listening_socket.listen(5) # listening_socket is a special kind of socket who's only job is to # listen for connections. while 1: # Accept a connection accepted_socket, address = listening_socket.accept() # Accepting the connection created another socket! # This is so that you can keep listening for new connections # while you deal with this connection. # accepted_socket behaves the same way as the socket in the client # Deal with this connection in a separate process, because we want # to keep listening for new connections if os.fork() != 0: # Parent process: close the new socket and keep looping accepted_socket.close() else: # Child process: close the listening socket and # handle the new connection listening_socket.close() incoming_stream = accepted_socket.makefile("rt") outgoing_stream = accepted_socket.makefile("wt") # Do something server-ish outgoing_stream.write("Hello world!\n") incoming_stream.close() outgoing_stream.close() accepted_socket.close() sys.exit(0)
... there is more to network programming than client-server TCP/IP.
Examples: libraries for languages such as Python and Perl
UDP (User Datagram Protocol) is a protocol that can be used instead TCP.
Examples: DNS (Domain Name Service), games such as Quake
NAT (Network Address Translation) networks appear on the internet to have a single IP address. The gateway to the network rewrites the source and destination IP addresses of incoming and outgoing packets, using magic.
Peer-to-peer software acts as both a client and server. Multiple computers cooperate to provide a service.
Examples: Gnutella, The Circle