You are on page 1of 24

01 Connectionless Sockets

• Connectionless Sockets
• Socket process for connectionless clients
and servers
• socket() Module Function
• Socket Object (Built-In) Methods
• The SocketServer Module
Connectionless Sockets

No connection is necessary before communication


can begin.

No guarantees of sequencing, reliability, or


nonduplication in the process of data delivery.

User Datagram Protocol (UDP)

To create UDP sockets, we must use


SOCK_DGRAM as the socket type.

2
13
Connectionless Sockets

These sockets also use the Internet Protocol to find


hosts in the network.

This system also has a more general name, going by


the combined names of both of these protocols
(UDP and IP), or UDP/IP.

3
13
Socket process for connectionless
clients and servers

4
13
Socket() Module Function

To create a socket, you must use the socket.socket()


function, which has the general syntax:

Socket (socket_family, socket_type, protocol=0)

The socket_family is either AF_UNIX or AF_INET,


and the socket_type is SOCK_ DGRAM.

5
13
Socket() Module Function

The protocol is usually left out, defaulting to 0.

To create a UDP/IP socket

udpSock = socket.socket
(socket.AF_INET, socket.SOCK_DGRAM)

6
13
Socket Object (Built-In) Methods

General Socket Methods

s.recvfrom() s.recvfrom_into() s.sendto()

Receive Receive UDP Transmit


UDP message into UDP
message specified buffer message

7
13
Socket Object (Built-In) Methods

Blocking-Oriented Socket Methods

s.setblocking()

Set blocking or non-blocking mode of socket

s.settimeout()

Set timeout for blocking socket operations

s.gettimeout()

8 Get timeout for blocking socket operations


13
Socket Object (Built-In) Methods

File-Oriented Socket Methods

s.fileno() File descriptor of socket

s.makefile() Create a file object associated with socket

9
13
Socket Object (Built-In) Methods

Data Attributes
s.family

The socket family

s.type

The socket type

s.proto
The socket protocol
10
13
The Socket Server Module

BaseServer Contains core server functionality and hooks


for mix-in classes

TCPServer/ Basic networked synchronous


UDPServer TCP/UDP server

UnixStreamServer / Basic file-based synchronous


UnixDatagramServer TCP/UDP server

11
13
The Socket Server Module

ForkingMixIn /
ThreadingMixIn

Core forking or threading functionality; used only as


mix-in classes with one of the server classes to
achieve some a synchronicity

ThreadingTCPServer/
ThreadingUDPServer

Combination of ThreadingMixIn and TCPServer/


UDPServer

12
13
The Socket Server Module

BaseRequestHandler

Contains core functionality for handling service requests;


used only for derivation

StreamRequestHandler /
DatagramRequestHandler

Implement service handler for TCP/UDP servers

13
13
User Datagram Protocol
02 (UDP)
• User Datagram Protocol (UDP)
• Differences between the TCP and UDP
• Python Internet modules
• Implementing the Client
• Implementing the Server
User Datagram Protocol (UDP)

UDP is typically used in applications that require little


overhead and higher network throughput, such as
multimedia streaming protocols.

It is capable of transmitting data to multiple


endpoints simultaneously because a connection is
not bound to a single address.

In environments where there is difficulty transmitting


to the destination, information may have to be re-
transmitted several times before the complete
2 message is received.
10
Differences between the TCP and UDP

TCP UDP
Service Service

Provide connection setup between client


connectionless
and server processes
Reliable data transfer between sending
and receiving processes by using flow
Unreliable
control, sequence numbers,
acknowledgements, and timer
Sender will not overwhelm receiver No

Throttle sender when network overloaded No


Timing, minimum throughput guarantees, The same with
3 security TCP
10
Python Internet modules

Protocol Common Function Port No Python module

httplib, urllib,
HTTP Web pages 80
xmlrpclib

NNTP Usenet news 119 nntplib

FTP File transfers 20 ftplib, urllib

SMTP Sending email 25 smtplib

4
10
Python Internet modules

Protocol Common Function Port No Python module

POP3 Fetching email 110 poplib

IMAP4 Fetching email 143 imaplib

Telnet Command lines 23 telnetlib

Gopher Document transfers 70 Gopherlib,urllib

5
10
Implementing the Client

Import the socket python module, this can be done like so:

import socket

Declare the IP address that we will be trying to send our


UDP messages to as well as the port number.

UDP_IP_ADDRESS = "127.0.0.1”
UDP_PORT_NO = 6789

6 Message = "Hello, Server”


10
Implementing the Client

Create the socket through which we will be sending our


UDP message to the server.
clientSock=
socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

Write the code that will send our UDP message:

clientSock.sendto(Message,(UDP_IP_ADDRESS,
UDP_PORT_NO))

7
10
Implementing the Server

Server program will be continuously listening on defined


IP address and port number for any UDP messages.

import socket
UDP_IP_ADDRESS = "127.0.0.1"
UDP_PORT_NO = 6789

8
10
Implementing the Server

When imported the socket module and declared IP


address and port number, we can create another
socket which will look exactly like the socket we
constructed in our client program.

serverSock = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
serverSock.bind((UDP_IP_ADDRESS,
UDP_PORT_NO))

9
10
Implementing the Server

The code that will keep the script continuously


listening to the server socket until its termination.

This takes form as a simple while loop, like so:

while True:
data, addr = serverSock.recvfrom(1024)
print ("Message: ", data)

10
10
Building a Python
03 Networking Program

You might also like