0% found this document useful (0 votes)
7 views15 pages

Socket Python

The document provides an overview of socket programming, detailing the types of sockets (Stream and Datagram) and their functionalities such as BIND, CONNECT, LISTEN, ACCEPT, SEND, RECEIVE, and CLOSE. It includes example Python scripts for creating a socket server and client, demonstrating how to establish communication between them. The document emphasizes the importance of sockets as endpoints for two-way communication in network programming.

Uploaded by

mojumderdipu66
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)
7 views15 pages

Socket Python

The document provides an overview of socket programming, detailing the types of sockets (Stream and Datagram) and their functionalities such as BIND, CONNECT, LISTEN, ACCEPT, SEND, RECEIVE, and CLOSE. It includes example Python scripts for creating a socket server and client, demonstrating how to establish communication between them. The document emphasizes the importance of sockets as endpoints for two-way communication in network programming.

Uploaded by

mojumderdipu66
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

Socket Programming

✓ A process at application layer sends messages into, and


receives messages from, transport layer through a software
interface / object / point entity called a socket.

Prof. Dr. Md. Imdadul Islam 1


There are two types of sockets:
Stream Socket (TCP)
Datagram Socket (UDP)
Prof. Dr. Md. Imdadul Islam 2
Create a socket or node named s, where s is also called socket object

# An example script to connect to Google using socket


# programming in Python socket.SOCK_STREAM
import socket represents TCP
s = [Link](socket.AF_INET, socket.SOCK_STREAM)
print ('Socket successfully created')
# default port for socket .AF_INET represents IPv4
port=80
host_ip = [Link]('[Link]')
# connecting to the server [Link]
[Link]((host_ip, port))
print ('IP of [Link]',host_ip)
Prof. Dr. Md. Imdadul Islam 3
We got the same IP
from tracert and socket
programming.

Access google using


[Link]

Socket successfully created


IP of [Link] [Link]

Prof. Dr. Md. Imdadul Islam 4


Socket successfully created
IP of [Link] [Link]
Prof. Dr. Md. Imdadul Islam 5
Client Server Communications

Prof. Dr. Md. Imdadul Islam 6


The main functions of socket programing:

BIND - attach a socket to a specific local IP address and port number.


CONNECT- attempt to establish a connection
LISTEN- indicate that the socket can accept new connections.
ACCEPT-begin a network connection following an incoming
connection request.
SEND- transmit data over the connection.
RECEIVE- receive data over the connection.
CLOSE- release the connection.

Prof. Dr. Md. Imdadul Islam 7


✓ Socket is considered as the endpoint (like an object at the communication device
i.e. telephone set) of a 2-way communication.

Prof. Dr. Md. Imdadul Islam 8


Server side Import the python socket module,
this is a built-in module
IP address [Link]

We pick a random high


import socket TCP port to listen on
LOCALHOST = "[Link]"
PORT = 8080
server = [Link](socket.AF_INET, socket.SOCK_STREAM)
[Link]((LOCALHOST, PORT))
[Link](1) BIND - attach a socket to a specific local IP address and
port number. For a local server, we might choose to bind to
print("Server started") the IP address [Link] with the TCP port number 8080.

print("Waiting for client request..")


It indicates that server has the capability of listening.
Prof. Dr. Md. Imdadul Islam 9
This method indicates that, the server
will wait for client connection.
clientConnection, clientAddress = [Link]() The ‘clientConnection’ is a new socket
object, is used exclusively to send and
print("Connected client :" , clientAddress) receive data (send(), recv()) with the
msg = ‘’ client.
while True:
in_data = [Link](1024) After a connection is established, the
msg = in_data.decode() server prints out the client address
if msg=='bye': and then waits for data.
break
print("From Client :" , msg) Byte code (UNICODE) to original
text converter for received data Received data from client in
out_data = input()
[Link](bytes(out_data,'UTF-8')) byte format where buffer size
of 1024 bytes at a time.
print("Client disconnected....")
[Link]()
Original Text is converted to byte
Prof. Dr. Md. Imdadul Islam (UNICODE) during transmission
10
import socket
LOCALHOST = "[Link]"
PORT = 8080
server = [Link](socket.AF_INET, socket.SOCK_STREAM)
[Link]((LOCALHOST, PORT)) Copy it on new file of idle shell
[Link](1) and save it as [Link]
print("Server started")
print("Waiting for client request..")
clientConnection, clientAddress = [Link]()
print("Connected clinet :" , clientAddress)
msg = ''
while True:
in_data = [Link](1024)
msg = in_data.decode()
if msg=='bye':
break
print("From Client :" , msg)
out_data = input()
[Link](bytes(out_data,'UTF-8'))
print("Client disconnected....")
[Link]()

Prof. Dr. Md. Imdadul Islam 11


Prof. Dr. Md. Imdadul Islam 12
UTF-8 (Unicode Transformation Format-8)
Client side Byte Code-এ প্রতিটি ক্যারেক্টাে কক্ ১ কেরক্ 8
বাইরে represent ক্রে
import socket
SERVER = "[Link]"
PORT = 8080 bytes("This is from Client",'UTF-8')
client = [Link](socket.AF_INET, socket.SOCK_STREAM)
[Link]((SERVER, PORT)) এই ইউতিরক্ারেে String টিরক্ Byte Code-এ
ক্িভােট ক্রে
[Link](bytes("This is from Client",'UTF-8'))
while True:
in_data = [Link](1024) out_data = input()
print("From Server :" ,in_data.decode()) [Link](bytes(out_data,'UTF-8’))
ক্লার়েরেে কেখা সমস্তইউতিরক্ারেে String-কক্
out_data = input() Byte code-এ ক্িভােট ক্রে সাভট াে এে ক্ারে
[Link](bytes(out_data,'UTF-8')) পাঠির়ে তিরব
if out_data=='bye':
break
[Link]()
Prof. Dr. Md. Imdadul Islam 13
Open a new shell and copy it on new file of idle shell
and save it as [Link]. Make conversation with the
server and client.

Prof. Dr. Md. Imdadul Islam 14


Client
=== RESTART:
C:\Users\CSEJU\AppData\Local\Programs\Python\Python310\[Link] ==
From Server : Hellow
I am client (2)
From Server : Nice to hear
me too (4)
From Server : bye
Bye (5)
Server
=== RESTART:
C:\Users\CSEJU\AppData\Local\Programs\Python\Python310\S
[Link] ==
Server started
Waiting for client request..
Connected clinet : ('[Link]', 57676)
Repeat the job using From Client : This is from Client
Hellow (1)
command prompt of C: From Client : I am client
Nice to hear (3)
From Client : me too
Bye (5)
Client
Prof. Dr. Md. disconnected....
Imdadul Islam 15

You might also like