[go: up one dir, main page]

0% found this document useful (0 votes)
12 views11 pages

Sockets

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)
12 views11 pages

Sockets

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/ 11

sockets

By Varun Krishnan M
What is socket..?
• A quick connection which allows the transmission of data between two
processes on the same machine or different machines over a network.
• Socket 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.
• Socket programming is started by importing the socket library and making a
simple socket.
import socket s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
Here we made a socket instance and passed it two parameters. The first parameter is AF_INET and the
second one is SOCK_STREAM. AF_INET refers to the address-family ipv4. The SOCK_STREAM
means connection-oriented TCP protocol.
Now we can connect to a server using this socket.
Connecting to a server:

• if any error occurs during the creation of a socket then a socket. error is
thrown and we can only connect to a server by knowing its IP. You can find
the IP of the server by using this :
$ ping www.google.com

• You can also find the IP using python:


import socket ip = socket.gethostbyname('www.google.com') print ip
Socket API Overview

The primary socket API functions and methods in this module are:

• socket()
• .bind()
• .listen()
• .accept()
• .connect()
• .connect_ex()
• .send()
• .recv()
• .close()
TCP Sockets
• create a socket object using socket.socket(), specifying the socket type as
socket.SOCK_STREAM.
• When you do that, the default protocol that’s used is the Transmission Control Protocol
(TCP). This is a good default .
• Why should you use TCP? The Transmission Control Protocol (TCP):
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.
• In contrast, User Datagram Protocol (UDP) sockets created with socket.SOCK_DGRAM
aren’t reliable, and data read by the receiver can be out-of-order from the sender’s writes.

Why is this important?

• There’s no guarantee that your data will reach its destination or that you’ll receive what’s
been sent to you.

• Network devices, such as routers and switches, have finite bandwidth available and come
with their own inherent system limitations.

• They have CPUs, memory, buses, and interface packet buffers, just like your clients and
servers.

• TCP relieves you from having to worry about packet loss, out-of-order data arrival, and
other pitfalls that invariably happen when you’re communicating across a network.
TCP socket flow
• The left-hand column represents the server. On the right-hand side is the
client.

• Starting in the top left-hand column, note the API calls that the server
makes to set up a “listening” socket:

• .socket()
• .bind()
• .listen()
• .accept()
• A listening socket does just what its name suggests. It listens for
connections from clients.

• When a client connects, the server calls .accept() to accept, or complete, the
connection.
• The client calls .connect() to establish a connection to the server and initiate
the three-way handshake.
Advantages:

Versatility: Python's socket library provides a powerful and versatile framework for networking
applications.

Cross-Platform: Python itself is a cross-platform language, and its socket library allows for
networking code that can run on various operating systems without modification.

Ease of Use: Python's socket API is relatively straightforward and easy to understand, especially
for beginners.

Community Support: Python has a large and active community of developers, which means
there's plenty of community support available for socket programming. Developers can find
libraries, frameworks, and online forums to help them with their networking projects.

Integration: Python's socket library integrates well with other Python libraries and frameworks,
making it easy to incorporate networking capabilities into existing Python applications.
Disadvantages:

Low-Level: While sockets provide a powerful way to communicate over a network, they
operate at a relatively low level compared to higher-level protocols.

Concurrency:While Python supports multi-threading and asynchronous I/O for concurrent


programming, developers need to be mindful its impact on performance.

Performance: While Python is known for its simplicity and ease of use, it may not always
be the best choice for high-performance networking applications

Security: Developers need to be aware of potential security vulnerabilities, such as buffer


overflows or injection attacks, and take appropriate measures to mitigate these risks.

Platform-Specific Issues: While Python itself is cross-platform, there may be platform-


specific issues or differences in behavior when using sockets on different operating
systems. Developers may need to account for these differences and write platform-specific
code to ensure compatibility across different environments.
Thank you

You might also like