342x Filetype PDF File size 0.50 MB Source: www.csc.villanova.edu
CSC5930/9010: Security and Privacy in
Cyber-physical Systems
Socket Programming using Python
Socket Programming
• Sockets
– A means of sending data over a network from one application to another.
– A door between application process and end-end-transport protocol
• One socket node listens on a particular port and IP, while other socket
reaches out to the other to form a connection.
application socket application controlled by
process process app developer
transport transport
network network controlled
link Internet link by OS
physical physical
Slide 2
Client/server socket interaction: UDP
server (runningon serverIP) client
create socket, port= x: create socket:
serverSocket = clientSocket =
socket(AF_INET,SOCK_DGRAM) socket(AF_INET,SOCK_DGRAM)
Create datagram with server IP and
port=x; send datagram via
read datagram from clientSocket
serverSocket
write reply to
serverSocket read datagram from
specifying clientSocket
client address,
port number close
clientSocket
Slide 3
Example app: UDP Client
Python UDPClient
include Python’s socket from socket import *
library
serverName = ‘hostname’
serverPort = 12000
create UDP socket for clientSocket = socket(AF_INET,
server SOCK_DGRAM)
get user keyboard message = input(’Input lowercase sentence:’)
input clientSocket.sendto(str.encode(message),
Attach server name, port to (serverName, serverPort))
message; send into socket
modifiedMessage, serverAddress =
read reply characters from clientSocket.recvfrom(2048)
socket into string print (bytes.decode(modifiedMessage))
clientSocket.close()
print out received string
and close socket
2-4 Slide 4
no reviews yet
Please Login to review.