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)
PDF
JavaSockets-Session10 New York university.pdf
PPT
Chapter_2_part5.ppt in the department of computer science
PPTX
Networking in python by Rj
PDF
Socket programming assignment
PPT
Socket programming-tutorial-sk
PPTX
Java socket programming
PDF
Socket programming using java
PPT
Socket programming
PDF
Lecture set 7
PPT
Socket programming in C
PPT
Npc08
PPT
Socket Programming
PPT
Application Layer and Socket Programming
PPT
Socket programming
PPTX
Network Programming-Python-13-8-2023.pptx
PPTX
Socket programming in Java (PPTX)
PPTX
#1 (TCPvs. UDP)
PDF
Network Sockets
PDF
CHAPTER 24.pdfhehbebbebebbebbeebbebbebeb
PDF
Socket programming
PPTX
EN-04 (1).pptx
PDF
sockets SMTP Bmsce ppt information science and engineering
PPTX
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PDF
Sockets
PDF
PRIZ Academy - Thinking The Skill Everyone Forgot
PPTX
CEC369 IoT P CEC369 IoT P CEC369 IoT PCEC369 IoT PCEC369 IoT P

More Related Content

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

Similar to Basics of Socket Programming using python

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

Recently uploaded

PDF
PRIZ Academy - Thinking The Skill Everyone Forgot
PPTX
CEC369 IoT P CEC369 IoT P CEC369 IoT PCEC369 IoT PCEC369 IoT P
PPTX
Computer engineering for collage studen. pptx
PDF
Event #3_ Build a Gemini Bot, Together with GitHub_private.pdf
PPTX
DevFest Seattle 2025 - AI Native Design Patterns.pptx
PDF
Introduction to MySQL Spatial Features and Real-World Use Cases
PDF
Stern-Gerlach-Experiment from quantum mechanics
PPTX
Supercapacitor.pptx...............................
PDF
application of matrix in computer science
PPTX
Generative AI Deep Dive: Architectures, Mechanics, and Future Applications
PDF
Soil Permeability and Seepage-Irrigation Structures
PPTX
Lead-acid battery.pptx.........................
PPTX
TRANSPORTATION ENGINEERING Unit-5.2.pptx
PPTX
introduction-to-maintenance- Dr. Munthear Alqaderi
PPTX
31.03.24 - 7.CURRICULUM & TEACHING - LEARNING PROCESS IMPLEMENTATION DETAILS....
PPTX
Principles of Energy Efficiency_ Doing More with Less
PDF
Reinforced Earth Walls Notes .pdf
PPTX
2-Photoelectric effect, phenomena and its related concept.pptx
PPTX
Waste to Energy - G2 Ethanol.pptx to process
PPTX
ensemble learning of machine learning .pptx
PRIZ Academy - Thinking The Skill Everyone Forgot
CEC369 IoT P CEC369 IoT P CEC369 IoT PCEC369 IoT PCEC369 IoT P
Computer engineering for collage studen. pptx
Event #3_ Build a Gemini Bot, Together with GitHub_private.pdf
DevFest Seattle 2025 - AI Native Design Patterns.pptx
Introduction to MySQL Spatial Features and Real-World Use Cases
Stern-Gerlach-Experiment from quantum mechanics
Supercapacitor.pptx...............................
application of matrix in computer science
Generative AI Deep Dive: Architectures, Mechanics, and Future Applications
Soil Permeability and Seepage-Irrigation Structures
Lead-acid battery.pptx.........................
TRANSPORTATION ENGINEERING Unit-5.2.pptx
introduction-to-maintenance- Dr. Munthear Alqaderi
31.03.24 - 7.CURRICULUM & TEACHING - LEARNING PROCESS IMPLEMENTATION DETAILS....
Principles of Energy Efficiency_ Doing More with Less
Reinforced Earth Walls Notes .pdf
2-Photoelectric effect, phenomena and its related concept.pptx
Waste to Energy - G2 Ethanol.pptx to process
ensemble learning of machine learning .pptx

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