0% found this document useful (0 votes)
10 views

Experiment 6

Raspberry Pi set up Experiment in headless mode

Uploaded by

Kalpesh Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Experiment 6

Raspberry Pi set up Experiment in headless mode

Uploaded by

Kalpesh Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CVM University IOT Minor Degree Programme 12 August 2022

SOCKET
PROGRAMMING
EXPERIMENT-6
CVM University IOT Minor Degree Programme 12 August 2022

• Python provides two levels of access to network services.


• At a low level, user can access basic socket support in the Raspbian OS
• Sockets allow to implement client and server for both connection-oriented and
connectionless protocols.
• Python has libraries that provide higher-level access to specific application-level
network protocols, such as FTP, HTTP, and so on
CVM University IOT Minor Degree Programme 12 August 2022

3 WHAT IS SOCKETS?

• Sockets are the endpoints of a bidirectional communications channel.


• Sockets may communicate within a process, between processes on the same
machine, or between processes on different continents.
• Sockets may be implemented over a number of different channel types: Unix
domain sockets, TCP, UDP, and so on.
• The socket library provides specific classes for handling the common transports
as well as a generic interface for handling the rest.
• Sockets have their own vocabulary
CVM University IOT Minor Degree Programme 12 August 2022

4 TERM & DESCRIPTION

• Domain : Family of protocols which are used as transport mechanism. These values are
constants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.
• Type: Type of communications between the two endpoints, typically SOCK_STREAM for
connection-oriented protocols and SOCK_DGRAM for connectionless protocols
• Protocol: Typically, zero. Used to identify a variant of a protocol within a domain and type.
• Hostname: The identifier of a network interface −
• A string, which can be a host name, a dotted-quad address, or an IPV6 address in colon (and
possibly dot) notation
• . Port: Each server listens for clients calling on one or more ports. A port may be a Fixnum port
number, a string containing a port number, or the name of a service.
CVM University IOT Minor Degree Programme 12 August 2022

5 THE SOCKET MODULE

• To create a socket, use the socket.socket() function available in socket module,


which has the general syntax s = socket.socket (socket_family, socket_type, protocol=0)
• socket_family − This is either AF_UNIX or AF_INET
• socket_type − This is either SOCK_STREAM or SOCK_DGRAM
• protocol − This is usually left out, defaulting to 0.
• Once socket object is created , then required functions to create client or server
program can be used
CVM University IOT Minor Degree Programme 12 August 2022

6
SERVER SOCKET METHODS GENERAL SOCKET METHODS
• s.recv(): Receives TCP message
• s.bind() : binds address (hostname,
port number pair) to socket. • s.send() : Transmits TCP message
• s.listen() : sets up and start TCP
• s.recvfrom() : Receives UDP message
listener
• s.accept() : accept TCP client • s.sendto() : Transmits UDP message
connection, wait until connection
• s.close(): closes socket
arrives (blocking).
Client Socket Methods • socket.gethostname(): Returns the
• s.connect() : Actively initiates TCP
hostname
server connection.
CVM University IOT Minor Degree Programme 12 August 2022

7 A SIMPLE SERVER

• #!/usr/bin/python # This is server.py file • while True:


• import socket # Import socket module • c, addr = s.accept() # Establish connection
• s = socket.socket() # Create a socket object with client.

• host = socket.gethostname() # Get local machine • print 'Got connection from', addr
name • c.send('Thank you for connecting')
• port = 12345 # Reserve a port for your service. • c.close() # Close the connection
• s.bind((host, port)) # Bind to the port
• s.listen(5) # Now wait for client
connection.
CVM University IOT Minor Degree Programme 12 August 2022

8 A SIMPLE CLIENT

• #!/usr/bin/python # This is client.py file


• import socket # Import socket module
• s = socket.socket() # Create a socket object
• host = socket.gethostname() # Get local machine name
• port = 12345 # Reserve a port for your service.
• s.connect((host, port))
• print s.recv(1024)
• s.close() # Close the socket when done

You might also like