Exemplo n.º 1
0
int main()
{
    TcpServer server;

    if (server.listen(2000) != AbstractSocket::Done)
    {
        perror("Error :");
        return -1;
    }

    TcpSocket* s = server.accept();

    if (s == NULL)
    {
        cout << "Error: cannot accept conection" << endl;
        return -1;
    }

    cout << "Connected client" << endl;
    int i = 4;
    i++;
    cout << "Remote host : " << s->getRemotePort() << endl;
    cout << s->getLocalPort() << endl;
    cout << server.getRemoteAddress().toString() << endl;
    cout << s->getRemoteAddress().toString() << endl;
    s->sendInt32(10);
    sleep(2);
    s->sendString("hello");
    s->sendCharArray("world", 5);
    Frame f;
    f << "This is a frame " << 666 << '\n';

    if (s->sendFrame(f) != AbstractSocket::Done)
        perror("error on send frame");

    delete s;
    server.close();
    s->close();
    return 1;
}
Exemplo n.º 2
0
int WiFiCmdRobot::WiFiCmdRobot_main() {
    String stringRead;
    int conx = 0;
    unsigned long timeout = 5; // 5s
    unsigned long start = 0;
    int ret=SUCCESS;

    // not specifically needed, we could go right to AVAILABLECLIENT
    // but this is a nice way to print to the serial monitor that we are 
    // actively listening.
    // Remember, this can have non-fatal falures, so check the status
    while (1) { 
        if(tcpServer.isListening(&status))
        {
            Serial.print("Listening on port: ");
            Serial.println(portServer, DEC);
            digitalWrite(Led_Yellow, HIGH);               // turn on led yellow
            break;
        }  
        else if(DNETcK::isStatusAnError(status))
        {
            Serial.println("Error Listening");
            tcpClient.close();
            tcpServer.close();
            return -1;
        }
    }

    // wait for a connection until timeout
    conx = 0;
    start = millis();
    while ((millis() - start < timeout*1000) && (conx == 0)) { 
        if((count = tcpServer.availableClients()) > 0)
        {
            Serial.print("Got ");
            Serial.print(count, DEC);
            Serial.println(" clients pending");
            conx = 1;
            // probably unneeded, but just to make sure we have
            // tcpClient in the  "just constructed" state
            tcpClient.close();             
        }
    }

    // accept the client 
    if((conx == 1) && (tcpServer.acceptClient(&tcpClient)))
    {
       Serial.println("Got a Connection");
       lcd.clear();
       lcd.print("Got a Connection");
       
       stringRead = "";
       while (tcpClient.isConnected())
       {
                if (tcpClient.available())
                {
                    char c = tcpClient.readByte();
                    if (c == '\n')
                    {
                          Serial.println (stringRead);
                          if (stringRead.startsWith("GET"))  
                          {
                                Serial.println("GET");
                                ret = Cmd (stringRead);
                                
                                if (ret == SUCCESS)
                                {
                                    ret = ReplyOK ();
                                }
                                else    
                                {
                                    cmd_GO[0] = 0; //reset GO command
                                    ret = ReplyKO ();
                                }
                                break;                             
 
                          }
                          else if (stringRead.startsWith("\r"))
                          {
                                // empty line => end
                                Serial.println("empty line => end");
                                break;
                          }                          
                          else
                          {
                                // no GET
                                Serial.println("no GET => ignore");
                          }
                          stringRead = "";              
                    }
                    else
                    {
                          stringRead += c;
                    }
                }
       } // end while            
    }
    else if((cmd_GO[0] == CMD_GO) && (cmd_GO[1] > t_GO+(uint16_t)timeout))  // GO ongoing
    {
       Serial.println("Continue command GO");
       cmd_GO[1] = cmd_GO[1] - t_GO - (uint16_t)timeout;
       
       cmd[0] = cmd_GO[0];
       cmd[1] = t_GO;
       cmd[2] = cmd_GO[2];   
       ret = CmdRobot (cmd, resp, &resp_len);
       
       Serial.print("Call CmdRobot, ret: ");
       Serial.print(ret);
       Serial.print(" / resp_len: ");
       Serial.println(resp_len);        
    }
    else if(cmd_GO[0] == CMD_GO) // end GO
    {
       Serial.println("End command GO");
       cmd_GO[0] = 0; //reset GO command
      
       cmd[0] = CMD_STOP;  // Stop after GO
       cmd[1] = 0;
       cmd[2] = 0;   
       ret = CmdRobot (cmd, resp, &resp_len);
       
       Serial.print("Call CmdRobot, ret: ");
       Serial.print(ret);
       Serial.print(" / resp_len: ");
       Serial.println(resp_len);        
    }   
      
    // Close
    tcpClient.close();
    digitalWrite(Led_Yellow, LOW);               // turn off led yellow
    Serial.println("Closing TcpClient");
    
    return SUCCESS;    
}