0% found this document useful (0 votes)
2 views21 pages

11 L11 Socket Programming_python

The document provides an overview of socket programming fundamentals, focusing on programming languages suitable for network programming, particularly Python. It discusses the differences between TCP and UDP protocols, socket functions, and methods for managing sockets in Python. Additionally, it includes examples and exercises for practical application of the concepts covered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

11 L11 Socket Programming_python

The document provides an overview of socket programming fundamentals, focusing on programming languages suitable for network programming, particularly Python. It discusses the differences between TCP and UDP protocols, socket functions, and methods for managing sockets in Python. Additionally, it includes examples and exercises for practical application of the concepts covered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CS302.

3
Wireless Technologies and
Network Programming
Lecture 11 – Sockets Programming Fundamentals

Chamara Disanayake
Department of Network and Security
Overview

• Select the programming language


• Comparing the languages
• Basic programing with Python
• Network Programming Basics
• Fundamentals in Socket Programming
• Basic Programing with UDP
• Basic Programing with TCP

2
Programming Language

• Languages have different flexibilities


• Object oriented concepts
• Network programming capabilities
• Concurrent programming capabilities
• Resource efficiency

https://statisticstimes.com/tech/top-computer-languages.php
3
Comparison

• C, C++, Python, Java

4
Requirement for Socket Programming

• Socket API is available for many languages on many platforms:


• C/C++, Java, Perl, Python (A common language that networking professionals
will encounter in their careers)
• Socket Programs written in any language and running on any platform can
communicate with each other!
• Writing communicating programs in different languages is a good exercise

5
Technical Aspects

• Code complexity “C++ too have some drawbacks. Implied complexity by the
language (from manual memory management, compiler
• Compatibility, Compilation, Libraries version and library incompatibility to subtle points like template
syntax, etc) makes writing proper complex software in C++
• Memory CPU utilization requiring better skills, longer debugging and testings and are
• Concurrent programming, threading harder to change.
Unless the application I'm building is CPU bounded and has
• Memory management efficiency been proven insufficient to be built with easier solutions like
Python, Java or .NET, I wouldn't start scratching it with C++.”
• Platform dependency - Expert Network Programmer, www.quora.com

Python provides two levels of access to network services.


Which is the best language for server-side socket programming? - At a low level, you can access the basic socket support in
Quora. the underlying operating system, which allows you to
“For productivity, and fitting your familiarity, Java is the first one. Java
implement clients and servers for both connection-
itself affords a nice API for socket programming called Java NIO, where
you can write your network module easily” oriented and connectionless protocols.

6
Examples

• Codes in C++ and Java

7
Programming with Java

8
Get familiar to Python programming

• Python in Windows environment.


• Python IDE
• IDLE (Integrated Development and Learning
Environment) is a default editor that comes with
Python.
• PayCharm is a cross-platform IDE used for Python
programming.
• Python Syntax
• https://www.101computing.net/wp/wp-
content/uploads/Python-Cheat-Sheet.pdf

9
Socket Programming

• Network programming is a way of connecting two nodes on a


network to communicate with each other.
• One socket(node) listens on a particular port at an IP, while the other socket
reaches out to the other to form a connection.
• The server forms the listener socket while the client reaches out to the
server.
• Socket - Basic Parameters
• Connection Address Method – The IP address/Node OS support
• Connection Type
• Reliable, Stream of data
• Fast, single communications

10
Working with TCP and UDP

• As described, the procedure in communication in TCP and UDP is


deferent to each other.
• TCP is connection-oriented protocol where as UDP is connection-less
When TCP should be selected?
•Is reliable: Packets dropped in the network
are detected and retransmitted by the
sender.
•Has in-order data delivery: Data is read by
your application in the order it was written
by the sender

11
How to Manage Socket in Python?

• A socket is one endpoint of a two-way communication link between


two programs running on the network.
• In Python, The socket function returns a new socket object.
socket.socket(family=AF_INET, type= SOCK_STREAM, proto=0, fileno=None)

Family : Address Families AF_INET, AF_INET6, AF_UNIX etc


Type : Socket types The valid values for socketType are socket.SOCK_STREAM,
socket.SOCK_DGRAM, socket.SOCK_RAW, socket.SOCK_RDM and
socket.SOCK_SEQPACKET.

To use sockets, import the Python socket library


import socket

12
Socket Functions and Methods
socket()
.bind() • socket.bind()
• The bind() method of Python's socket class assigns an IP address and a port number to a
.listen() socket instance.
.accept() • The bind() method is used when a socket needs to be made a server socket.
.connect() • socket.listen()
.connect_ex() •• Calling listen() makes a socket ready for accepting connections.
The listen() method should be called before calling the accept() method on the server socket.
.send() • A listening socket does just what its name suggests. It listens for connections from clients
.recv() • socket.accept()
.close() • The accept() method of Python's socket class, accepts an incoming connection request from
a TCP client.
• The accept() method is called on a TCP based server socket.
• When a client connects, it returns a new socket object representing the connection and a
tuple holding the address of the client.
• The tuple will contain (host, port) for IPv4 connections or (host, port, flowinfo, scopeid) for IPv6.

13
Client Functions

• socket.connect()
• The connect() method of Python’s socket module, connects a TCP based client
socket to a TCP based server socket.
• The connect() function initiates a 3-way handshake with the server socket and
establishes the connection.
• Once a connection is established to the remote socket the send(),
sendall() methods can be used for sending data to the server.
• In the similar way the recv() function can be used for receiving data
from the server socket.
• The recv() function wait for a message to arrive in the socket unless the
socket is non-blocking mode and read up to the specified buffer size.

14
TCP Communication over Sockets

• Server makes to set up a “listening” socket with


bind() and listen() functions and ready to accept
connections with accept()
• The client calls .connect() to establish a
connection to the server and initiate the three-
way handshake.
• When a client connects, the server calls .accept()
to accept, or complete, the connection.
• In the middle is the round-trip section, where
data is exchanged between the client and server
using calls to .send() and .recv().
• At the bottom, the client and server close their
respective sockets. 15
UDP Communication

• The UDP does not provide guaranteed delivery of message packets. If


for some issue in a network if a packet is lost it could be lost forever.

16
TCP vs UDP

• TCP • UDP
• Socket creates with socket type • Socket creates with socket type
socket.SOCK_STREAM socket.SOCK_DGRAM

• Unique Functions • Unique Functions


• listen() • sendto()
• accept() • recvfrom()
• .send() and .recv().
Common Functions
• .socket() to create the socket
• .bind() – Bind the socket to an IP and Port 17
Exercise 1

• Communicating 2 nodes with UDP by sending a messages (UDP)


between the nodes (server and Client)
• Create a echo server which listen to a UDP socket and send a message to client when connect to the
UDP port while displaying client’s message
• Create a client which connect to the server with given server port and send message to server while
displaying server message at the client end.

UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)

msgFromServer = UDPClientSocket.recvfrom(bufferSize)

18
Advancing with User Input and valuable Communication

• Compare the UDP communication with TCP communication with


Wireshark and justify the characteristics of TCP and UDP
• Improve the code to send user defined message from the client and
sent the datagram to the server while customizing the server message
as desired.
• Convert the echo server to a DNS resolving server which resolve the
DNS query sent by the client and send the resolved IP to the client.

19
Output – Basic Communication

• Server Started and display client message with client connection


parameters

• Client send a message and display server message at the client end

20
References

• https://realpython.com/python-sockets/
• https://pythontic.com/modules/socket/udp-client-server-example

21

You might also like