Introduction to Network Programming
An introduction to Network programming, covering basic concepts and practical usage
Definition
Network Programming, also known as socket programing, is a technique that enable communication between two or more devices over a network, whether it be a local network (LAN) or the internet. The core concept of nerwork programming is the socket, which serve as an endpoint for communication between devices.
Understanding Sockets
Socket is an interface that allows programs to send and recieve data over a network. In socker-based communication, there are two main roles:
- Server --> listen for and accepts connections from clients.
- Client --> initiate connections to servers.
Each socket is identifeid by a combination of IP address and port number. The IP address is the address of the device on the network, while the port number is a unique identifier for the application running on the device.
Types of Sockets
Sockets are classified based on the communication protocol they use. The most common types of sockets are:
a. TCP Sockets (Transmission Control Protocol)
- Connection-oriented, Establises a connection before sending data.
- Ensure data order and reliability.
- Used in applications like web servers, FTP, SSH.
b. UDP Sockets (User Datagram Protocol)
- Connectionless, No need to establish connection before sending data.
- Faster but does not gurantee data order or reliability.
- Used in applications like video streaming, online gaming, DNS.
How Socket Programming Works
a. TCP Socket Programming
In TCP, the server and client go through several steps:
Server-side TCP:
- Create a socket ->
socket()
- Bind the socket to an IP and port ->
bind()
- Listen for incoming connections ->
listen()
- Accept client connections ->
accept()
- Send and recieve data ->
send()
,recv()
- Close the connection ->
close()
Client-side TCP:
- Create a socket ->
socket()
- Connect to the server ->
connect()
- Send and recieve data ->
send()
,recv()
- Close the connection ->
close()
b. UDP Socket Programming
Since UDP is connectionless, data is send directly without establishing a connection. The steps are:
- Create a socket ->
socket()
- Send data ->
sendto()
- Recieve data ->
recvfrom()
- Close the socket ->
close()
Example: TCP Server-Client in Python
Here is a simple example of a TCP server and client in Python:
TCP Server:
import socket
server_address = ('107.0.0.1', 8080)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(server_address)
server_socket.listen(5)
print("Waiting for connection...")
client_socket, client_address = server_socket.accept()
print(f"Connected to {client_address}")
data = client_socket.recv(1024)
print(f"Received: {data.decode()}")
client_socket.send(b"Hello from server!")
client_socket.close()
server_socket.close()
TCP Client:
import socket
server_address = ('localhost', 8080)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(server_address)
client_socket.send(b'Hello, server!')
response = client_socket.recv(1024)
print("Server response:", response.decode())
client_socket.close()
Applications of Socket Programming
Socket programming is used in a wide range of applications, including:
- Web servers and browsers (HTTP using TCP),
- Remote login services (SSH using TCP, Telnet using TCP),
- File transfer protocols (FTP),
- Online multiplayer games (UDP for low latency),
- Media streaming services (video/audio),
- Internet of Things (IoT) for device communication.
Conclusion
Socket programing enable devices to communicate over a network using protocols like TCP and UDP. By understanding sockets, you can build various network-based application, such as web servers, chat applications, and more.