Socket Interface
The Socket Interface is an API (Application Programming Interface) used for network communication
between devices. It provides a way for applications to send and receive data over a network using
protocols like TCP (Transmission Control Protocol) or UDP (User Datagram Protocol).
Sockets are endpoints in a communication link, allowing two devices to establish a connection and
exchange data.
Key Components of a Socket
1. IP Address:
o Identifies the network device uniquely.
o Example: 192.168.1.1.
2. Port Number:
o Identifies the specific application or service running on a device.
o Example: Port 80 is used for HTTP.
3. Protocol:
o Defines how data is transmitted. Common protocols:
TCP: Reliable, connection-oriented.
UDP: Fast, connectionless.
Socket Types
1. Stream Sockets (SOCK_STREAM):
o Use TCP for reliable, connection-oriented communication.
o Ensures data delivery in the correct order.
o Example: Web applications.
2. Datagram Sockets (SOCK_DGRAM):
o Use UDP for fast, connectionless communication.
o Data may arrive out of order or be lost.
o Example: Online gaming, video streaming.
3. Raw Sockets (SOCK_RAW):
o Allow direct access to lower-level protocols like IP.
o Used for network monitoring and diagnostics.
Socket Programming Workflow
1. Client-Side Workflow
Socket Creation: Create a socket using the appropriate protocol.
Connect: Establish a connection to the server.
Send/Receive Data: Exchange data with the server.
Close Socket: Release resources when done.
2. Server-Side Workflow
Socket Creation: Create a socket.
Bind: Associate the socket with a specific IP address and port.
Listen: Wait for client requests.
Accept: Establish a connection with a client.
Send/Receive Data: Communicate with the client.
Close Socket: Close the connection after communication.
Socket Functions
1. socket()
o Creates a socket.
o Syntax: socket(family, type, protocol)
family: Address family (e.g., AF_INET for IPv4).
type: Socket type (e.g., SOCK_STREAM for TCP).
protocol: Protocol to use (default is 0).
2. bind()
o Assigns an IP address and port to the socket.
o Syntax: bind(socket, address)
3. listen()
o Marks the socket as a listening socket (server-side).
o Syntax: listen(socket, backlog)
4. accept()
o Accepts a client connection (server-side).
o Syntax: accept(socket)
5. connect()
o Establishes a connection to a server (client-side).
o Syntax: connect(socket, address)
6. send() and recv()
o send(): Sends data over a socket.
Syntax: send(socket, data)
o recv(): Receives data from a socket.
Syntax: recv(socket, buffer_size)
7. close()
o Closes a socket.
o Syntax: close(socket)
Example: Socket Programming in Python
Server Code
# Create a socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to an address and port
server_socket.bind(('localhost', 12345))
# Listen for connections
server_socket.listen(5)
print("Server is listening...")
# Accept a client connection
client_socket, addr = server_socket.accept()
print(f"Connection established with {addr}")
# Receive data from client
data = client_socket.recv(1024).decode()
print(f"Client says: {data}")
# Send response
client_socket.send("Hello from server!".encode())
# Close connections
client_socket.close()
server_socket.close()
Client Code
python
Copy code
import socket
# Create a socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the server
client_socket.connect(('localhost', 12345))
# Send data to server
client_socket.send("Hello from client!".encode())
# Receive response
response = client_socket.recv(1024).decode()
print(f"Server says: {response}")
# Close the socket
client_socket.close()
Advantages of Using Socket Interface
1. Flexibility:
o Can be used for different types of communication (e.g., client-server, peer-to-peer).
2. Efficiency:
o Direct communication without intermediaries.
3. Cross-Platform:
o Works on multiple operating systems (Linux, Windows).
Use Cases
1. Web Servers and Clients:
o Browsers use sockets to communicate with web servers.
2. File Transfer Protocols:
o FTP uses sockets for data transfer.
3. Online Games and Streaming:
o Real-time communication relies on UDP sockets.
4. IoT Devices:
o Communicate with servers via socket connections.