This article demonstrates how to develop a TCP server communication setup on the MH1905 Evaluation Board (EVB), running under the Ubuntu system. TCP/IP (Transmission Control Protocol/Internet Protocol) is fundamental for network communication, enabling devices to exchange data reliably over the internet. Let's explore how to utilize TCP for creating server applications in Python, particularly beneficial for IoT applications using the MH1905.
TCP is a core communication protocol within the internet protocol suite. It facilitates reliable, ordered, and error-checked delivery of a stream of data between applications running on hosts communicating via an IP network. It's particularly important in settings where data must be accurately delivered from sender to receiver.
Relevance to MH1905: For IoT devices like the MH1905, TCP is crucial for ensuring that data sent across a network reaches its destination intact and in order, which is essential for applications that rely on seamless and continuous data communication.
Steps to Develop a TCP Server in Python on MH1905The following steps will guide you through setting up a basic TCP server using Python, which will listen for and respond to client requests on the MH1905 EVB.
- Setting Up the Python Environment:
- Writing the TCP Server Code:
Ensure that Python and necessary libraries are installed on your MH1905 system. Python 3.8, included in your Ubuntu image, should suffice for most TCP server tasks.
Create a new Python script named tcp_server.py:
vi tcp_server.py
Enter the following Python code into your script:
import socket
# Define the host and port
HOST = '127.0.0.1'
PORT = 12345
# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the host and port
server_socket.bind((HOST, PORT))
# Set timeout for the server socket
server_socket.settimeout(60) # Timeout set to 60 seconds
# Listen for incoming connections
server_socket.listen(1)
print('Server listening on {}:{}'.format(HOST, PORT))
while True:
try:
# Accept incoming connection
client_socket, client_address = server_socket.accept()
print('Connection from', client_address)
# Receive data from client
data = client_socket.recv(1024)
if not data:
break
print('Received:', data.decode())
# Send a response back to the client
client_socket.sendall('Message received'.encode())
# Close the connection with the client
client_socket.close()
except socket.timeout:
print("No client connection for 60 seconds. Closing server socket.")
break
Explanation of the Code:- The socket library is used to facilitate network communications.
- The server is set up to listen on localhost and a specified port (65432 by default).
- socket.accept() blocks and waits for an incoming connection. Once a connection is established, the server receives data and sends back received data message.
- If socket has been opened for more than 20 seconds, it will automatically close the socket.
3. Running the Server: Execute the script in your terminal:
python3 tcp_server.py
This command starts the server, which will wait for connections from clients to receive and send back data.
ConclusionThe TCP protocol is integral to IoT applications, particularly for devices that require stable, reliable network communication. With this straightforward Python TCP server example on the MH1905, developers can build a foundation for more complex applications that communicate over TCP. From managing device interactions to handling real-time data streams, mastering TCP communications empowers developers to leverage the full potential of IoT solutions.
Comments