Exp 7
Exp 7
Experiment No.07
A.1 Aim:
To demonstrate a simple socket for basic information exchange between server and client.
A.2 Prerequisite:
1. python basics and control structures
A.3 Outcome:
After successful completion of this experiment students will be able to
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.
Domain
The family of protocols that is used as the transport mechanism. These values are constants such
as AF_INET, PF_INET
type
The type of communications between the two endpoints, typically SOCK_STREAM for
connection-oriented protocols and SOCK_DGRAM for connectionless protocols.
protocol
Typically zero, this may be 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
● A string "<broadcast>", which specifies an INADDR_BROADCAST address.
● A zero-length string, which specifies INADDR_ANY, or
● An Integer, interpreted as a binary address in host byte order.
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.
s.listen()
This method sets up and start TCP listener.
s.accept()
This passively accept TCP client connection, waiting until connection arrives (blocking).
Client Socket Methods
s.connect()
This method actively initiates TCP server connection.
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the practical. The
soft copy must be uploaded on the Blackboard or emailed to the concerned lab in charge faculties at
the end of the practical in case the there is no Black board access available)
Grade :
B.1 Document created by the student:
Server part:-
Output:-
Client Part:-
Output:-
1. Initialization:
- Server binds to a specific host and port (`bind()`), then listens for connections (`listen()`).
2. Data Exchange:
- Data exchanged is encoded to bytes before sending (`encode()`), decoded back to strings
after
receiving (`decode()`).
4. Closing Connections:
- Both server and client close the connection using `close()` after data exchange.
5. Observations:
- Error handling and security measures are not included in this basic example.
B.4 Conclusion:
(Students must write the conclusion as per the attainment of individual outcome listed above and
learning/observation noted in section B.3)
that for real-world applications, additional considerations such as error handling, security
communication between networked devices and forms the basis for many client-server
channels over a single TCP connection. Unlike HTTP, which follows the request-response
model,
WebSocket allows the server to send data to the client and vice versa without the need for the
client to continually request updates. This makes WebSocket ideal for real-time applications
where data needs to flow between the client and server in both directions.
To use WebSocket in Python, we can rely on the popular library called “websockets.” You can