7.6 Writing Networked Applications

Advertisement

The applications which send and receive data over the network are written in one or more programming languages. Many programming languages have libraries of code that make it quite simple to write application code to send and receive data across the network. With a good programming library, making a connection to an application running on a server, sending data to the server, and receiving data from the server is generally as easy as reading and writing a file.

As an example, the code below is all it takes in the Python programming language to make a connection to a web server and retrieve a document:

import socket
 mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 mysock.connect(('www.py4inf.com', 80))
 mysock.send('GET http://www.py4inf.com/code/romeo.txt HTTP/1.0nn')
while True:
 data = mysock.recv(512)
 if ( len(data) < 1 ) :
 break
 print data
 mysock.close()

Figure 7.7: Programming with Sockets in Python

While you may or not know the Python programming language, the key point is that it only takes ten lines of application code to make and use a network connection. This code is simple because the Transport, Internetwork, and Link layers so effectively solve the problems at each of their layers that applications using the network can ignore nearly all of the details of how the network is implemented.

In the Python application, in this line of code
mysock.connect((‘www.py4inf.com’, 80))
we have specified that we are connecting to the application that is listening for incoming connections on port 80 on the remote computer www.py4inf.com.

By choosing port 80 we are indicating that we want to connect to a World Wide Web server on that host and are expecting to communicate with that server using the HyperText Transport Protocol.

Back to Book’s Main Index

Advertisement

Leave a Reply

Your email address will not be published. Required fields are marked *