question on ftp and socket

  • Thread starter Thread starter sidd
  • Start date Start date
S

sidd

Hi,
I am trying to ftp a file using socket class in system.net .
problem is it does not write the file on the server.
there are no errors.

below is the code
=============================
string strIPAddress;
int strPortNumber;
strIPAddress="IP address here";
strPortNumber=111;
IPEndPoint lep = new IPEndPoint(IPAddress.Parse(strIPAddress), strPortNumber);
Socket s = new Socket(lep.Address.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
s.Connect(lep);
if (s.Connected)
{
string command = "USER Anonymous" ;
s.Send(Encoding.ASCII.GetBytes(command), mmand.Length, 0);
command = "PASS " ;
s.Send(Encoding.ASCII.GetBytes(command), command.Length, 0);
//reply is my sructure holds the code and message returned
commandReply reply = readCommandReply(s);
Debug.Write(reply.code.ToString() + " " + reply.message.ToString());
//NeedToRevisit : need to break it in small byte size before sending.

Byte[] SendFileBytes = Encoding.ASCII.GetBytes("File path to be uploaded");
SendFileHelper(SendFileBytes);
byte[] msg = aSendFileBytes;
// Blocks until send returns.
int i = s.Send(msg, 0, msg.Length, SocketFlags.None);
reply = readCommandReply(s);
Debug.Write(reply.code.ToString() + " " + reply.message.ToString());
// Blocks until read returns.
byte[] bytes = new byte[1024];
s.Receive(bytes, 0, s.Available, SocketFlags.None);
reply = readCommandReply(s);
Debug.Write(reply.code.ToString() + " " + reply.message.ToString());
//Displays to the screen.
//Console.WriteLine(Encoding.ASCII.GetString(bytes));
s.Shutdown(SocketShutdown.Both);
s.Close();



===========================
there is no error but it does not write the file.

also is this the right direction if you want to implement ftp in .net.
can i FTP a file using webrequest or one of it's decendent class.
thanks
sidd
 
Hi Sidd,

Thanks for posting.

I have briefly reviewed the code. You do not show the detailed
implementation of "SendFileHelper" and "readCommandReply". As a result, it
is quite difficult to say what is wrong.

Here is a sample on FTP client in .Net:

http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=2152e
0bb-4efd-4517-80e7-ad6f0e2a1603

I hope it helps.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi ,
Thanks for the reply.

just disregard that line "SendFileHelper(SendFileBytes);"
i have copied the code for SendFileHelper directly in the main
function
i forgot to comment that call to "SendFileHelper(SendFileBytes);"

now commandReply is just a helper function to read the response
back.i do not think you need to look at it.below is the code.



========================
private struct commandReply
{
public int code;
public string message;
}


static private commandReply readCommandReply(Socket s)
{
// Read the first line
commandReply reply;
Byte[] readBytes;
bool foundLastLine;
int sizeReceived;
string serverMessage = "";
//int count = 0;
reply.message="";
reply.code=0;
if (s.Poll(1000, SelectMode.SelectRead)==true )
{
do
{
readBytes = new Byte[COMMAND_BLOCK_SIZE];
sizeReceived = s.Receive(readBytes);
serverMessage += Encoding.ASCII.GetString(readBytes, 0,
sizeReceived);

// count++;
}

while (sizeReceived == readBytes.Length &
!serverMessage.EndsWith(CRLF)); // Go back for more if necessary
// Check id there are more lines to receive
reply.code = Convert.ToInt32(serverMessage.Substring(0,3));
if (serverMessage[3] == '-' & serverMessage.IndexOf(CRLF + reply.code
+ ' ') == -1)
{
// reply.code =
Convert.ToInt32(serverMessage.Substring(0,3));
do
{
readBytes = new Byte[COMMAND_BLOCK_SIZE];
sizeReceived = s.Receive(readBytes);
serverMessage += Encoding.ASCII.GetString(readBytes, 0,
sizeReceived);
foundLastLine = serverMessage.IndexOf(CRLF + reply.code + ' ') > 0;
}
while (!foundLastLine | !serverMessage.EndsWith(CRLF)); // Go back
for more if necessary
}

// Extract the code and message
reply.code = Convert.ToInt32(serverMessage.Substring(0,3));
reply.message = serverMessage.Substring(4,serverMessage.Length-6);
// Ignore the <CRLF>
//this.handleServerOutput(reply.code + " " + reply.message);
}
return reply;
}




===========================
thanks
sidd
 
Hi Sidd,

Thanks for your update. However, the code still does not compile. For
example:

s.Send(Encoding.ASCII.GetBytes(command), mmand.Length, 0);

byte[] msg = aSendFileBytes;

In addition, how do you define COMMAND_BLOCK_SIZE and CRLF?

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top