Ash
Ash

Reputation: 328

Python sockets recv() issue

I have a socket server running on an Arduino board and am trying to control it via a Python script. Using the basic example socket documentation, I have this:

import socket 
import sys

TCP_IP = '192.168.254.100'
TCP_PORT = 5012
BUFFER_SIZE = 1024
MESSAGE = "Status"
# Create a TCP/IP socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(MESSAGE)
# data = s.recv(BUFFER_SIZE)
# print (data)
s.close()
sys.exit()

My script works fine when I comment out the lines to receive the response of the server. However, if I attempt to read the response, my server and python script hangs.

On the server side, here is a snippet of what the code looks like:

void loop() {
  // listen for incoming clients
 client = server.available();
  if (client){
    Serial.println("Client connected");
    while (client.connected()){
        // Read the incoming TCP command
        String command = ReadTCPCommand(&client);
        // Debugging echo command to serial
        command.trim();
        Serial.println(command);
        // Debugging echo command back to client
        client.println(command);
        // Phrase the command
        PhraseTCPCommand(&client, &command);
    }
    // Stop the client
    client.stop();
    Serial.println("Client disconnected");
  }

 }

The library I am utilising for the server is the Arduino WiFi library. The function PhraseTCPCommand, takes the command and triggers external events with the GPIO pins of the board. This action is performed fine by the Python script when the recv() is commented out. The response string sent from the server is terminated with a newline and carriage return. Could that be causing issues?

Additionally, I am able to connect and receive responses from the server with no issues using either telnet, netcat or PuTTY, which leads me to believe it's something to do with the way my Python script attempts to read the response from the server.

Upvotes: 0

Views: 1180

Answers (2)

Armali
Armali

Reputation: 19375

The response string sent from the server is terminated with a newline and carriage return. Could that be causing issues?

No, what is causing the issue is possibly that the command MESSAGE is not terminated with a newline and the function ReadTCPCommand() expects one. Change to:

MESSAGE = "Status\n"

Upvotes: 1

donkopotamus
donkopotamus

Reputation: 23176

The issue here could be that your message has not been fully sent, and reflects a common misunderstanding of socket programming. Low level calls such as send do not promise to send the full message. Instead the caller must check the number of bytes actually sent (from the return value) and continue to iteratively call the function until the full message is sent.

In general, a bare send call without such iteration is a mistake.

Python does provides a higher level function socket.sendall however that will perform this job for you.

Upvotes: 0

Related Questions