You are on page 1of 29

 Any request/response paradigm ==> client/server roles

 One machine will be the server, listening in for connections and the other, the client,
attempting to make a connection with the server.
 Can happen with different protocols eg: HTTP (Web), TCP/IP using sockets, etc.
 Hyper Text Transfer Protocol (HTTP) is a protocol used for transferring web page between a client
and server.
 Web servers are needed for the Clients and servers to communicate using the HyperText
Transfer Protocol (HTTP)
 For e.g.: Fetching a web page. Your web browser, or the client, attempts to make a connection to
the web server. The web server, listening in for clients, will accept the connection and then
proceed to handle the clients request. Here, the browser will ask the web server for the page,
before the web server responds with the page data. This is basic HTTP polling mechanism that
happens for the web-based client-server communication with client being the web browser.
 Servers are accessed via socket addresses, a combination of the server's IP address (or domain
name) and a port number. The port can be thought as a connection point on the sever, like USB or
Firewire ports, with each port serving a specific purpose. For example, web pages are served on
port 80 (HTTP) . Ports numbers can be customized and chosen by the user as we did in case of
running Flask server on RPi.

Socket Address
 Using the Web Servers such as
Flask, Clients and servers
communicate using the HyperText
Transfer Protocol (HTTP)
1. Client and server establish a TCP
connection
2. TCP stands for Transmission Control
protocol that defines how to establish
and maintain a network conversation
via which application programs can
exchange data.
3. Client requests content using the GET
HTTP method
4. Server responds with requested
content using POST or PUT HTTP
method
5. Client and server close connection (its
automatically terminated each
communication session)
 Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate
within a process (or an application), between processes on the same machine, or between
processes on different machines.
 One machine will be the server, listening in for connections and the other, the client, attempting
to make a connection with the server.
 A socket is an endpoint of a connection between two processes. These two processes or
applications can be running on two different machines (Client-Server role) or between two
processes on the same machine.
 Most clients and servers communicate by sending streams of bytes over connections – E.g.,
using TCP, the Transmission Control Protocol .
 Sockets may be implemented over a number of different channel types: Unix
domain sockets, TCP, UDP, and so on.
 With socket connection one can design your own protocol for network connection
between two systems, by default it uses TCP protocol.
Sockets

Persistent
connection
TCP Sockets HTTP (FLASK)
Socket is used to transport data between systems HTTP connection is a higher-level abstraction of a
(Peer Communication (P2P) between any two network connection.
applications running on two machines). It simply
connects two systems together, an IP address is the
address of the machine over an IP based network.
sockets go on the level lower details of TCP/IP With HTTP connection the implementation takes
connection and actually control the connection and care of all these higher-level details and simply
send/receive raw data in bytes. send HTTP request (some header information) and
receive HTTP response from the server.
It's session-less which means you send text request
like GET google.com and receive text or binary data
or HTML pages in return, after that connection is
closed
TCP Sockets HTTP (FLASK)
One time handshake to establish connection, There is a constant polling between the
persistent connection between client and server client and the server.
until one of them chooses to close/terminate. Client Web Browsers would have to poll the
server for new information by repeating
requests every so many seconds or
minutes to see if there was anything new.
The opening and closing creates overhead,
and for certain applications, especially
those that want rapid responses or real
time interactions or display streams of data,
this just doesn’t work.
 Socket protocol is very flexible for transferring data to and from servers from the browser, as well
as Peer-to-Peer (P2P).
 Unlike HTTP, the socket that is connected to the server that stays “open” for communication. That
means data can be “pushed” to the client in realtime on demand, over a persistent connection
 The advantage of a regular TCP socket has over HTTP is that
 The Socket is full-duplex (allows for simultaneous two-way communication)
 The overhead of opening and closing connections can be avoided in sockets
 Only one time “Hand-shake” is required to establish a connection in sockets
 Once a connection is established, the connection persists, until specified to close the connection by either
client or the server.
 It is TCP based, bi-directional, full-duplex messaging
 Hostname: This refers to the IP address of the Server machine. It acts like an
identifier for a network interface.
 Port: Transport layer protocols such as Transmission Control Protocol (TCP) needs
to specify a source and destination numbers in their headers. A port number is a
16-bit unsigned integer, thus ranging from 0 to 65535.Each Server listens for one or
more clients on a port number.
 Socket address: Socket address of a server is a combination of the server's IP
address (or domain name) and a port number.
 To create a socket, you must use the socket.socket() function available
in socket module, which has the general syntax −

s=socket.socket(AF_INET,SOCK_STREAM)

 socket_family: This is either AF_UNIX or AF_INET protocol used as transport


mechanism
 socket_type: This is either SOCK_STREAM or SOCK_DGRAM.
 protocol: This is usually left out, defaulting to 0.

 Once the socket object is created, then you can use required functions to create
your client or server program
 Once a socket instance is created on the server machine, the server (program) running on
that specific machine (RPi) will have a socket that is bound to a specific port. The server
waits and listens to the socket for a client to make a connection request.

# Server Side:
s=socket.socket(AF_INET,SOCK_STREAM) # Creating a socket instance
s.bind(host IP address, port #) # bind the socket to server IP address (EG: “192.168.25.67”) and port
s.listen(int number_of_clients) # listens to the socket for a client to make a connection request
 If the client makes the request with the correct host server’s IP, and port number, the
server accepts the connection from the client.
 Upon acceptance, the server gets a new socket bound to a different port. It needs a new
socket (consequently a different port number assigned by the OS ) so that it can continue
to listen to the original socket (assigned by the user) for connection requests while
serving the connected client.
 Client will also be assigned with a port number from its OS after the connection is
established and accepted by the Server host machine.

Server Side: Client Side:


s.accept() # accept TCP client connection s=socket.socket(AF_INET,SOCK_STREAM)
# Creating a socket instance on client
S.connect(hostIP,hostPortNumber)
# Starts a TCP connection on server IP address, port #.
s=socket.socket(AF_INET,SOCK_STREAM) # Creates a Socket Instance ‘s’

Method Description

s.bind(host IP address, port #) Bind socket to IP address/port


This method binds socket address (hostname, port
number pair) to socket

s.listen(int number_of_clients) Mark the socket as accepting connections. This


method sets up and starts TCP listener. The parameter
number_of_clients are the number of clients server is
expecting incoming connections from
s.accept() This passively accept TCP client connection, waiting
until connection arrives. This function returns a new
socket on which it will listen on at the client.
Method Description

s.connect() This method actively initiates TCP server


connection.
Method Description

s.recv(size_of _message_in_bytes) This method receives TCP message in String.


Maximum length of message is 1024 bytes.

s.send(size_of _message_in_bytes) This method transmits TCP message in String.


Maximum length of message is 1024 bytes.

s.close() This method closes socket connection.

s.gethostname() Returns the hostname on server machine.


 Binds a newly created socket to the specified address.

 s=socket.socket(AF_INET,SOCK_STREAM) # Creating a socket instance


 int hostIP= s.gethostname() # Returns the IP address of the server (host machine)
 s.bind(hostIP, 5020) # Bind the socket ‘s’ to hostIP and port # 5020
 Used by connection-oriented servers to indicate an application on server is
willing to receive connections

 s=socket.socket(AF_INET,SOCK_STREAM) # Creating a socket instance


 int hostIP= s.gethostname() # Returns the IP address of the server (host machine)
 s.bind(hostIP, 5020) # Bind the socket ‘s’ to hostIP and port # 5020
 s.listen(5) # Indicates that the server is running and willing to
accept connections from maximum 5 clients. 5 is the
number of connection requests that can be queued
by the system while waiting for server to execute
accept call.
 After executing listen, the accept call carries out a passive open (server prepared to accept
connects).
 It returns with a new socket that corresponds with new connection and the address contains
the clients address.
 s=socket.socket(AF_INET,SOCK_STREAM) # Creating a socket instance
 int hostIP= s.gethostname() # Returns the IP address of the server (host machine)
 s.bind(hostIP, 5020) # Bind the socket ‘s’ to hostIP and port # 5020
 s.listen(5) # Indicates that the server is running and willing to
accept connections from maximum 5 clients. 5 is the
number of connection requests that can be queued
by the system while waiting for server to execute
accept call.
 q,addr=s.accept() # Returns the new socket on which it receives messages
from client in ‘q’ variable. And ‘addr’ is the client address
it receives after connection is established
 Client executes an active open of a connection
 Client OS usually selects random, unused port on the client to establish connection
between the server-client.

 int hostIP= “192.162.1.103” # Assign host(Server) IP address


 int hostPortNumber=5020 # Assign host Port Number
 s=socket.socket(AF_INET,SOCK_STREAM) # Creating a socket instance on client
 S.connect(hostIP,hostPortNumber) # Starts a TCP connection on host IP
address and host port number . The
port on client machine is chosen by the OS.
 After connection has been made, client-server application can use send/recv to
data.

Server (Master/host) Client(Slave)

s=socket.socket(AF_INET,SOCK_STREAM) # Creating a socket instance


int hostIP= “192.162.1.103” # Assign host(Server) IP address
int hostIP= s.gethostname() # Returns the IP address of the server (host
int hostPortNumber=5020 # Assign host Port Number
machine)
s=socket.socket(AF_INET,SOCK_STREAM) # Creating a socket instance on client
s.bind(hostIP, 5020) # Bind the socket ‘s’ to hostIP and port # 5020
S.connect(hostIP,hostPortNumber) # Starts a TCP connection on host IP
s.listen(5) # Indicates that the server is running and
address and host port number . The
accept connections from maximum 5 clients.
port on client machine is chosen by the OS.
q,addr=s.accept() # Returns the new socket on which it receives
s.send( “ Hi Server, this is client”) # send
messages from client in ‘q’ variable. And ‘addr’
Msg=s.recv(1024) # receiving message from server
is the client address it receives after connection
is established
msg=q.recv(1024) # Receive message from specified socket ‘q’ .
Maximum size of message to be received is 1024 bytes.
q.send(“Hi Client, this is server”) # Send message from specified socket ‘q’
Write a socket programming based on Weather Station that uses a client/server connection such that
the client allows two capabilities: 1. Auto Control and 2. Manual Control.
• For Auto Control (Based on Socket Connection): The Client RPi interfaced with the
Temperature sensor sends temperature readings to the Server RPi using the TCP socket
connection, on port 12333.
The server Rpi receives the temperature readings and sends control message to the client based
on the temperature value; 1-Turn ON the fan and 0-Turn OFF the fan.

• For Manual Control (Based on Webserver): The Client RPi will start a web server (Flask) and
create two webpages; one of which will display the temperature values in real time and second
will be on a dynamic route path to control the status of the fan.
Temperature Sensor

3.3V Client Server

Ch-2 Socket: Initial Handshake

Socket: s.sendall(buf) % send temp

Socket: q.send(“1”) % send control

GND
DC Brushless Fan
 https://docs.python.org/3/howto/sockets.html
 https://docs.python.org/3/library/socket.html
 https://www.tutorialspoint.com/python/python_networking.htm
 https://users.cs.duke.edu/~chase/cps196/slides/sockets.pdf

You might also like