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

Week-9 LDQ: 1. Socket Programming ? Ans

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

Week-9 LDQ: 1. Socket Programming ? Ans

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/ 6

WEEK-9 LDQ

1. Socket Programming ?

Ans:

Socket programming is a way of connecting two nodes on a network to


communicate with each other. One socket(node) listens on a particular port
at an IP, while the other socket reaches out to the other to form a connec-
tion. The server forms the listener socket while the client reaches out to the
server.

They are the real backbones behind web browsing. In simpler terms, there
is a server and a client.

2. What are the primary socket API functions ?

Ans: The primary socket API functions and methods in this module are:

• socket()
• .bind()
• .listen()
• .accept()
• .connect()
• .connect_ex()
• .send()
• .recv()
• .close()
3. Why should you use TCP?

Ans: This high-level protocol is a core element in establishing regulations


and standards for data transfers between multiple programs over networks.
For web access, HTTP and HTTPS rely on the TCP connection.

As a reliable protocol, it ensures communication occurs consistently, re-


gardless of the devices and locations. Furthermore, it protects data delivery
with advanced controls, such as acknowledgments and congestion avoid-
ance mechanisms.

Aside from being non-proprietary, TCP also allows and accommodates


new protocols.

4. Write about Echo Client and Server

Ans: An echo client and server is a simple communication protocol in


which the server echoes back any message sent to it by the client. This
protocol is commonly used for testing and debugging network applica-
tions. In Python, we can implement an echo client and server using the
socket module.

Echo Server in Python:

The server listens on a specific port for incoming connections from clients.
When a client connects, the server reads the data sent by the client, echoes
it back to the client, and then closes the connection. Here's an example im-
plementation of an echo server in Python:

Example:

import socket
HOST = '' # The server's hostname or IP address

PORT = 5000 # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:

s.bind((HOST, PORT))

s.listen()

print(f'Server listening on port {PORT}')

conn, addr = s.accept()

with conn:

print(f'Connected by {addr}')

while True:

data = conn.recv(1024)

if not data:

break

conn.sendall(data)

The above code listens on port 5000 for incoming connections. When a
connection is established, it reads data from the connection using the
recv() method, and echoes it back to the client using the sendall() method.
The connection is closed when the client sends an empty message.

Echo Client in Python:


The client connects to the server and sends a message. When the client re-
ceives a response from the server, it prints the message to the console.
Here's an example implementation of an echo client in Python:

Example:

import socket

HOST = 'localhost' # The server's hostname or IP address

PORT = 5000 # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:

s.connect((HOST, PORT))

message = input("Enter message to send: ")

s.sendall(message.encode())

data = s.recv(1024)

print(f'Received: {data.decode()}’)

The above code connects to the server at localhost on port 5000. It prompts
the user to enter a message to send to the server, and sends it using the
sendall() method. The client then waits for a response from the server using
the recv() method, and prints the response to the console.

Overall, an echo client and server is a simple communication protocol that


can be used for testing and debugging network applications. The Python
socket module provides a simple way to implement this protocol.
5. Explain TCP Socket Flow?

Ans: The left-hand column represents the server. On the right-hand side is
the client.

Starting in the top left-hand column, note the API calls the server makes to
setup a “listening” socket:

• socket()
• bind()
• listen()
• accept()

A listening socket does just what it sounds like. It listens for connections
from clients.

When a client connects, the server calls accept() to accept, or complete, the
connection.

The client calls connect() to establish a connection to the server and initi-
ate the three- way handshake. The handshake step is important since it en-
sures that each side of the connection is reachable in the network, in other
words that the client can reach the server and vice-versa. It may be that
only one host, client or server, can reach the other.

In the middle is the round-trip section, where data is exchanged between


the client and server using calls to send() and recv().

At the bottom, the client and server close() their respective sockets.

6. What is the purpose of bind() method and its parameter?


Ans:

• The bind() method of Python's socket class assigns an IP address and a


port number to a socket instance.
• The bind() method is used when a socket needs to be made a server
socket.
• As server programs listen on published ports, it is required that
a port and the IP address to be assigned explicitly to a server socket.
• For client programs, it is not required to bind the socket explicitly to a
port. The kernel of the operating system takes care of assigning the
source IP and a temporary port number.
• The client socket can use the connect() method, after the socket cre-
ation is complete to contact the server socket.

You might also like