Introduction to Python - Kansas State University
Download
Report
Transcript Introduction to Python - Kansas State University
Introduction to Network
Programming with Sockets
Network Programming
Kansas State University at Salina
Protocols
Rules that determine how communication is
achieved.
Human protocols: vocal cords to ears, sign
language to eyes, English, French,
Spanish…
Layers of protocols – may replace any
protocol with another functionally equivalent.
Network protocols – Ethernet, IP, TCP, UDP,
HTTP, SMTP, etc…
TCP/IP
OSI Model
TCP/IP
Transmission
Control
Protocol/Internet
Protocol (TCP/IP)
Suite of small,
specialized
protocols called
subprotocols
TCP/IP compared to the OSI Model
Internet Protocol (IP)
IP datagram
IP portion of
TCP/IP
frame that
acts as an
envelope for
data
Contains
information
necessary
for routers to
transfer data
between
subnets
Components of an IP datagram
Transport Control Protocol (TCP)
TCP provides reliable end to end communication.
A TCP
segment
User Datagram Protocol (UDP)
Connectionless transport service
Only four fields in UDP header (10 in TCP
header)
Unreliable
Used for broadcast applications and
voice/video data.
Getting Data from Source to
Destination
IP address – IP header
Protocol (TCP or UDP) – IP header
Port – TCP or UDP protocol
A port is a rendezvous point between TCP
and the application.
A socket allows the application to read from
and write to the port.
Postal Mail Analogy
• IP gets the data to the
right place.
• TCP puts the data in
the right port (mail
box).
• The socket goes and
gets the data from the
port (mail box) and
delivers it to the
application.
Sockets
API for applications to read and write data
from TCP/IP or UDP/IP
File abstraction (open, read, write, close)
Abstract operating system resource.
First introduced with BSD UNIX
De-facto standard API for TCP/IP
Ports
Well Known Ports are in the range of 0 to
1023 only the operating system or an
Administrator of the system can access
Registered Ports are in the range of 1024 to
49151.
Dynamic and/or Private Ports are those from
49152 through 65535 and are open for use
without restriction
The Socket API Commands
See example session
Initialize
sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
Will always use above options.
First option says it is IP version 4 (another value for IP
version 6), in UNIX it can also be set to a named pipe.
Second option says that it will connect to a TCP port.
These names are the original ones used.
Socket API (cont.) Server Side
sock.bind(( hostname, port ))
Hostname can be one of: socket.gethostname(),
‘localhost’ or the machine host name as a string.
The port should usually be between 1024 to
49151.
Can not bind to a port with another socket bound
to it.
Note the double parenthesis, bind() takes a single
tuple argument. The extra set of parenthesis
makes the two values a tuple.
Socket API (cont.) Server Side
sock.listen(n)
Tells the socket to listen for incoming connections
n here is an integer for number of new
connections to hold in queue. For now, we can
set n to 1.
(newsock, address) = ss.accept()
Wait for a connection – blocking
Returns a tuple of a new allocated socket and the
IP address and port number of the client. The
address is a tuple.
A new socket is allocated because a server will
usually want to continue listening to the old
socket.
Socket API (cont.) Client Side
sock.connect(( hostname, portNumber ))
Connect to a server that is listening for
connections
For most exercises in this class, just set hostname
to ‘localhost’.
portNumber should match the port number that
the sever was bound to.
A blocking statement, until the connection is
established.
Socket API (cont.)
message = sock.recv(N)
sock.send( msg )
Read a message from the socket and return a string.
N is the maximum message size in characters.
Blocking
Send a string messages (msg) over an established socket
connection.
sock.close()
Always a good idea to close sockets when finished with
them.