Socket Programming
1. What is Socket Programming?
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, while the other socket reaches out to establish a connection. Once connected, they can exchange messages.
- Client: The node initiating the connection.
- Server: The node waiting for the connection.
- Socket: An endpoint for sending or receiving data across a network.
2. Server Code
The server listens for a connection on a specific port and sends a message to the client upon connection.
# Server Code
import socket
# Setup server
server_socket = socket.socket()
server_socket.bind(('localhost', 12345)) # Bind to localhost and port 12345
server_socket.listen()
# Respond to client
client_socket, client_conn = server_socket.accept()
print(client_socket)
print("Connected to " + str(client_conn))
client_socket.sendall(b'Hi from server')
# Teardown
client_socket.close()
server_socket.close()
Explanation:
socket.socket(): Creates a socket object.bind(('localhost', 12345)): Binds the socket to the address (localhost) and port (12345).listen(): Puts the server in listening mode, waiting for incoming connections.accept(): Accepts a connection from a client and returns the client socket and address.sendall(): Sends data to the connected client.close(): Closes the socket to free resources.
3. Client Code
The client connects to the server and receives a message.
# Client Code
import socket
# Setup client
client_socket = socket.socket()
server_addr = input("Enter server address: ") # e.g., localhost
server_port = int(input("Enter server port: ")) # e.g., 12345
# Connect to server
client_socket.connect((server_addr, server_port))
print(client_socket.recv(1024))
# Teardown
client_socket.close()
Explanation:
socket.socket(): Creates a socket object for the client.connect((server_addr, server_port)): Connects the client to the specified server address and port.recv(1024): Receives up to 1024 bytes of data from the server.close(): Closes the client socket to free resources.
4. Steps to Run the Code
- Open two Python IDLE instances (or terminals).
- Run the server code in one instance. The server will start listening on port
12345. - Run the client code in the other instance. Provide the server address (
localhost) and port (12345). - Observe the interaction: The client receives the message "Hi from server".
5. Concepts of Socket Programming
- IP Address: Identifies a device on a network.
- Port: Identifies a specific process or service on a device.
- TCP: A reliable protocol for ensuring data is received correctly.
- UDP: A faster but less reliable protocol compared to TCP.
6. Key Functions in Python Socket Programming
| Function | Description |
|---|---|
socket() |
Creates a new socket. |
bind() |
Associates the socket with an address and port. |
listen() |
Puts the socket into listening mode for incoming connections. |
accept() |
Accepts a connection from a client. |
connect() |
Connects the client to a server. |
sendall() |
Sends data to the connected socket. |
recv() |
Receives data from the connected socket. |
close() |
Closes the socket connection. |