0% found this document useful (0 votes)
15 views10 pages

Exp 7

The document outlines an experiment aimed at demonstrating basic socket communication between a server and client using Python. It covers prerequisites, expected outcomes, theory, procedures for creating and managing sockets, and the data exchange process. Additionally, it includes sections for student documentation, observations, conclusions, and questions related to socket programming and WebSocket usage in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

Exp 7

The document outlines an experiment aimed at demonstrating basic socket communication between a server and client using Python. It covers prerequisites, expected outcomes, theory, procedures for creating and managing sockets, and the data exchange process. Additionally, it includes sections for student documentation, observations, conclusions, and questions related to socket programming and WebSocket usage in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PART A

(PART A : TO BE REFFERED BY STUDENTS)

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

To demonstrate basic concepts in python looping.

A.4 Theory& Procedure:

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.

The socket Module


To create a socket, you must use the socket.socket() function available in socket

s = socket.socket (socket_family, socket_type, protocol=0)


Here is the description of the parameters
● socket_family
o socket_type
● protocol

Server Socket Methods


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

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)

Roll No.: C-33 Name: Ashish Gajanan Bhosale

Class :SE-C Batch :C-2

Date of Experiment: 26.03.2024 Date of Submission:29.03.2024

Grade :
B.1 Document created by the student:

Server part:-
Output:-
Client Part:-
Output:-

B.3 Observations and learning:


(Students are expected to understand the selected topic. Have to list out the components & functionality.
Prepare a flow of the algorithm defined in the paper. List the performance metrics that is used)

1. Initialization:

- Both server and client create socket objects (`socket.socket()`).

- Server binds to a specific host and port (`bind()`), then listens for connections (`listen()`).

- Client connects to the server using `connect()`.

2. Data Exchange:

- Server sends a message to the client using `send()`.

- Client receives the message using `recv()`.

- Client sends a response message to the server.


- Server receives the response message.

3. Encoding and Decoding:

- 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:

- Server can handle multiple client connections sequentially.

- 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)

In conclusion, the provided server-client socket communication example demonstrates a

fundamental implementation of network communication using sockets in Python. By


following

the steps outlined in the example:

1. Initialization of socket objects.

2. Establishment of connections between the server and client.

3. Exchange of data between them.

4. Proper encoding and decoding of data.

5. Closure of connections after communication.

This example serves as a foundational understanding of socket programming, which can be


further extended and adapted for various network applications. However, it's important to
note

that for real-world applications, additional considerations such as error handling, security

measures, and scalability need to be incorporated. Overall, socket programming enables


efficient

communication between networked devices and forms the basis for many client-server

interactions in computer networks.

B.5 Question of Curiosity


1. Write an python script to connect to Google using socket programming.

2. What is WebSocket and how to use it in Python?

WebSocket is a communication protocol that provides full-duplex, bidirectional communication

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

install it using pip:

pip install websockets;

3. How to implement a WebSocket server using Tornado?

You might also like