Professional Documents
Culture Documents
Network Paradigms
Network Paradigms
TextBook:
• Types
– LAN (Local Area Network)
– WAN (Wide Area Network)
– MAN (Metropolitan Area Network)
– PAN(Personal Area Network)
What is Socket?
Order Not ordered: If two messages are sent Ordered: The messages are
to the same recipient, the order in delivered in a particular order
which they arrive cannot be predicted. in which they were meant to
be.
• sock_object.connect():
– This method is used to connect the client to host and port
– Initiate the connection towards the server.
General Socket Methods
• sock_object.recv():
– Use this method to receive messages at endpoints when the value of the
protocol parameter is TCP.
• sock_object.send():
– Apply this method to send messages from endpoints in case the protocol is TCP.
• sock_object.recvfrom():
– Call this method to receive messages at endpoints if the protocol used is UDP.
• sock_object.sendto():
– Invoke this method to send messages from endpoints if the protocol parameter
is UDP.
• sock_object.gethostname():
– This method returns hostname.
• sock_object.close():
– This method is used to close the socket. The remote endpoint will not receive
data from this side.
Python socket module
• socket.bind(address)
– e.g. socket.bind((host, port))
• socket.listen(backlog)
– backlog specifies wait queue size
– e.g. socket.listen(5)
• socket.accept()
– Blocks until a client makes a connection
– Returns (conn, address) where conn is a new socket object
usable to send and receive data
– address is the address bound to the socket on the other end of
the connection
Socket Object Methods
import socket
import sys
# Create a TCP/IP socket
stream_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'localhost‘ # Define host
port = 8080 # define the communication port
# Connect the socket to the port where the server is listening
server_address = ((host, port))
print "connecting”
stream_socket.connect(server_address)
message = 'message‘ # Send data
stream_socket.sendall(message)
data = stream_socket.recv(10) # response
print data
print 'socket closed'
stream_socket.close()
Build a Simple Server
• s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
• s.bind(): Binds address (hostname, port number) to socket.
• s.listen(): Sets up and starts TCP listener.
• s.accept(): Accepts TCP client connection.
• Steps:
– Create a socket.
– Bind the socket to a port
– Start accepting connections on the socket.
Server Program
import socket
import sys
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a TCP/IP socket
host = 'localhost‘
port = 8080
sock.bind((host, port))
sock.listen(1)
print 'waiting for a connection'
connection, client = sock.accept()
print client, 'connected'
# Receive the data in small chunks and retransmit it
data = connection.recv(16)
print 'received "%s"' % data
if data:
connection.sendall(data)
else:
print 'no data from', client
# Close the connection
connection.close()
Resources
• Useful Resources
• Python socket module documentation
• Python Network Programming Cookbook
• Simple Client-Server Example
• Packet Sniffer Example
• File Transfer Example
• Unix Sockets Tutorial
Quiz URL
https://www.techbeamers.com/python-program
ming-quiz-for-beginners-part-1/