0

I'm triyng to connect to my filezilla ftp server from a windows phone 8.1.

I have this code for now :

StreamSocket s = new StreamSocket();
await s.ConnectAsync(new HostName("192.168.254.53"), "21");
DataWriter writer = new DataWriter(s.OutputStream);
byte[] data = GetBytes(string.Format("{0}\r\n", "USER test"));
writer.WriteBytes(data);
await writer.StoreAsync();
await writer.FlushAsync();
...
static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

ConnectAsync works properly :

(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> Connected on port 21, sending welcome message...
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 220-FileZilla Server version 0.9.49 beta
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 220-written by Tim Kosse (tim.kosse@filezilla-project.org)
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 220 Please visit https://filezilla-project.org/

But for the user command, this is what the server receives :

(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> U
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 500 Syntax error, command unrecognized.
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> S
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 500 Syntax error, command unrecognized.
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> E
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 500 Syntax error, command unrecognized.
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> R
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 500 Syntax error, command unrecognized.
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)>  

Am I writing correctly onto my OutputStream ?

tufekoi
  • 929
  • 2
  • 14
  • 33
  • 1
    Not sure, but aren't the FTP commands 8-bit ASCII? The .net char is 16-bit. – Stefan Mar 03 '15 at 16:25
  • can you use the `Encoding` class? – Daniel A. White Mar 03 '15 at 16:31
  • I tried with `GetBytes(string.Format("{0}\r", "USER test")`, `Encoding.UTF8.GetBytes("USER ippon")`, `Encoding.BigEndianUnicode.GetBytes("USER ippon")` and `Encoding.Unicode.GetBytes("USER ippon")` but they keep giving me the same result... – tufekoi Mar 03 '15 at 17:00
  • I change my code to this `byte[] data = Encoding.Unicode.GetBytes("USER ippon\r");` and I notice the function add a byte to 0 between each letter of the command. That's why the server interpreted each letter as a command... So I just remove all the 0 byte and it works ! – tufekoi Mar 04 '15 at 09:48

1 Answers1

0

I'm using this now :

byte[] data = Encoding.Unicode.GetBytes("USER ippon\r");

And during debug, I noticed this :

enter image description here

The Encoding.Unicode.GetBytes function add a 0 byte after every other bytes. That's why it doesn't work ! My code now :

StreamSocket s = new StreamSocket();
await s.ConnectAsync(new HostName("192.168.254.53"), "21");
byte[] data = Encoding.Unicode.GetBytes("USER ippon\r");
data = data.Where(val => val != 0).ToArray();
await s.OutputStream.WriteAsync(data.AsBuffer());
await s.OutputStream.FlushAsync();
tufekoi
  • 929
  • 2
  • 14
  • 33