problem with YunServe connection

i use this code:

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

#define LED_PIN 9

YunServer server;

void setup() {
  
  // init LED PIN
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  
  // start the Bridge library
  Bridge.begin();
  
  // init serial connection and wait until it's available
  Serial.begin(9600);
  while (!Serial);
  
  // listen for incoming connections from localhost only
  
  server.begin();
  server.listenOnLocalhost();
  Serial.println("Accepting commands...");
}

void loop() {
  
  // try to accept a new connection
  YunClient client = server.accept();
  
  // a client was accepted?
  if(client) {
  
    // read the command and trim spaces
    String command = client.readString();
    command.trim();
    
    // print the received command on the console
    Serial.print("New command -> ");
    Serial.println(command);
     if(command == "1") {
          
          client.println("spara 1");
        } 
    // perform the requested action
        if(command == "on") {
          digitalWrite(LED_PIN, HIGH);
          client.println("LED acceso");
        } 
        if(command == "off") {
          digitalWrite(LED_PIN, LOW);
          client.println("LED spento");
        } 
      

        
    // close the connection
    client.stop();
  }
}

This works only if the Arduino is connected to PC:

  1. I connect Arduino to PC-A for power supply. (work)
  2. wi-fi connect PC-B to YUN (work)
  3. I can turn on/off led by PC-B

If I connect Arduino to external power supply:

  1. I connect Arduino to external power supply. (work)
  2. wi-fi connect PC-B to YUN (work)
  3. I can turn on/off led by PC-B or PC-A (problem)

What should I do to get this project ?

my second is steep connect two Arduino yun together via wifi (one client and one server ) if anyone has a working example I thank him...

Change "Serial" to "Console".

Tappo:
This works only if the Arduino is connected to PC:

Your problem is probably this line of code:

 while (!Serial);

This will cause the code to stay in this right little do-nothing loop until a USB serial connection is made. If you hook it up to an external power supply, the Yun will boot up, and the Linux side will run, but the sketch will never get past this one of code. Therefore, it will never get to the part where it is actually listening for commands.

my second is steep connect two Arduino yun together via wifi (one client and one server ) if anyone has a working example I thank him...

Take a look at this post: Send data from Arduino yun to particular IP address - #4 by ShapeShifter - Arduino Yún - Arduino Forum

It is an example of a Yun sketch making outgoing TCP connections to a server, and another sketch that is a server accepting such a connection. It may be quite close to what you want. If you have any questions, ask them in this thread.