Professional Documents
Culture Documents
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)
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.
Method Description
• 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
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