User Tools

Site Tools


tanszek:oktatas:iss_t:python_example_for_blocking_and_non-blocking_socket

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tanszek:oktatas:iss_t:python_example_for_blocking_and_non-blocking_socket [2024/02/25 15:49] kneheztanszek:oktatas:iss_t:python_example_for_blocking_and_non-blocking_socket [2024/02/25 15:55] (current) knehez
Line 1: Line 1:
 +=== Blocking socket server ===
 +
 +To illustrate the difference with a blocking socket approach, we'll create a simple blocking TCP server and a corresponding client. This server will handle one connection at a time in a blocking manner, meaning it will wait (or block) on I/O operations like accepting new connections or receiving data.
 +
 +<sxh python>
 +import socket
 +
 +HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
 +PORT = 65432        # Port to listen on (non-privileged ports are > 1023)
 +
 +# Create a socket
 +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
 +    server_socket.bind((HOST, PORT))
 +    server_socket.listen()
 +    print(f"Server is listening on {HOST}:{PORT}")
 +
 +    while True:
 +        # Accept a new connection
 +        conn, addr = server_socket.accept()
 +        with conn:
 +            print(f"Connected by {addr}")
 +            while True:
 +                data = conn.recv(1024)
 +                if not data:
 +                    break  # No more data from client, close connection
 +                print(f"Received {data.decode()} from {addr}")
 +                response = "This is a response from the server.".encode()
 +                conn.sendall(response)
 +</sxh>
 +
 +=== Blocking client ===
 +<sxh python>
 +import socket
 +
 +HOST = '127.0.0.1'  # The server's hostname or IP address
 +PORT = 65432        # The port used by the server
 +
 +# Create a socket
 +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
 +    s.connect((HOST, PORT))
 +    print("Connected to the server")
 +
 +    # Send data
 +    message = 'Hello, server'.encode()
 +    s.sendall(message)
 +    print("Message sent to the server")
 +
 +    # Wait for a response
 +    data = s.recv(1024)
 +    print("Received response from the server")
 +
 +print(f"Received: {data.decode()}")
 +</sxh>
 +
 +----
 === Non-blocking server === === Non-blocking server ===
 +
 +Creating a non-blocking TCP socket server in Python involves setting up a socket to listen for connections without blocking the main execution thread of the program. Below is a simple example of a non-blocking TCP server that accepts multiple client connections and handles them asynchronously. This server uses the select method, which is a way to check for I/O readiness on sockets, making it possible to manage multiple connections without blocking on any single one.
  
 <sxh python> <sxh python>
Line 69: Line 126:
 </sxh> </sxh>
  
-=== Non blocking client ===+=== Non-blocking client === 
 + 
 +To test the non-blocking TCP server, you can create a simple client that connects to the server, sends a message, and then waits to receive a response. Below is an example of a basic TCP client in Python that interacts with our non-blocking server. 
 <sxh python> <sxh python>
 import socket import socket
tanszek/oktatas/iss_t/python_example_for_blocking_and_non-blocking_socket.1708876198.txt.gz · Last modified: 2024/02/25 15:49 by knehez