Movatterモバイル変換


[0]ホーム

URL:


PPTX, PDF37 views

Basics of Socket Programming using python

The document outlines the process of building client/server applications that communicate using sockets, detailing both UDP (unreliable) and TCP (reliable) socket programming. It provides examples of how clients and servers interact using Python code, illustrating the creation of sockets, data transmission, and handling responses. Additionally, it touches on multithreading for handling multiple client connections in a TCP server environment.

Embed presentation

Download to read offline
goal: learn how to build client/server applications thatcommunicate using socketssocket: door between application process and end-end-transport protocolInternetcontrolledby OScontrolled byapp developertransportapplicationphysicallinknetworkprocesstransportapplicationphysicallinknetworkprocesssocketSocket Programming
Two socket types for two transport services:• UDP: unreliable datagram• TCP: reliable, byte stream-orientedApplication Example:1. Client reads a line of characters (data) from itskeyboard and sends the data to the server.2. The server receives the data and convertscharacters to uppercase.3. The server sends the modified data to the client.4. The client receives the modified data and displaysthe line on its screen.Socket Programming
UDP: no “connection” between client & server• no handshaking before sending data• sender explicitly attaches IP destination address andport # to each packet• rcvr extracts sender IP address and port# from receivedpacketUDP: transmitted data may be lost or receivedout-of-orderApplication viewpoint:• UDP provides unreliable transfer of groups of bytes(“datagrams”) between client and serverSocket Programming w/ UDP
closeclientSocketread datagram fromclientSocketcreate socket:clientSocket =socket(AF_INET,SOCK_DGRAM)Create datagram with server IP andport=x; send datagram viaclientSocketcreate socket, port= x:serverSocket =socket(AF_INET,SOCK_DGRAM)read datagram fromserverSocketwrite reply toserverSocketspecifyingclient address,port numberserver (running on serverIP) clientSocket Programming w/ UDP
from socket import *serverName = ‘hostname’serverPort = 12000clientSocket = socket(socket.AF_INET,socket.SOCK_DGRAM)message = raw_input(’Input lowercase sentence:’)clientSocket.sendto(message,(serverName, serverPort))modifiedMessage, serverAddress =clientSocket.recvfrom(2048)print modifiedMessageclientSocket.close()Python UDPClientinclude Python’s socketlibrarycreate UDP socket forserverget user keyboardinputAttach server name, port tomessage; send into socketprint out received stringand close socketread reply characters fromsocket into stringSocket Programming w/ UDP
from socket import *serverPort = 12000serverSocket = socket(AF_INET, SOCK_DGRAM)serverSocket.bind(('', serverPort))print “The server is ready to receive”while 1:message, clientAddress = serverSocket.recvfrom(2048)modifiedMessage = message.upper()serverSocket.sendto(modifiedMessage, clientAddress)Python UDPServercreate UDP socketbind socket to local portnumber 12000loop foreverRead from UDP socket intomessage, getting client’saddress (client IP and port)send upper case stringback to this clientSocket Programming w/ UDP
client must contact server• server process must first berunning• server must have createdsocket (door) that welcomesclient’s contactclient contacts server by:• Creating TCP socket, specifyingIP address, port number ofserver process• when client creates socket:client TCP establishesconnection to server TCP• when contacted by client, serverTCP creates new socket forserver process to communicatewith that particular client• allows server to talk withmultiple clients• source port numbers usedto distinguish clients (morein Chap 3)TCP provides reliable, in-orderbyte-stream transfer (“pipe”)between client and serverapplication viewpoint:Socket Programming w/ TCP
wait for incomingconnection requestconnectionSocket =serverSocket.accept()create socket,port=x, for incomingrequest:serverSocket = socket()create socket,connect to hostid, port=xclientSocket = socket()server (running on hostid) clientsend request usingclientSocketread request fromconnectionSocketwrite reply toconnectionSocketTCPconnection setupcloseconnectionSocketread reply fromclientSocketcloseclientSocketSocket Programming w/ TCP
from socket import *serverName = ’servername’serverPort = 12000clientSocket = socket(AF_INET, SOCK_STREAM)clientSocket.connect((serverName,serverPort))sentence = raw_input(‘Input lowercase sentence:’)clientSocket.send(sentence)modifiedSentence = clientSocket.recv(1024)print ‘From Server:’, modifiedSentenceclientSocket.close()Python TCPClientcreate TCP socket forserver, remote port 12000No need to attach servername, portSocket Programming w/ TCP
from socket import *serverPort = 12000serverSocket = socket(AF_INET,SOCK_STREAM)serverSocket.bind((‘’,serverPort))serverSocket.listen(1)print ‘The server is ready to receive’while 1:connectionSocket, addr = serverSocket.accept()sentence = connectionSocket.recv(1024)capitalizedSentence = sentence.upper()connectionSocket.send(capitalizedSentence)connectionSocket.close()Python TCPServercreate TCP welcomingsocketserver begins listening forincoming TCP requestsloop foreverserver waits on accept()for incoming requests, newsocket created on returnread bytes from socket (butnot address as in UDP)close connection to thisclient (but not welcomingsocket)Socket Programming w/ TCP
Multithreading in python
Thread Lifecycle
Multithreading in python
Multithreading in python
Multithreading in python
MultiClient TCP programming in Python
Multithreaded TCP Server in Python
Multithreaded TCP Client in Python

Recommended

PPT
Socket Programming_theory.ppt
PPTX
Networking in Python2025 (programs allll)
PPT
Chapter_2_part5.ppt in the department of computer science
PDF
Socket programming assignment
PPT
Socket programming-tutorial-sk
PDF
JavaSockets-Session10 New York university.pdf
PPTX
Networking in python by Rj
PPTX
EN-04 (1).pptx
PPT
Socket programming
PDF
Socket programming using java
PPTX
Java socket programming
PPT
Socket programming in C
PPT
Application Layer and Socket Programming
PPTX
Socket programming in Java (PPTX)
PPT
Socket programming
PDF
Lecture set 7
PPTX
Network Programming-Python-13-8-2023.pptx
PDF
sockets SMTP Bmsce ppt information science and engineering
PPT
Npc08
PDF
Socket programming
PPTX
#1 (TCPvs. UDP)
PDF
Sockets
PDF
Network Sockets
PPT
Socket Programming
PDF
CHAPTER 24.pdfhehbebbebebbebbeebbebbebeb
PPTX
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PPTX
ensemble learning of machine learning .pptx
PDF
@Regenerative braking system of DC motor

More Related Content

PPT
Socket Programming_theory.ppt
PPTX
Networking in Python2025 (programs allll)
PPT
Chapter_2_part5.ppt in the department of computer science
PDF
Socket programming assignment
PPT
Socket programming-tutorial-sk
PDF
JavaSockets-Session10 New York university.pdf
PPTX
Networking in python by Rj
Socket Programming_theory.ppt
Networking in Python2025 (programs allll)
Chapter_2_part5.ppt in the department of computer science
Socket programming assignment
Socket programming-tutorial-sk
JavaSockets-Session10 New York university.pdf
Networking in python by Rj

Similar to Basics of Socket Programming using python

PPTX
EN-04 (1).pptx
PPT
Socket programming
PDF
Socket programming using java
PPTX
Java socket programming
PPT
Socket programming in C
PPT
Application Layer and Socket Programming
PPTX
Socket programming in Java (PPTX)
PPT
Socket programming
PDF
Lecture set 7
PPTX
Network Programming-Python-13-8-2023.pptx
PDF
sockets SMTP Bmsce ppt information science and engineering
PPT
Npc08
PDF
Socket programming
PPTX
#1 (TCPvs. UDP)
PDF
Sockets
PDF
Network Sockets
PPT
Socket Programming
PDF
CHAPTER 24.pdfhehbebbebebbebbeebbebbebeb
PPTX
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
EN-04 (1).pptx
Socket programming
Socket programming using java
Java socket programming
Socket programming in C
Application Layer and Socket Programming
Socket programming in Java (PPTX)
Socket programming
Lecture set 7
Network Programming-Python-13-8-2023.pptx
sockets SMTP Bmsce ppt information science and engineering
Npc08
Socket programming
#1 (TCPvs. UDP)
Sockets
Network Sockets
Socket Programming
CHAPTER 24.pdfhehbebbebebbebbeebbebbebeb
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA

Recently uploaded

PPTX
ensemble learning of machine learning .pptx
PDF
@Regenerative braking system of DC motor
PPTX
Lead-acid battery.pptx.........................
PDF
Advancements in Telecommunication for Disaster Management (www.kiu.ac.ug)
PPTX
waste to energy deck v.3.pptx changing garbage to electricity
PDF
Soil Permeability and Seepage-Irrigation Structures
PPTX
introduction-to-maintenance- Dr. Munthear Alqaderi
PDF
Best Marketplaces to Buy Snapchat Accounts in 2025.pdf
PDF
Grade 11 Quarter 3 Gravitational Potentional Energy
PDF
application of matrix in computer science
PPTX
31.03.24 - 7.CURRICULUM & TEACHING - LEARNING PROCESS IMPLEMENTATION DETAILS....
PDF
Stern-Gerlach-Experiment from quantum mechanics
PPTX
Control Structures and Looping Basics Understanding Control Flow and Loops Co...
PDF
ANPARA THERMAL POWER STATION[1] sangam.pdf
PPTX
CEC369 IoT P CEC369 IoT P CEC369 IoT PCEC369 IoT PCEC369 IoT P
PPTX
TRANSPORTATION ENGINEERING Unit-5.1.pptx
PDF
Reinforced Earth Walls Notes .pdf
PPTX
Ship Repair and fault diagnosis and restoration of system back to normal .pptx
PPTX
TRANSPORTATION ENGINEERING Unit-5.2.pptx
PPTX
Push pop Unit 1 CEC369 IoT P CEC369 IoT P
ensemble learning of machine learning .pptx
@Regenerative braking system of DC motor
Lead-acid battery.pptx.........................
Advancements in Telecommunication for Disaster Management (www.kiu.ac.ug)
waste to energy deck v.3.pptx changing garbage to electricity
Soil Permeability and Seepage-Irrigation Structures
introduction-to-maintenance- Dr. Munthear Alqaderi
Best Marketplaces to Buy Snapchat Accounts in 2025.pdf
Grade 11 Quarter 3 Gravitational Potentional Energy
application of matrix in computer science
31.03.24 - 7.CURRICULUM & TEACHING - LEARNING PROCESS IMPLEMENTATION DETAILS....
Stern-Gerlach-Experiment from quantum mechanics
Control Structures and Looping Basics Understanding Control Flow and Loops Co...
ANPARA THERMAL POWER STATION[1] sangam.pdf
CEC369 IoT P CEC369 IoT P CEC369 IoT PCEC369 IoT PCEC369 IoT P
TRANSPORTATION ENGINEERING Unit-5.1.pptx
Reinforced Earth Walls Notes .pdf
Ship Repair and fault diagnosis and restoration of system back to normal .pptx
TRANSPORTATION ENGINEERING Unit-5.2.pptx
Push pop Unit 1 CEC369 IoT P CEC369 IoT P

Basics of Socket Programming using python

  • 1.
    goal: learn howto build client/server applications thatcommunicate using socketssocket: door between application process and end-end-transport protocolInternetcontrolledby OScontrolled byapp developertransportapplicationphysicallinknetworkprocesstransportapplicationphysicallinknetworkprocesssocketSocket Programming
  • 2.
    Two socket typesfor two transport services:• UDP: unreliable datagram• TCP: reliable, byte stream-orientedApplication Example:1. Client reads a line of characters (data) from itskeyboard and sends the data to the server.2. The server receives the data and convertscharacters to uppercase.3. The server sends the modified data to the client.4. The client receives the modified data and displaysthe line on its screen.Socket Programming
  • 3.
    UDP: no “connection”between client & server• no handshaking before sending data• sender explicitly attaches IP destination address andport # to each packet• rcvr extracts sender IP address and port# from receivedpacketUDP: transmitted data may be lost or receivedout-of-orderApplication viewpoint:• UDP provides unreliable transfer of groups of bytes(“datagrams”) between client and serverSocket Programming w/ UDP
  • 4.
    closeclientSocketread datagram fromclientSocketcreatesocket:clientSocket =socket(AF_INET,SOCK_DGRAM)Create datagram with server IP andport=x; send datagram viaclientSocketcreate socket, port= x:serverSocket =socket(AF_INET,SOCK_DGRAM)read datagram fromserverSocketwrite reply toserverSocketspecifyingclient address,port numberserver (running on serverIP) clientSocket Programming w/ UDP
  • 5.
    from socket import*serverName = ‘hostname’serverPort = 12000clientSocket = socket(socket.AF_INET,socket.SOCK_DGRAM)message = raw_input(’Input lowercase sentence:’)clientSocket.sendto(message,(serverName, serverPort))modifiedMessage, serverAddress =clientSocket.recvfrom(2048)print modifiedMessageclientSocket.close()Python UDPClientinclude Python’s socketlibrarycreate UDP socket forserverget user keyboardinputAttach server name, port tomessage; send into socketprint out received stringand close socketread reply characters fromsocket into stringSocket Programming w/ UDP
  • 6.
    from socket import*serverPort = 12000serverSocket = socket(AF_INET, SOCK_DGRAM)serverSocket.bind(('', serverPort))print “The server is ready to receive”while 1:message, clientAddress = serverSocket.recvfrom(2048)modifiedMessage = message.upper()serverSocket.sendto(modifiedMessage, clientAddress)Python UDPServercreate UDP socketbind socket to local portnumber 12000loop foreverRead from UDP socket intomessage, getting client’saddress (client IP and port)send upper case stringback to this clientSocket Programming w/ UDP
  • 7.
    client must contactserver• server process must first berunning• server must have createdsocket (door) that welcomesclient’s contactclient contacts server by:• Creating TCP socket, specifyingIP address, port number ofserver process• when client creates socket:client TCP establishesconnection to server TCP• when contacted by client, serverTCP creates new socket forserver process to communicatewith that particular client• allows server to talk withmultiple clients• source port numbers usedto distinguish clients (morein Chap 3)TCP provides reliable, in-orderbyte-stream transfer (“pipe”)between client and serverapplication viewpoint:Socket Programming w/ TCP
  • 8.
    wait for incomingconnectionrequestconnectionSocket =serverSocket.accept()create socket,port=x, for incomingrequest:serverSocket = socket()create socket,connect to hostid, port=xclientSocket = socket()server (running on hostid) clientsend request usingclientSocketread request fromconnectionSocketwrite reply toconnectionSocketTCPconnection setupcloseconnectionSocketread reply fromclientSocketcloseclientSocketSocket Programming w/ TCP
  • 9.
    from socket import*serverName = ’servername’serverPort = 12000clientSocket = socket(AF_INET, SOCK_STREAM)clientSocket.connect((serverName,serverPort))sentence = raw_input(‘Input lowercase sentence:’)clientSocket.send(sentence)modifiedSentence = clientSocket.recv(1024)print ‘From Server:’, modifiedSentenceclientSocket.close()Python TCPClientcreate TCP socket forserver, remote port 12000No need to attach servername, portSocket Programming w/ TCP
  • 10.
    from socket import*serverPort = 12000serverSocket = socket(AF_INET,SOCK_STREAM)serverSocket.bind((‘’,serverPort))serverSocket.listen(1)print ‘The server is ready to receive’while 1:connectionSocket, addr = serverSocket.accept()sentence = connectionSocket.recv(1024)capitalizedSentence = sentence.upper()connectionSocket.send(capitalizedSentence)connectionSocket.close()Python TCPServercreate TCP welcomingsocketserver begins listening forincoming TCP requestsloop foreverserver waits on accept()for incoming requests, newsocket created on returnread bytes from socket (butnot address as in UDP)close connection to thisclient (but not welcomingsocket)Socket Programming w/ TCP
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

[8]ページ先頭

©2009-2025 Movatter.jp