23. august 2004 - 22:05Der er
37 kommentarer og 1 løsning
send en fil over netværk
Nogle der har en god ide til hvordan jeg for sendt en fil fra den ene computer til den anden?
JEg skal have sendt nogle filer fra mine clienter til min server. Min server fungere bla. som ftp, så hvis det kunne foregå via ftp ville det være max ok.
Problemet er så noget code til at kunne oprette en forbindelse til en ftp server, med mulighed for at sende og modtage filer og liste dem der er på serveren?
Public Class Form1 Inherits System.Windows.Forms.Form
Private mTCPClient As New TcpClient Private mNetStream As NetworkStream Private mBytes() As Byte Private intBytesRec As Int64 Private mDataStream As NetworkStream Private mTCPData As New TcpClient
Private mServerAddr As IPAddress Private mFTPPort As Int32 = 21 Private mConnected As Boolean = False Private mFTPResponse As String
Public Event ServerReplied(ByVal ServerReply As String) Public Event ServerCalled(ByVal CallMsg As String) Public Event ErrorOccured(ByVal ErrorCode As Integer, ByVal ErrMessage As String) Public Event Transferring(ByVal intTransferred As Integer, ByVal intTotalFile As Integer)
'This call is required by the Windows Form Designer. InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub
'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(768, 533) Me.Name = "Form1" Me.Text = "Form1"
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strTemp As String Dim code As String
Dim priSM As New MemoryStream Dim intport As Int32
Private Function GetResponse() As String Dim strTemp As String Do ReDim mBytes(mTCPClient.ReceiveBufferSize) intBytesRec = mNetStream.Read(mBytes, 0, CInt(mTCPClient.ReceiveBufferSize)) strTemp = strTemp & Encoding.ASCII.GetString(mBytes, 0, intBytesRec) Loop While mNetStream.DataAvailable mFTPResponse = mFTPResponse & strTemp GetResponse = strTemp End Function
End Class ---
Problemet er så at --- Private Function GetResponse() As String Dim strTemp As String Do ReDim mBytes(mTCPClient.ReceiveBufferSize) intBytesRec = mNetStream.Read(mBytes, 0, CInt(mTCPClient.ReceiveBufferSize)) strTemp = strTemp & Encoding.ASCII.GetString(mBytes, 0, intBytesRec) Loop While mNetStream.DataAvailable mFTPResponse = mFTPResponse & strTemp GetResponse = strTemp End Function ---
Din kode virker ikke fordi du ikke bruger data connecntion men kun kontrol connection.
Her er et simpelt eksempel som kun understøtter LIST og NLST:
Imports System Imports System.Text Imports System.Net.Sockets Imports System.Threading
Public Class FtpClient Private ctrl As TcpClient Private ctrlstm As NetworkStream Private data As TcpClient Private datastm As NetworkStream Private Shared Sub Send(ByVal stm As NetworkStream, ByVal line As String) Dim b As Byte() = Encoding.Default.GetBytes(line) stm.Write(b, 0, b.Length) End Sub Private Shared Function Receive(ByVal stm As NetworkStream) As String Dim b(100000 - 1) As Byte Dim ix As Integer = 0 Dim n As Integer While stm.DataAvailable n = stm.Read(b, ix, b.Length - ix) ix += n End While Return Encoding.Default.GetString(b, 0, ix) End Function Public Shared Function Command(ByVal stm As NetworkStream, ByVal cmd As String) As String Send(stm, cmd + Convert.ToChar(13) + Convert.ToChar(10)) Thread.Sleep(50) Return Receive(stm) End Function Private Sub SetupData() Dim dataaddr As String = Command(ctrlstm, "PASV") Dim addrparts As String() = dataaddr.Split("()".ToCharArray)(1).Split(",".ToCharArray) Dim datahost As String = addrparts(0) + "." + addrparts(1) + "." + addrparts(2) + "." + addrparts(3) Dim dataport As Integer = Integer.Parse(addrparts(4)) * 256 + Integer.Parse(addrparts(5)) data = New TcpClient (datahost, dataport) datastm = data.GetStream End Sub Public Sub New(ByVal host As String, ByVal username As String, ByVal password As String) ctrl = New TcpClient (host, 21) ctrlstm = ctrl.GetStream Command(ctrlstm, "USER " + username) Command(ctrlstm, "PASS " + password) End Sub Public Function Dir() As String SetupData Command(ctrlstm, "LIST") Dim res As String = Receive(datastm) Receive(ctrlstm) datastm.Close data.Close Return res End Function Public Function ShortDir() As String SetupData Command(ctrlstm, "NLST") Dim res As String = Receive(datastm) Receive(ctrlstm) datastm.Close data.Close Return res End Function Public Sub Logout() Command(ctrlstm, "QUIT") ctrlstm.Close ctrl.Close End Sub End Class
Class TestClass Public Shared Sub Main(ByVal args As String()) Dim cli As FtpClient = New FtpClient ("localhost", "anonymous", "arne@") Console.WriteLine(cli.ShortDir) Console.WriteLine(cli.Dir) cli.Logout End Sub End Class
Private Sub SetupData() Dim dataaddr As String = Command(ctrlstm, "PASV") Dim addrparts As String() = dataaddr.Split("()".ToCharArray)(1).Split(",".ToCharArray) Dim datahost As String = addrparts(0) + "." + addrparts(1) + "." + addrparts(2) + "." + addrparts(3) Dim dataport As Integer = Integer.Parse(addrparts(4)) * 256 + Integer.Parse(addrparts(5)) data = New TcpClient(datahost, dataport) datastm = data.GetStream End Sub
fejl: An unhandled exception of type 'System.IndexOutOfRangeException' occurred in WindowsApplication16.exe
Additional information: Index was outside the bounds of the array.
Imports System Imports System.IO Imports System.Text Imports System.Net.Sockets Imports System.Threading
Public Class FtpClient Private ctrl As TcpClient Private ctrlstm As NetworkStream Private data As TcpClient Private datastm As NetworkStream
Private Shared Sub Send(ByVal stm As NetworkStream, ByVal line As String) Dim b As Byte() = Encoding.Default.GetBytes(line) stm.Write(b, 0, b.Length) End Sub
Private Shared Function Receive(ByVal stm As NetworkStream) As String Dim b(100000 - 1) As Byte Dim ix As Integer = 0 Dim n As Integer While stm.DataAvailable n = stm.Read(b, ix, b.Length - ix) ix += n End While Return Encoding.Default.GetString(b, 0, ix) End Function
Public Shared Function Command(ByVal stm As NetworkStream, ByVal cmd As String) As String Send(stm, cmd + Convert.ToChar(13) + Convert.ToChar(10)) Thread.Sleep(50) Return Receive(stm) End Function
Private Sub SetupData() Dim dataaddr As String = Command(ctrlstm, "PASV") Dim addrparts As String() = dataaddr.Split("()".ToCharArray)(1).Split(",".ToCharArray) Dim datahost As String = addrparts(0) + "." + addrparts(1) + "." + addrparts(2) + "." + addrparts(3) Dim dataport As Integer = Integer.Parse(addrparts(4)) * 256 + Integer.Parse(addrparts(5)) data = New TcpClient (datahost, dataport) datastm = data.GetStream End Sub
Public Sub New(ByVal host As String, ByVal username As String, ByVal password As String) ctrl = New TcpClient (host, 21) ctrlstm = ctrl.GetStream Command(ctrlstm, "USER " + username) Command(ctrlstm, "PASS " + password) End Sub
Public Function Dir() As String SetupData Command(ctrlstm, "LIST") Dim res As String = Receive(datastm) Receive(ctrlstm) datastm.Close data.Close Return res End Function
Public Function ShortDir() As String SetupData Command(ctrlstm, "NLST") Dim res As String = Receive(datastm) Receive(ctrlstm) datastm.Close data.Close Return res End Function
Public Sub MkDir(ByVal dir As String) Command(ctrlstm, "MKD " + dir) End Sub
Public Sub RmDir(ByVal dir As String) Command(ctrlstm, "RMD " + dir) End Sub
Public Sub ChDir(ByVal dir As String) Command(ctrlstm, "CWD " + dir) End Sub
Public Sub UpLoad(ByVal filename As String, ByVal binary As Boolean) If binary Then Command(ctrlstm, "TYPE I") Else Command(ctrlstm, "TYPE A") End If SetupData Command(ctrlstm, "STOR " + filename) Receive(ctrlstm) Dim f As FileStream = New FileStream (filename, FileMode.Open) Dim b(100000 - 1) As Byte Dim ix As Integer = 0 Dim n As Integer While ix < f.Length n = f.Read(b, 0, b.Length) datastm.Write(b, 0, n) Thread.Sleep(100) ix += n End While f.Close datastm.Close data.Close End Sub
Public Sub DownLoad(ByVal filename As String, ByVal binary As Boolean) If binary Then Command(ctrlstm, "TYPE I") Else Command(ctrlstm, "TYPE A") End If SetupData Command(ctrlstm, "RETR " + filename) Receive(ctrlstm) Dim f As FileStream = New FileStream (filename, FileMode.Create) Dim b(100000 - 1) As Byte Dim n As Integer While datastm.DataAvailable n = datastm.Read(b, 0, b.Length) f.Write(b, 0, n) Thread.Sleep(100) End While f.Close datastm.Close data.Close End Sub
Public Sub Logout() Command(ctrlstm, "QUIT") ctrlstm.Close ctrl.Close End Sub End Class
Class TestClass Public Shared Sub Main(ByVal args As String()) Dim cli As FtpClient = New FtpClient ("localhost", "anonymous", "arne@") Console.WriteLine(cli.ShortDir) Console.WriteLine(cli.Dir) Directory.SetCurrentDirectory("C:\") cli.DownLoad("z.zip", True) cli.MkDir("subdir") cli.ChDir("subdir") cli.UpLoad("z.zip", True) cli.Logout End Sub End Class
Public Function Dir() As String SetupData Command(ctrlstm, "LIST") Dim res As String = Receive(datastm) Receive(ctrlstm) datastm.Close data.Close Return res End Function
->
Public Function Dir() As String SetupData Command(ctrlstm, "LIST") Dim res As String = Receive(datastm) Dim resp As String = Receive(ctrlstm) Dim ix As Integer = resp.IndexOf(" ") Dim code As Integer = Int32.Parse(res.Substring(0, ix - 1)) Console.WriteLine("Vi got a " & code) datastm.Close data.Close Return res End Function
Public Class FtpClient Private ctrl As TcpClient Private ctrlstm As NetworkStream Private data As TcpClient Private datastm As NetworkStream
Private Shared Sub Send(ByVal stm As NetworkStream, ByVal line As String) Dim b As Byte() = Encoding.Default.GetBytes(line) stm.Write(b, 0, b.Length) End Sub
Private Shared Function Receive(ByVal stm As NetworkStream) As String Dim b(100000 - 1) As Byte Dim ix As Integer = 0 Dim n As Integer While stm.DataAvailable n = stm.Read(b, ix, b.Length - ix) ix += n End While
Return Encoding.Default.GetString(b, 0, ix) End Function
Public Shared Function Command(ByVal stm As NetworkStream, ByVal cmd As String) As String Send(stm, cmd + Convert.ToChar(13) + Convert.ToChar(10)) Thread.Sleep(250)
'Dim resp As String = Receive(stm) 'Dim ix As Integer = resp.IndexOf("") 'Dim code As Integer = Int32.Parse(res.substring(0, ix - 1))
Return Receive(stm)
End Function
Private Sub SetupData() Dim dataaddr As String = Command(ctrlstm, "PASV") Dim addrparts As String() = dataaddr.Split("()".ToCharArray)(1).Split(",".ToCharArray) Dim datahost As String = addrparts(0) + "." + addrparts(1) + "." + addrparts(2) + "." + addrparts(3) Dim dataport As Integer = Integer.Parse(addrparts(4)) * 256 + Integer.Parse(addrparts(5)) data = New TcpClient(datahost, dataport) datastm = data.GetStream End Sub
Public Sub New(ByVal host As String, ByVal username As String, ByVal password As String) ctrl = New TcpClient(host, 21) ctrlstm = ctrl.GetStream Command(ctrlstm, "USER " + username) Command(ctrlstm, "PASS " + password) End Sub
Public Function Dir() As String SetupData() Command(ctrlstm, "LIST") Dim res As String = Receive(datastm) Dim resp As String = Receive(ctrlstm) Dim ix As Integer = resp.IndexOf(" ") Dim code As Integer = Int32.Parse(resp.Substring(0, ix - 1)) Console.WriteLine("Vi got a " & code)
datastm.Close() data.Close() Return res End Function
Public Function ShortDir() As String SetupData() Command(ctrlstm, "NLST") Dim res As String = Receive(datastm) Receive(ctrlstm) datastm.Close() data.Close() Return res End Function
Public Sub MkDir(ByVal dir As String) Command(ctrlstm, "MKD " + dir) End Sub
Public Sub RmDir(ByVal dir As String) Command(ctrlstm, "RMD " + dir) End Sub
Public Sub Delete(ByVal dir As String) Command(ctrlstm, "DELE " + dir) End Sub
Public Sub ChDir(ByVal dir As String) Command(ctrlstm, "CWD " + dir) End Sub
Public Sub UpLoad(ByVal filename As String, ByVal binary As Boolean) If binary Then Command(ctrlstm, "TYPE I") Else Command(ctrlstm, "TYPE A") End If SetupData() Command(ctrlstm, "STOR " + filename) Receive(ctrlstm) Dim f As FileStream = New FileStream(filename, FileMode.Open) Dim b(100000 - 1) As Byte Dim ix As Integer = 0 Dim n As Integer While ix < f.Length n = f.Read(b, 0, b.Length) datastm.Write(b, 0, n) Thread.Sleep(100) ix += n End While f.Close() datastm.Close() data.Close() End Sub
Public Sub DownLoad(ByVal filename As String, ByVal binary As Boolean) If binary Then Command(ctrlstm, "TYPE I") Else Command(ctrlstm, "TYPE A") End If SetupData() Command(ctrlstm, "RETR " + filename) Receive(ctrlstm) Dim f As FileStream = New FileStream(filename, FileMode.Create) Dim b(100000 - 1) As Byte Dim n As Integer While datastm.DataAvailable n = datastm.Read(b, 0, b.Length) f.Write(b, 0, n) Thread.Sleep(100) End While f.Close() datastm.Close() data.Close() End Sub
Public Sub Logout() Command(ctrlstm, "QUIT") ctrlstm.Close() ctrl.Close() End Sub
End Class
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.