hii readers,
readers, This is our third part of Python Socket Programming . And In Today's Tutorial We will learn About how to create simple UDP server and a simple UDP Client.
As I Already Said In My previous Tutorials, Internet and its Components are very complex to understand in starting.
So, If You are a newbie than i think below links can boost your mind in networking concept.
so, let's again focus on main point.
Q 1. What Is UDP?
Ans. In simple words, User Datagram Protocol is one of the core member of the internet protocol suite. The User Datagram Protocol works differently from TCP/IP. TCP (Transmission Control Protocol) is a stream oriented protocol where UDP (User Datagram Protocol ) is a message oriented protocol. the main difference between UDP and TCP/IP are
- UDP Does Not Need Long-Live Connection But TCP/IP Can Work On long live connections
- Setting UDP Connection Is little bit simple against TCP/IP
- UDP Message Must Fit Within A Single Packet
- UDP Does Not Provide Packet Delivery Guarantee But TCP/IP Provides.
Want For Description?
As Wikipedia says :
"
UDP uses a simple connectionless transmission model with a minimum of protocol mechanism. UDP provides checksums for data integrity, and port numbers for addressing different functions at the source and destination of the datagram. It has no handshaking dialogues, and thus exposes the user's program to any unreliability of the underlying network: there is no guarantee of delivery, ordering, or duplicate protection. If error-correction facilities are needed at the network interface level, an application may use the Transmission Control Protocol (TCP) or Stream Control Transmission Protocol (SCTP) which are designed for this purpose.UDP is suitable for purposes where error checking and correction are either not necessary or are performed in the application; UDP avoids the overhead of such processing at the level of the network interface. Time-sensitive applications often use UDP because dropping packets is preferable to waiting for delayed packets, which may not be an option in a real-time system. "
hmm,
i think, this is enough for theoretically description.
now, let's start some practical example.
so, here first,
we will create UDP Server.
And In This Code, As You Can see,
first import module
then, creating socket object. because to create a socket we need to import socket module in python language that can provide us access for operating system socket.
# import module
import socket
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# IP and Port for receving connection
client_address = ('localhost', 10000)
# print address and port
print "[+] Client IP {} | Port {}".format(client_address[0], client_address[1])
As You Can See In my next Codes:
Binding Socket Object with client IP And Port Address
# bind socket with server
sock.bind(client_address)
# Create Loop
while True:
# Wait for a connection
print '[+] Waiting for a client connection'
# connection established
data, client_address = sock.recvfrom(4096)
print data
And Finally,
This Is Our Complete Code Of UDP Server Script.
# ============================= #
# ====== SERVER ==== UDP ====== #
# ============================= #
# import module
import socket
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# IP and Port for receving connection
client_address = ('localhost', 10000)
# print address and port
print "[+] Client IP {} | Port {}".format(client_address[0], client_address[1])
# bind socket with server
sock.bind(client_address)
# Create Loop
while True:
# Wait for a connection
print '[+] Waiting for a client connection'
# connection established
data, client_address = sock.recvfrom(4096)
print data
Now, Our Server Is Ready!
let's move ahead and
try to create UDP Client Script
So, let's Start our writing process again and
As i does in previous codes, here In this Code .
first importing socket module
then creating socket object for connecting to server
# Import Module
import socket
import time
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
And Finally,
this is our complete script of UDP Client.
# ============================ #
# ==== Client === UDP ======== #
# ============================ #
# Import Module
import socket
import time
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
print '[+] Connecting to %s Port %s' % server_address
message="Message No. {} | Sent By Client At Time {} "
for i in range(15):
sock.sendto(message.format(i, time.asctime()), server_address)
Now, Run These Code One By One.
first run server script and then client script.
here, my output is
$ python ./client.py
[+] Connecting to localhost Port 10000
$ python ./server.py
[+] Client IP localhost | Port 10000
[+] Waiting for a client connection
Message No. 0 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 1 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 2 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 3 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 4 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 5 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 6 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 7 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 8 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 9 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 10 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 11 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 12 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 13 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
Message No. 14 | Sent By Client At Time Sun Mar 12 00:00:42 2017
[+] Waiting for a client connection
I Think, This is enough for today.
So, readers In My Next Tutorial You Will See More Useful Examples and codes.For More Details about socket :Reference Links : Python Official SiteAnd : pymotw
If You Like My Article,
Like, Comment, Share.
Written By :
Suraj