Search:
Namespaces
Discussions
.NET v1.1
Feedback
File access from within a function.
Messages
Related Types
This message was discovered on
ASPFriends.com 'ngfx-io' list
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Meyer, Richard D (Rick)
-- Moved from [aspngcs] to [ngfx-io] by devin <
Click here to reveal e-mail address
> --
I need to create a function that can read files from a remote server.
My access will be via FTP login.
Is there a way to do this from within a function, maybe an FTP class or
something?
Reply to this message...
Peter Brunone
Richard,
You'll probably end up using the System.Net.Sockets.
TcpClient
class on port
21 (or whichever port your FTP server uses), issuing standard FTP commands
as your server requires, and parsing the resulting output for display.
Here's some sample code (issued with no guarantees, but it works in a
WinForm):
Dim myFTP As
TcpClient
= New
TcpClient
()
myFTP.Connect("myserver.com", 21)
Dim myFTPStream As
NetworkStream
= myFTP.GetStream()
If myFTPStream.CanRead And myFTPStream.CanWrite Then
MsgBox("Read and Write")
' Does a simple write.
Dim sendBytes As [Byte]() =
Encoding
.ASCII.GetBytes("Is anybody there")
TelnetStream.Write(sendBytes, 0, sendBytes.Length)
' Reads the
NetworkStream
into a byte buffer.
Dim bytes(Telnet.ReceiveBufferSize) As Byte
TelnetStream.Read(bytes, 0, CInt(Telnet.ReceiveBufferSize))
' Returns the data received from the host to the console.
Dim returndata As String =
Encoding
.ASCII.GetString(bytes)
MsgBox(("This is what the host returned to you: " + returndata))
End If
myFTPClient.Close()
Cheers,
Peter
|-----Original Message-----
|From: Meyer, Richard D (Rick) [mailto:
Click here to reveal e-mail address
]
|
|I need to create a function that can read files from a remote server.
|My access will be via FTP login.
|Is there a way to do this from within a function, maybe an FTP class or
|something?
|
Reply to this message...
Mitch Denny (VIP)
Richard,
This URL might be helpful, it contains a fairly good
implementation of a FtpWebRequest class:
http://www.gotdotnet.com/userfiles/Feroze/ftpclient.zip
If you are looking for something rock solid head on
over to
http://www.mabry.com
, they are working on a
commercial implementation, I haven't used it but it
is probably worth a look.
----------------------------------------
- Mitch Denny
-
Click here to reveal e-mail address
- +61 (414) 610-141
-
-----Original Message-----
From: Meyer, Richard D (Rick) [mailto:
Click here to reveal e-mail address
]
Sent: Wednesday, 8 May 2002 05:46
To: ngfx-io
Subject: [ngfx-io] File access from within a function.
-- Moved from [aspngcs] to [ngfx-io] by devin
<
Click here to reveal e-mail address
> --
I need to create a function that can read files from a remote server. My
access will be via FTP login. Is there a way to do this from within a
function, maybe an FTP class or something?
| [ngfx-io] member
Click here to reveal e-mail address
= YOUR ID
|
http://www.aspfriends.com/aspfriends/ngfx-io.asp
= JOIN/QUIT
Reply to this message...
Meyer, Richard D (Rick)
I asked a question awhile ago about an FTP class. The links giver were
great.
So far so good I have implemented a very basic set of commands. But....
I'm having trouble with my 'put' command. The test environment hangs on
the very next read on the command port after I send the data to be
written to a file and the file on the server contains no data until I
kill the test program. I have used a network sniffer and I can see the
data being sent to the server. I guess that the server has no idea that
I have finished sending data. Here are the relevant functions:
/// <summary>
/// FTP STOR command implementation.
/// </summary>
/// <param name=3D"name">The name of the file to place on
the server.</param>
/// <param name=3D"data">A string containing the data to
store on the server.</param>
/// <returns>True if the command was successful, False
if not.</returns>
public bool Put(string name, string fileData)
{
bool retVal =3D false;
if(this.isCmdOpen && this.isUserLoggedIn)
{
if(OpenDataPort())
{
if(SendCmd("STOR " + name))
{
string cmdData =3D
ReadCmd();
//Debug line
=09
Console
.WriteLine(cmdData);
=09
switch(cmdData.Substring(0, 3))
{
case "125":
break;
case "150":
retVal =3D
SendData(fileData);
=09
CloseDataPort();
//
if(SendCmd("STATUS"))
// {
//
cmdData =3D ReadCmd();
//
//Debug line
//
Console
.WriteLine(cmdData);
// }
//
if(cmdData.StartsWith("226") || cmdData.StartsWith("250"))
// {
//
//Not sure yet
// }
// else
// {
//
this.lastError =3D cmdData;
//
retVal =3D false;
// }
break;
default:
=09
this.lastError =3D cmdData;
break;
}
}
CloseDataPort();
}
}
return retVal;
}
/// <summary>
/// Sends information to the data stream.
/// </summary>
/// <param name=3D"sendStr">The string to send.</param>
/// <returns>True if the operation was successful, False
if not.</returns>
private bool SendData(string sendStr)
{
bool retVal =3D false;
if(this.isDataOpen && this.cmdStream.CanWrite)
{
//Send the text
string sendData =3D sendStr + "/r/n";
byte[] sendBytes =3D
Encoding
.ASCII.GetBytes(sendData.ToCharArray());
this.dataStream.Write(sendBytes, 0,
sendBytes.Length);
this.dataStream.Flush();
retVal =3D true;
}
else
{
this.lastError =3D "Cannot write to this
stream.";
}
return retVal;
}
Any ideas would be greatly appreciated.
Rick Meyer
Click here to reveal e-mail address
Reply to this message...
System.Console
System.Net.Sockets.NetworkStream
System.Net.Sockets.TcpClient
System.Text.Encoding
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com