Upload
susanna-cobb
View
212
Download
0
Tags:
Embed Size (px)
Citation preview
Structural view of networks◦ Physical layer: wire, radio, etc.◦ Data link layer: protocol principles, etc◦ Network layer: routing, internetworking◦ Transport layer: protocols (tcp), etc◦ Application layer: email, web, class project
We will use one common method at the transport/network level (TCP/IP via sockets)
application
transport
network
link
physical
Internetwork: a network of networks
Uses packet switching to send data between machines on this “internet”
Home network
Institutional network
Mobile network
Global ISP
Regional ISP
Each machine has a unique address on the network (IP address)
Internet Protocol (IP): format of packets used to create these envelops of data◦ Like mailing a multipage letter by putting each page into a separately mailed envelop
◦ May not arrive in correct order
1. Hosts (e.g., computers and other devices) are mapped to a set of 32-bit* IP addresses.◦ 147.129.181.14
2. The set of IP addresses is mapped to a set of identifiers called Internet domain names.◦ 147.129.56.50 is mapped to www.ithaca.edu
3. A process on one Internet host can communicate with a process on another Internet host over a connection.
* this is IPv4. There is a new IP standard iPv6 that uses 64-bit addresses. See http://en.wikipedia.org/wiki/IPv6
Windows NT/2000/XP/Win 7◦ Click on start, run, type cmd and at the prompt type ipconfig
◦ Type exit to close the command prompt window.
You’ll see something like:
Windows IP Configuration
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . :IP Address. . . . . . . . . . . . : 192.168.1.101Subnet Mask . . . . . . . . . . : 255.255.255.0Default Gateway . . . . . . . : 192.168.1.1
From the Finder, under Applications -> Utilities launch Network Utility. ◦ in OS X 9.x look in /System/Library/CoreServices/Applications
◦ Select the correct ethernet interface (normally Ethernet 1 (en0)).
◦ Information and utilities are available from the tabs above.
From the Finder, choose apple->System Preferences. ◦ Then click on “Network” and in the resulting window choose “ethernet”
Or, start a terminal window. ◦ Type “ifconfig –a” ◦ Look under “en0”
For Linux or Unix users, to view their IP address or network information, users must have administrator or root privileges.
1. Open the Linux or Unix shell if you are utilizing a GUI interface for your Linux or Unix machine.
2. From the prompt, type ifconfig eth0 and press enter. You’ll see:
eth0 Link encap:Ethernet HWaddr 00:A0:24:72:EB:0A inet addr:10.10.10.2 Bcast:10.0.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:5569799 errors:32 dropped:32 overruns:0 frame:6 TX packets:3548292 errors:0 dropped:0 overruns:0 carrier:3 Collisions:14 Interrupt:18 Base address:0xda00
As seen from the above example, users will commonly see the network settings for all their network devices when running the "ifconfig" command. First in the above example we have the network settings for the "lo" or "local loopback", next is the actual network settings of your network adapter.
ping◦ “a computer network administration utility used to test the reachability of a host on an Internet Protocol (IP) network and to measure the round-trip time for messages sent from the originating host to a destination computer”
Open a terminal window (Mac) or do run->cmd (Windows)
A228536:~ barr$ ping www.ithaca.edu
PING www.ithaca.edu (147.129.56.50): 56 data bytes
64 bytes from 147.129.56.50: icmp_seq=0 ttl=62 time=1.236 ms
64 bytes from 147.129.56.50: icmp_seq=1 ttl=62 time=1.269 ms
64 bytes from 147.129.56.50: icmp_seq=2 ttl=62 time=1.309 ms
The whois utility looks up records in the databases maintained by several Network Information Centers (NICs).
whois -h hostname domainName e.g. whois ithaca.eduOr, if you know of a whois server like whois.educause.net (stores edu
names):
whois -h whois.educause.net ithaca.edu
Barr$ whois ithaca.edu
Domain Name: ITHACA.EDU
Registrant:
Ithaca College
Network & Communication Services
110 Phillips Hall
Ithaca, NY 14850-7028
UNITED STATES
Administrative Contact:
Bernie Rhoades
Director, Network & Communication Services
Ithaca College
Information Technology Services
110 Phillips Hall
Ithaca, NY 14850-7028
UNITED STATES
(607) 274-1694
Technical Contact:
William W. Weeks
Network Services Manager
Ithaca College
Information Technology Services 110 Phillips Hall Ithaca, NY 14850-7028 UNITED STATES (607) 274-1311 [email protected]
Name Servers: RESOLVER1.ITHACA.EDU 147.129.1.2 RESOLVER2.ITHACA.EDU 147.129.94.4 RESOLVER3.ITHACA.EDU 208.81.240.2
Domain record activated: 18-Jun-1990Domain record last updated: 24-Sep-2007Domain expires: 31-Jul-2009
Transmission Control Protocol (TCP): ◦Used to create reliable byte-streaming communication
◦Ensures that messages are passed error-free from one machine to another on an internet
◦Divides data into discrete messages before passing them on to the IP layer
◦At the receiving end, protocol used to “reconstruct” the packets correctly
Applications utilize TCP/IP communication by having the sender and the receiver create end points called Sockets
Sockets are created and maintained by the Operating System (not the network stack)
Created in the early 80’s as part of the original Berkeley distribution of Unix that contained an early version of the Internet protocols.
Provides a user-level interface to the network.
Underlying basis for all Internet applications.
Based on client/server programming model.
What is a socket?◦ To the kernel, a socket is an endpoint of communication.
◦ To an application, a socket is a file descriptor that lets the application read/write from/to the network. In Unix all I/O devices, including networks, are modeled as files.
Clients and servers communicate with each other by reading from and writing to socket descriptors.
A socket is not a physical device!◦ your computer may have one physical Ethernet connection◦ and many sockets
Each socket has a socket number (address)◦ IP address of the host computer◦ Port number local to that host to which the socket is attached Port numbers below 1024 are reserved (e.g. for email and FTP)
Some addresses are “well known”, Port 7: Echo server Port 23: Telnet server Port 25: Mail server Port 80: Web server
You must choose a port not already in use
To use TCP service a connection must be explicitly established between a socket on the sending machine and a socket on the receiving machine
A socket may be used for multiple connections at the same time.
A computer (and/or application) may have multiple open socket connections
Computer
Physical connection (e.g.
ethernet, RS
232)
OSapplication
Port 80
Port 5678
socket
IP: 147.129.30.2
Client Server
socket socket
bind
listen
rio_readlineb
rio_writenrio_readlineb
rio_writen
Connectionrequest
rio_readlineb
close
closeEOF
Await connectionrequest fromnext client
open_listenfd
open_clientfd
acceptconnect
use netstat.exe program (ships with windows, mac, and linux).
Start ”Command Prompt" using run-> cmd on Windows type netstat -na and then press Enter, use command "more" to stop when your listing contains to much lines (netstat -na | more).
Each line of netstat's listing contains these information:
* Protocol type - it can be TCP or UDP. Two sockets can use the same port number when they use different protocols.
* Local Address - local IP address which is used. In most cases it is 0.0.0.0 (any = not bound to the specific IP address. It can accept connection from any PC's interface). When IP address is specified then only connection from interface with the same IP address are accepted.
Local Address field contains port number which is used to accept connection or receive UDP data.
* Foreign Address - address of PC on remote side. It will be displayed when connection is established.
* State - state of socket. You can see this states:
LISTENING - socket waits for connection (or data in case of UDP socket)
ESTABLISHED - connection to the remote side is established
CLOSE_WAIT, TIME_WAIT - socket waits for connection close or other type of negotiation.
To determine whether desired port number is already in use you must check Local Address field on all lines.
On Windows:
Example line:
TCP 0.0.0.0:25 0.0.0.0:0 LISTENING
Some application which uses port 25 (SMTP server port) is running.
On Linux:
Read the /etc/services file. If you want the list of open ports, try netstat -nlp◦ Arguments: n = show network addresses as numbers, l = show listening sockets, p = show pid and name of program
On Macs:
Read the /etc/services file. If you want the list of open ports, try netstat -n◦ The port follows the IP address
Networked applications may be structured any number of ways
One popular structure is client-server Server:◦ Usually unique◦ Provides a service ◦ Runs continuously
Clients◦ Usually many◦ Send request for service, receive result from server
◦ Connect and disconnect as needed
Clientprocess
Serverprocess
1. Client sends request
2. Server handlesrequest
3. Server sends response4. Client handlesresponse
Resource
Web server(port 80)
Client host
Server host 128.2.194.242
Echo server(port 7)
Service request for128.2.194.242:80(i.e., the Web
server)
Web server(port 80)
Echo server(port 7)
Service request for128.2.194.242:7
(i.e., the echo server)
Kernel
Kernel
Client
Client
Servers are long-running processes (daemons in Unix).◦ Created at boot-time (typically) by the init process (process 1)
◦ Run continuously until the machine is turned off. Each server waits for requests to arrive on a well-known port associated with a particular service.◦ Port 7: echo server◦ Port 23: telnet server◦ Port 25: mail server◦ Port 80: HTTP server◦ See
http://www.answersthatwork.com/Download_Area/ATW_Library/Networking/Network__2-List_of_Common_TCPIP_port_numbers.pdf
A machine that runs a server process is also often referred to as a “server.”
Web server (port 80)◦ Resource: files/compute cycles (CGI programs)◦ Service: retrieves files and runs CGI programs on behalf of the client
FTP server (20, 21)◦ Resource: files◦ Service: stores and retrieve files
Telnet server (23)◦ Resource: terminal◦ Service: proxies a terminal on the server machine
Mail server (25)◦ Resource: email “spool” file◦ Service: stores mail messages in spool file
See /etc/services for a comprehensive list of the services available on a Linux machine.
Your web browser is the client Computer containing the pages you are trying to access runs the web server
Your computer requests the page Server sends the page Many other people may be requesting the same page
Note: these are distinct applications. Your browser (client) never acts as a server
As you develop networked applications:◦ Is the client-server model a good model?
Minimizes network communication? Minimizes computational redundancy?
If so:◦ Which program will be on the server side (e.g. provide a service)?
◦ Which must be on the client side?
python provides built-in support for TCP/IP network communication
There are methods for connecting a client and methods for connecting a server
Within a Client application, create a socket and connect to a specific server:
#Socket client example in python
import socket #for sockets
#create an AF_INET, STREAM socket (TCP)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Address Family : AF_INET (this is IP version 4 or IPv4)Type : SOCK_STREAM (this means connection oriented TCP protocol)
Better to do error checking:#handling errors in python socket programs
import socket #for sockets
import sys #for exit
try:
#create an AF_INET, STREAM socket (TCP)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except (socket.error, msg):
print ('Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1])
sys.exit()
To connect to a remote server need 2 things, IP address and port number. ◦ need to know the IP address of the remote server◦ Here we used the ip address of google.com as a sample.host = 'www.google.com' try: remote_ip = socket.gethostbyname( host ) except socket.gaierror: #could not resolve print ('Hostname could not be resolved. Exiting’) sys.exit() print ('Ip address of ' + host + ' is ' + remote_ip)
method to obtain an IP address from a host name
method to obtain an IP address from a host name
Now connect to google on port 80 (the well known http port)
#Connect to remote serverport = 80s.connect((remote_ip , port)) print ('Socket Connected to ' + host + ' on ip ' + remote_ip)
Now we can send and receive messages◦ sendall method sends bytes through the socket◦ recv method receives a number of bytes
#Send some data to remote servermessage = "GET / HTTP/1.1\r\n\r\n" try : #Set the whole string s.sendall(bytes(message, 'UTF-8'))except socket.error: #Send failed print ('Send failed') sys.exit() print ('Message send successfully’)#Now receive datareply = s.recv(4096) print (reply)
must convert string to bytesmust convert string to bytes
Servers do the following actions:1. Open a socket 2. Bind to a address(and port).
3. Listen for incoming connections. 4. Accept connections 5. Read/Send
try: #create an AF_INET, STREAM socket (TCP) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)except (socket.error, msg): print ('Failed to create socket. Error code: ' + str(msg[0]) + ' , Error: ' + msg[1]) sys.exit();
open a socket for the server just as we did for the client
open a socket for the server just as we did for the client
Next must bind the socket to an address and a port
By doing this we ensure that all incoming data which is directed towards this port number on this machine is received by this application.
To bind the socket use the following method:◦ s.bind(hostname, port number)
Ways of specifying arguments to bind.
to find the name of the host that the program is running on use:◦ hostname = socket.gethostname()
to create a “server” socket that is only visible within the same machine use ◦ s.bind(('localhost', 80)) or ◦ s.bind(('127.0.0.1', 80))
To specify that the socket is reachable by any address the machine happens to have use◦ s.bind(('', 80))
To bind the socket to an address and a port
import socketimport sys HOST = '' # Symbolic name meaning all available interfacesPORT = 8888 # Arbitrary non-privileged port
try: s.bind((HOST, PORT))except socket.error , msg: print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] sys.exit() print 'Socket bind complete'
the server socket will listen on port 8888 (not a well known port) on all interfaces
the server socket will listen on port 8888 (not a well known port) on all interfaces
The next step is to make the socket listen for incoming requests
We must put the socket into listening mode:◦ s.listen(x)
The parameter x is called the backlog. ◦ It controls the number of incoming connections that are kept "waiting" if the program is already busy.
◦ x =10 means that if 10 connections are already waiting to be processed, then the 11th connection request shall be rejected.
The final step is for the socket to accept incoming requests
When a request to this machine on the correct port comes in, the socket must accept the request:◦ (conn, addr) = s.accept()
The result conn is a new socket with which we can communicate with the client.
The result addr is an array. ◦first element is the IP of the client◦second element is the port the client is using
(conn, addr) = s.accept()#display client informationprint ('Connected with ' + addr[0] + ':' +str(addr[1]))
This displays the client addr
Now can send & receive over the connection:
#now keep talking with the clientdata = conn.recv(1024)print ("Server received: " + data)conn.sendall("Server says: Hello client!")
When finished, close the connection & socket:
conn.close()s.close()
See Class web site: ◦ Course Resources -> Program Examples->find_ip.py
◦ Course Resources -> Program Examples->client.py
◦ Course Resources -> Program Examples->server.py
Calculating area:◦ Server calculates area given radius◦ Client gets radius from user, requests area calculation
server
radius
clientclient
radius
Area