Arduino Delphi socket communication

Hi!

So my problem is, i wrote a Websocket server on my Arduino and my Delphi client can't connect to that, because the connection is refused (error 10061). I wrote the first test program in Python, everything worked fine, so the problem is not on the Arduino's side. In Delphi I use TClientSocket and this is my code below:

Client.ClientType := ctBlocking;
Client.Address := 'ws://192.168.0.24';
Client.Port := 80;
Client.Host := 'localhost';
Client.Active := true;

The Arduino's IP address is: 192.168.0.24. The server listen's on the port 80 but i already tried to change that to 6667 and the client's port to 6667 but still nothing.
I am using Delphi 11 and RAD studio. What am I doing wrong?

You should ask that question on a Delphi forum because it's not Arduino related.

You either specify an address (IP) or a host. TClientSocket cannot understand URI's like "ws://192.168.0.24". I suggest you either set Client.Address:= '192.168.0.24'; or Client.Host:= 'localhost';. Setting the host will allow IP address to change without breaking the program.

I tried this, doesen't work.

The snippet makes it impossible to help any further.

This is on the Arduino's side, when the socket gets a message or we are connecting to the socket server:

wss.onConnection([](WebSocket &ws) {
    _SERIAL.println("onConnection");  //Debug
    String myString = String(Ethernet.localIP()[0]) + String(".") + String(Ethernet.localIP()[1]) + String(".") + String(Ethernet.localIP()[2]) + String(".") + String(Ethernet.localIP()[3]);
    int length = myString.length() + 1;
    char myIPtoSend[length];
    myString.toCharArray(myIPtoSend, length);
    wss.broadcast(WebSocket::DataType::TEXT, myIPtoSend, strlen(myIPtoSend));
    wss.listen();
    delay(1500);

    const auto protocol = ws.getProtocol();
    if (protocol) {
      _SERIAL.print(F("Client protocol: "));
      _SERIAL.println(protocol);
    }
    ws.onMessage([](WebSocket &ws, const WebSocket::DataType dataType, const char *message, uint16_t length) {
      switch (dataType) {
        case WebSocket::DataType::TEXT:

          if (firstmsg == 0 && message == "ENERGOLINE") {
            firstmsg++;
          } else {
            Serial.print("Message");
            dataArrive(message, ws);
            ws.send(WebSocket::DataType::TEXT, message, strlen(message));
            ResetVariables();  
            break;
          }
        case WebSocket::DataType::BINARY:
          _SERIAL.println(F("Received binary data"));
          dataArrive(message, ws);
          ws.send(WebSocket::DataType::TEXT, message, strlen(message));
          ResetVariables();
          break;
      }
    });
    ws.onClose([](WebSocket &, const WebSocket::CloseCode, const char *,
                  uint16_t) {
      _SERIAL.println(F("Disconnected"));
    });
    _SERIAL.print(F("New client: "));
    _SERIAL.println(ws.getRemoteIP());
  });
  wss.begin([](const IPAddress &ip, const char *header, const char *value) {

  // verify "Origin" header:
  if (strcmp_P(header, (PGM_P)F("Origin")) == 0)
    if (strcmp_P(value, (PGM_P)F("file://")) == 0) return false;

  return true;
});

The Arduino server listens on the 80 port. The code above works perfectly with my python test program.

Use wireshark to see what is being exchanged in the working Python script, and then follow the same analysis with the Delphi app.

I cannot see your python code, so I do not know what might differ. The error you get i Delphi is related to an attempt to connect to a port which is not listening.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.