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
PDF
JavaSockets-Session10 New York university.pdf
PPTX
Networking in Python2025 (programs allll)
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
#1 (TCPvs. UDP)
PPTX
Socket programming in Java (PPTX)
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
PDF
JavaSockets-Session10 New York university.pdf
PPTX
Networking in Python2025 (programs allll)
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
JavaSockets-Session10 New York university.pdf
Networking in Python2025 (programs allll)
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
#1 (TCPvs. UDP)
PPTX
Socket programming in Java (PPTX)
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
#1 (TCPvs. UDP)
Socket programming in Java (PPTX)
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
PPT
Virtual Instrumentation Programming Techniques.ppt
PPTX
Washing-Machine-Simulation-using-PICSimLab.pptx
PDF
ANPARA THERMAL POWER STATION[1] sangam.pdf
PPTX
DevFest Seattle 2025 - AI Native Design Patterns.pptx
PDF
OOPCodesjavapracticalkabirpawarpptinparacticalexamination
PDF
application of matrix in computer science
PDF
HEV Descriptive Questions https://www.slideshare.net/slideshow/hybrid-electr...
PDF
Small Space Big Design - Amar DeXign Scape
PPTX
Lead-acid battery.pptx.........................
PPT
399-Cathodic-Protection-Presentation.ppt
 
PPTX
clustering type :hierarchical clustering.pptx
PPTX
Mc25104 - data structures and algorithms using PYTHON OOP_Python_Lecture_Note...
PDF
November_2025 Top 10 Read Articles in Computer Networks & Communications.pdf
PPTX
Intrusion Detection Systems presentation.pptx
PDF
Reinforced Earth Walls Notes .pdf
PDF
Grade 11 Quarter 3 Gravitational Potentional Energy
PPTX
Presentation 1.pptx WHAT IS ARTIFICIAL INTELLIGENCE?
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
Virtual Instrumentation Programming Techniques.ppt
Washing-Machine-Simulation-using-PICSimLab.pptx
ANPARA THERMAL POWER STATION[1] sangam.pdf
DevFest Seattle 2025 - AI Native Design Patterns.pptx
OOPCodesjavapracticalkabirpawarpptinparacticalexamination
application of matrix in computer science
HEV Descriptive Questions https://www.slideshare.net/slideshow/hybrid-electr...
Small Space Big Design - Amar DeXign Scape
Lead-acid battery.pptx.........................
399-Cathodic-Protection-Presentation.ppt
 
clustering type :hierarchical clustering.pptx
Mc25104 - data structures and algorithms using PYTHON OOP_Python_Lecture_Note...
November_2025 Top 10 Read Articles in Computer Networks & Communications.pdf
Intrusion Detection Systems presentation.pptx
Reinforced Earth Walls Notes .pdf
Grade 11 Quarter 3 Gravitational Potentional Energy
Presentation 1.pptx WHAT IS ARTIFICIAL INTELLIGENCE?
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