top of page

TCP Listener

  • Writer: Mike Corona-Gonzalez
    Mike Corona-Gonzalez
  • Feb 15, 2024
  • 2 min read

In this post, I will talk about a simple Python script was able to make. The provided Python script, which establishes a basic TCP server to listen for incoming connections, receive data, and echo it back to the sender, can serve as a foundational component in a variety of applications. While the script itself is primarily educational, it illustrates key concepts that are applicable in more complex scenarios.


Here is the repository for the code on my GitHub account for those of you who want to follow along: https://github.com/mikegonzo10/tcplistener


This script is a basic example of a TCP server written in Python using the socket library. It's designed to listen for incoming connections on a specific IP address and port, echo back any received data, and then close the connection when there's no more data to read. Let's break it down step by step:


  1. Import the socket library: The socket library provides the necessary functions and structures for creating sockets in Python, allowing for network communications. import socket

  2. Define server settings: The server's IP address (tcp_ip) is set to '127.0.0.1', which is the loopback address, meaning it's intended to communicate within the same machine. The TCP port number (tcp_port) is set to 55, and the buffer_size is set to 100 bytes, determining how much data can be read at a time. tcp_ip = '127.0.0.1' tcp_port = 55 buffer_size = 100

  3. Create a socket object: The socket.socket() function is used to create a new socket, specifying socket.AF_INET for IPv4 addressing and socket.SOCK_STREAM for TCP (a reliable, connection-oriented protocol). s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  4. Bind the socket to address and port: The bind() method associates the socket with its IP address and port number. s.bind((tcp_ip, tcp_port))

  5. Listen for incoming connections: The listen() method enables the server to accept connections, with the argument 1 indicating the server can queue up at least one connection request before refusing new ones. s.listen(1)

  6. Accept a connection: The accept() method waits for an incoming connection. It returns a new socket object (conn) representing the connection, and a tuple holding the address of the client (addr). The script prints the client's address to the console. conn, addr = s.accept() print('Connection address: ', addr)

  7. Receive and echo data loop: while 1: data = conn.recv(buffer_size) if not data: break print('received data: ', data) conn.send(data)

  • The server enters an infinite loop, constantly waiting for data from the client.

  • It attempts to receive data using conn.recv(buffer_size), with the buffer size indicating the maximum amount of data to be received at once.

  • If recv() returns an empty bytes object (not data), it means the client has closed the connection, and the server breaks out of the loop.

  • If data is received, it prints the received data and then sends it back to the client using conn.send(data). This echoing back demonstrates a simple form of data interaction between the server and the client.

  1. Close the connection: Once the loop is exited (indicating no more data is being sent by the client), the server closes the connection socket with conn.close(). This effectively ends the communication session with the client. conn.close()

This script exemplifies a fundamental TCP server that echoes received data. It's important to note that, in a real-world application, additional error handling and security measures would be necessary to handle multiple clients and potential security threats effectively.


 
 
 

Comments


Subscribe to My Blog

Thanks for submitting!

  • GitHub
  • LinkedIn

Proudly created with Wix.com

bottom of page