0% found this document useful (0 votes)
28 views3 pages

Exp 1.1

cn experiment for practice
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

Exp 1.1

cn experiment for practice
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

16/09/2024, 12:57 HIRU-VIRU/SocketStudy

Ex.No:1a Study of Socket Programming


Aim:
To perform a study on Socket Programming
Introduction:
Socket programming is a crucial aspect of network communication, allowing for data excha

Understanding Socket Programming:


Socket programming involves the use of sockets, which serve as endpoints for communicatio

Key Concepts in Socket Programming:


1.Sockets • A socket is a software representation of a communication endpoint in a network. • It is
identified by an IP address and a port number. • Sockets can be classified into two main types:
Stream Sockets and Datagram Sockets. • Stream Sockets provide a reliable, connection-oriented
communication, while Datagram Sockets are connectionless and operate in a best-effort mode.
2. Client-Server Model
• Socket programming typically follows the client-server model. • The server listens for incoming
connections from clients, while clients initiate connections to the server. • Servers are passive,
waiting for connection requests, and clients are active, initiating communication.
3, TCP/IP Protocol:
• Transmission Control Protocol (TCP) and Internet Protocol (IP) are the foundational protocols for
socket programming. • TCP provides reliable, connection-oriented communication, ensuring data
integrity and order. • IP facilitates the routing of data between devices in a network.
4.Basic Socket Functions:
• Socket programming involves a set of functions provided by the operating system or
programming language to create, bind, listen, accept, connect, send, and receive data through
sockets. • Examples of functions include socket(), bind(), listen(), accept(), connect(), send(), and
recv().
Server-Side Operations:
• Servers create a socket using socket() and bind it to a specific IP address and port using bind(). •
They then listen for incoming connections with listen() and accept connections with accept(). • Once
a connection is establi • shed, servers can send and receive data using send() and recv().
Client –Server Operations
Clients create a socket using socket() and connect to a server using connect(). After establishing a
connection, clients can send and receive data using send() and recv().
Use Cases of Socket Programming:
Socket programming finds applications in various domains, including web development, file
transfer protocols, online gaming, and real-time communication. It is the foundation for protocols
like HTTP, FTP, and SMTP, which power the internet. Socket programming enables the development
of both server and client applications, facilitating the exchange of information between devices in a
networked environment.
Example Use Cases:
https://github.com/HIRU-VIRU/SocketStudy 1/3
16/09/2024, 12:57 HIRU-VIRU/SocketStudy

1. Web servers: Web servers use socket programming to handle incoming HTTP requests from
clients, serving web pages and content.
2. Chat Application: Instant messaging and chat applications use sockets to enable real-time
communication between users.
3. File Transfer Protocol: Protocols like FTP (File Transfer Protocol) utilize socket programming for
transferring files between a client and a server.
4. Networked Games: Online multiplayer games rely on socket programming to facilitate
communication between game clients and servers.
5. RPC mechanisms: which allow processes to execute code on a remote server, often use socket
programming for communication.
CLIENT:
import socket

# Create a socket object


client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to the server


client_socket.connect(('localhost', 8000))

# Print the client's socket name


print(f"Client connected from: {client_socket.getsockname()}")

# Receive a message from the server


server_message = client_socket.recv(1024).decode()
print(f"Received from server: {server_message}")

# Send a message to the server


client_socket.send("Acknowledgement received from the client.".encode())

# Close the connection


client_socket.close()

SERVER:
import socket

# Create a socket object


server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the host and port


server_socket.bind(('localhost', 8000))

# Listen for incoming connections (max 1 connection)


server_socket.listen(1)
print("Server is waiting for a connection...")

# Accept the connection


conn, addr = server_socket.accept()
print(f"Connected by {addr}")

https://github.com/HIRU-VIRU/SocketStudy 2/3
16/09/2024, 12:57 HIRU-VIRU/SocketStudy

# Send a message to the client


conn.send("Hello from the server!".encode())

# Receive a message from the client


data = conn.recv(1024)
print(f"Received from client: {data.decode()}")

# Close the connection


conn.close()
server_socket.close()

OUTPUT:
CLIENT:

SERVER:

Result:
Thus the study of Socket Programming Completed Successfully

https://github.com/HIRU-VIRU/SocketStudy 3/3

You might also like