TCPCLient write()

HI,

i need i little help to use the write’s TCPClient call, the program connect to the server but when i try to send some command (FTP) i dont get any answer back or, probably the message never get there.

This is what i have done so far, i tried to send both char and bytes:

TCPClient client,client1,client2;//First, we construct the client that will connect to our local server.
String messageToSend;
char string1[1] = "";
byte * msg;
int length;
void ipArrayFromString(byte ipArray[], String ipString) {
  int dot1 = ipString.indexOf('.');
  ipArray[0] = ipString.substring(0, dot1).toInt();
  int dot2 = ipString.indexOf('.', dot1 + 1);
  ipArray[1] = ipString.substring(dot1 + 1, dot2).toInt();
  dot1 = ipString.indexOf('.', dot2 + 1);
  ipArray[2] = ipString.substring(dot2 + 1, dot1).toInt();
  ipArray[3] = ipString.substring(dot1 + 1).toInt();
}

//callback to the connect function
void connectToMyServer(String ip) {
  byte serverAddress[4];
  ipArrayFromString(serverAddress, ip);
  if (client.connect(serverAddress, 21)){// && client1.connect(serverAddress, 9000) && client2.connect(serverAddress, 9000)) {
    Serial.println("connecterd ");
  } else {
    Serial.println("failed ");
  }
}

void setup() {
      Serial.begin(9600);
    while(!Serial.available()) Spark.process();
  
 connectToMyServer("148.251.48.69");
}

void loop() {
  
  //this allow me to control what is doing
  while(Serial.available())
  {
      sprintf(string1,"%c",Serial.read());
      messageToSend+=string1;
      
      
  } 
  
  if (messageToSend != ""){
      Serial.print("preparing to send to the server: ");
      Serial.println(messageToSend);
      length=messageToSend.length();
      /*msg= (byte *)malloc(sizeof(byte) * length);
      messageToSend.getBytes(msg,length);*/
      client.write((uint8_t *)messageToSend.c_str(),length);
      //client.write(msg,length);
      //free(msg);
      messageToSend="";
  }
  //Client     
  if (client.connected()) {
    if (client.available()) {
      char charac1 = client.read();
      Serial.print(charac1);
    }
  }
}

http://cr.yp.to/ftp/request.html

It says

Request format

A request is a string of bytes. It contains
a verb consisting of alphabetic ASCII characters;
optionally, a space followed by a parameter; and
\015\012.
Some clients fail to include the \015, so I recommend that servers look only for \012. The parameter cannot contain \012.

@bko, @peekay123, @mdma,

maybe one of you can give some inputs on this when time permits. :wink:

I would try to debug this on a host PC program that is a bit simpler than FTP. On Linux/Mac you can use nc or netcat with something like nc -l 3333 to listen on port 3333. Then you will have full control over both sides of the conversation for debugging.

1 Like

In the end i solved in this way, as my last message was hinting i send the line breaker so that the FTP elaborate the command i sent:

TCPClient client,client1,client2;//First, we construct the client that will connect to our local server.
String messageToSend;
char string1[1] = "";
byte * msg;
int length;
char LineBreaker[2];
void ipArrayFromString(byte ipArray[], String ipString) {
  int dot1 = ipString.indexOf('.');
  ipArray[0] = ipString.substring(0, dot1).toInt();
  int dot2 = ipString.indexOf('.', dot1 + 1);
  ipArray[1] = ipString.substring(dot1 + 1, dot2).toInt();
  dot1 = ipString.indexOf('.', dot2 + 1);
  ipArray[2] = ipString.substring(dot2 + 1, dot1).toInt();
  ipArray[3] = ipString.substring(dot1 + 1).toInt();
}

//callback to the connect function
void connectToMyServer(String ip) {
  byte serverAddress[4];
  ipArrayFromString(serverAddress, ip);
  if (client.connect(serverAddress, 21)){// && client1.connect(serverAddress, 9000) && client2.connect(serverAddress, 9000)) {
    Serial.println("connecterd ");
  } else {
    Serial.println("failed ");
  }
}

void setup() {
      Serial.begin(9600);
      LineBreaker[0]='\015';
      LineBreaker[1]='\012';
      LineBreaker[2]='\0';
    while(!Serial.available()) Spark.process();
  
 connectToMyServer("148.251.48.69");
}

void loop() {
  
  //this allow me to control what is doing
  while(Serial.available())
  {
      sprintf(string1,"%c",Serial.read());
      messageToSend+=string1;
      
      
  } 
  
  if (messageToSend != ""){
      Serial.print("preparing to send to the server: ");
      Serial.println(messageToSend);
      length=messageToSend.length();
      /*msg= (byte *)malloc(sizeof(byte) * length);
      messageToSend.getBytes(msg,length);*/
      client.write((uint8_t *)messageToSend.c_str(),length);
      client.write((uint8_t *)LineBreaker,2);
      //client.write(msg,length);
      //free(msg);
      messageToSend="";
  }
  //Client     
  if (client.connected()) {
    if (client.available()) {
      char charac1 = client.read();
      Serial.print(charac1);
    }
  }
}

1 Like