Imports System.Net.Sockets Imports System.Text Imports System.Net Imports System.IO Public Class Form1 Dim Client As New TcpClient() ' Create a TcpClient. Dim Stream As NetworkStream 'use to send and receive data Dim TransferClient As New TcpClient() 'second to transfere files Dim TransferStream As NetworkStream 'use to send and receive data on second socket Dim TransferEndpoint As IPEndPoint 'ip address and port of server for file transfere Dim IP As String 'ipaddress of transfer Dim Port As Integer 'port to transfer Dim responsecode As String Dim data As Byte() ' Buffer to store the response bytes. Dim responseData As [String] = [String].Empty ' String to store the response ASCII Dim waiting As Boolean = False 'waiting after logon Dim filepath As String 'save path on client Dim filesize As Integer 'size of file to transfer Dim subsize As Integer 'size of each seconds worth of data from server Dim filend As String 'file name for uploading file Dim upsize As Integer 'size file to upload Dim uptime As Integer 'time to upload file Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'DISPLAY IP CLIENT ADDRESS RichTextBox1.Clear() ' Clear all text from the RichTextBox; RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Blue 'so make it blue RichTextBox1.SelectedText = "Client IPAddress " Dim hostname = System.Net.Dns.GetHostName() Dim IPAddress = System.Net.Dns.GetHostEntry(hostname).AddressList(0).ToString() RichTextBox1.SelectionColor = Color.Black 'so make it black RichTextBox1.SelectedText = IPAddress & vbNewLine End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click 'DOWNLOAD FILE DIRECTORY If Client.Connected Then waiting = False sendrecv("PASV" & vbNewLine) 'get set to transfere files If responsecode = "227" Then connecttransfer() sendrecv("TYPE A" & vbNewLine) 'ascii transfer sendrecv("LIST" & vbNewLine) 'directory listing If responsecode = "150" Then TransferStream = TransferClient.GetStream() 'load data from socket data = New [Byte](4024) {} 'allow for directory size Timer6.Enabled = True End If Else RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Red 'so make it red RichTextBox1.SelectedText = "server not ready for transfere" & vbNewLine End If End If End Sub Private Sub Timer6_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer6.Tick 'WAIT FOR DIRECTORY TO LOAD If TransferStream.DataAvailable Then ' Read the first batch of the TcpServer response bytes. Dim bytes As Int32 = TransferStream.Read(data, 0, data.Length) responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes) RichTextBox1.SelectionFont = New Font("verdana", 8, FontStyle.Regular) RichTextBox1.SelectedText = responseData & vbNewLine 'for directory End If TransferClient.Close() TransferStream.Close() If Stream.DataAvailable Then 'check control stream recvdisplay() 'should get 226 file done End If Timer6.Enabled = False waiting = True End Sub Private Sub connecttransfer() ' The IP address and port number is appended to the response. Dim StartPos As Integer = responseData.IndexOf("(") Dim EndPos As Integer = responseData.IndexOf(")") Dim IPAndPort As String = responseData.Substring(StartPos + 1, _ EndPos - StartPos - 1) Dim IPParts() As String = IPAndPort.Split(","c) IP = IPParts(0) + "." + IPParts(1) + "." + IPParts(2) + "." + IPParts(3) Port = Convert.ToInt32(IPParts(4)) * 256 + Convert.ToInt32(IPParts(5)) RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Blue 'so make it blue RichTextBox1.SelectedText = "Transfer IPAddress " RichTextBox1.SelectionColor = Color.Black 'so make it black RichTextBox1.SelectedText = IP & " Port " & Port & vbNewLine 'display ' Create the data transfer connection. RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectedText = "Connect to transfer socket " & IP & vbNewLine TransferClient = New TcpClient() 'new socket for data transfer TransferEndpoint = New IPEndPoint(IPAddress.Parse(IP), Port) Try 'use try block if cannot connect to server TransferClient.Connect(TransferEndpoint) Catch err As Exception 'write error message RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Red 'so make it red RichTextBox1.SelectedText = "cannot connect to transfer server" & vbNewLine End Try End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click 'DOWNLOAD FILE If Client.Connected Then waiting = False sendrecv("PASV" & vbNewLine) 'get set to transfere files If responsecode = "227" Then connecttransfer() sendrecv("TYPE I" & vbNewLine) 'binary mode sendrecv("RETR " & TextBox4.Text & vbNewLine) 'download file If responsecode = "150" Then filesize = responseData.Substring(responseData.IndexOf("(") + 1, responseData.IndexOf(")") - responseData.IndexOf("(") - 7) RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Green 'so make it green RichTextBox1.SelectedText = "Downloading " & TextBox4.Text & " " & filesize & " bytes" & vbNewLine TransferStream = TransferClient.GetStream() 'load data from socket data = New [Byte](filesize - 1) {} 'buffer for each file subsize = 0 'start from 0 in data buffer Timer5.Enabled = True 'each second load received data into buffer End If Else RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Red 'so make it red RichTextBox1.SelectedText = "server not ready for transfere" & vbNewLine End If End If End Sub Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick 'EACH SECOND LOAD NEW DATA INTO FILE If TransferStream.DataAvailable Then 'Read the first batch of the TcpServer response bytes. Dim bytes As Int32 = TransferStream.Read(data, 0, data.Length) 'return size of received data subsize = subsize + bytes 'load this chunk of file streamdata Dim percent As Integer = subsize / filesize * 100 '% of total file received RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Green 'so make it green RichTextBox1.SelectedText = "Downloaded " & bytes & " bytes per second " & " total so far " & subsize & " bytes " & percent & " % of file" & vbNewLine Dim subdata As Byte() = New [Byte](bytes - 1) {} For i = 0 To bytes - 1 'copy the received data this is because cannot subdata(i) = data(i) 'get length of transferstream - not supported Next filepath = System.IO.Path.Combine(TextBox5.Text, TextBox4.Text) My.Computer.FileSystem.WriteAllBytes(filepath, subdata, True) 'save End If If subsize = filesize Then 'have we done all of the file TransferClient.Close() TransferStream.Close() If Stream.DataAvailable Then 'check control stream recvdisplay() 'should get 226 file done End If Timer5.Enabled = False 'yes all done so stop looking at transfer stream waiting = True End If End Sub Private Sub sendrecv(ByVal message As [String]) 'display the message to be sent RichTextBox1.SelectionFont = New Font("verdana", 12, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Blue 'so make it blue RichTextBox1.SelectedText = message.Substring(0, 4) 'the response code RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Black 'so make it black RichTextBox1.SelectedText = " " & message.Substring(4) 'rest of message ' Translate the passed message into ASCII and store it as a Byte array. Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message) ' Send the message to the connected TcpServer. Stream.Write(data, 0, data.Length) ' Receive the TcpServer.response. recvdisplay() End Sub Private Sub recvdisplay() data = New [Byte](256) {} ' Read the first batch of the TcpServer response bytes. Dim bytes As Int32 = Stream.Read(data, 0, data.Length) responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes) RichTextBox1.SelectionFont = New Font("verdana", 12, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Red 'so make it red responsecode = responseData.Substring(0, 3) RichTextBox1.SelectedText = responsecode 'the response code RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Black 'so make it black RichTextBox1.SelectedText = " " & responseData.Substring(4) 'rest of message End Sub Private Sub connect() Client = New TcpClient RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectedText = "Connect to server " & TextBox1.Text & vbNewLine Try 'use try block if cannot connect to server ' Note, for this client to work you need to have a TcpServer ' connected to the same address as specified by the server, port combination Client.Connect(TextBox1.Text, 21) Stream = Client.GetStream() 'get response from server recvdisplay() Catch err As Exception 'write error message RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Red 'so make it red RichTextBox1.SelectedText = "cannot connect to server" & vbNewLine End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'CONNECT TO SERVER If Client.Connected Then Button2_Click(Nothing, Nothing) 'disconnect first connect() 'connect to server If Client.Connected Then If responsecode = "220" Then sendrecv("USER " & TextBox2.Text & vbNewLine) Else RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Red 'so make it red RichTextBox1.SelectedText = "server not ready" & vbNewLine End If If responsecode = "331" Then sendrecv("PASS " & TextBox3.Text & vbNewLine) Else RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Red 'so make it red RichTextBox1.SelectedText = "user name incorrect" & vbNewLine End If If responsecode <> "230" Then RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Red 'so make it red RichTextBox1.SelectedText = "could not logon" & vbNewLine End If Timer3.Enabled = True End If End Sub Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick 'MORE LOGON DATA COULD BE SENT If Stream.DataAvailable Then data = New [Byte](1024) {} ' Read the first batch of the TcpServer response bytes. Dim bytes As Int32 = Stream.Read(data, 0, data.Length) responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes) RichTextBox1.SelectionFont = New Font("verdana", 8, FontStyle.Regular) RichTextBox1.SelectedText = responseData End If Timer3.Enabled = False waiting = True End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 'CHECK CONNECTIONS If Client.Connected Then Label1.BackColor = Color.Green Label1.Text = "Server" & vbNewLine & "is" & vbNewLine & "connected" Else Label1.BackColor = Color.Red Label1.Text = "Server" & vbNewLine & "not" & vbNewLine & "connected" End If If TransferClient.Connected Then Label2.BackColor = Color.Green Label2.Text = "Transfer" & vbNewLine & "is" & vbNewLine & "connected" Else Label2.BackColor = Color.Red Label2.Text = "Tranfer" & vbNewLine & "not" & vbNewLine & "connected" End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'DISCONNECT SERVER If Client.Connected Then sendrecv("QUIT" & vbNewLine) Timer4.Enabled = True End If End Sub Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick 'WAIT FOR MORE DATA AFTER DISCONNECT If Stream.DataAvailable Then data = New [Byte](1024) {} ' Read the first batch of the TcpServer response bytes. Dim bytes As Int32 = Stream.Read(data, 0, data.Length) responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes) RichTextBox1.SelectedText = responseData End If If responsecode = "221" Then 'closing so disconnect client Client.Close() Stream.Close() waiting = False End If Timer4.Enabled = False End Sub Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick 'CHECK FOR TIMEOUT AT SERVER If waiting And Client.Connected Then If Stream.DataAvailable Then recvdisplay() End If If responsecode = "421" Then Client.Close() Stream.Close() 'disconnect client waiting = False End If End If End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Dim dlg As New FolderBrowserDialog dlg.SelectedPath = "C:\" dlg.Description = "browse for a folder" If dlg.ShowDialog = DialogResult.OK Then TextBox5.Text = dlg.SelectedPath End If End Sub Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click 'GO TO ROOT DIRECTORY If Client.Connected Then sendrecv("CWD" & vbNewLine) End If End Sub Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click 'OPEN NEW DIRECTORY If Client.Connected Then sendrecv("CWD /" & TextBox6.Text & vbNewLine) End If End Sub Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click 'DISPLAY FTP INFORMATION Dim page As String = My.Resources.pagehtm 'add htm page into resources then Dim tempfile As String = Path.GetTempFileName & "pagehtm.htm" 'store in tempory My.Computer.FileSystem.WriteAllText(tempfile, page, True) 'file this time as text Process.Start(tempfile) 'then run the program that opens htm by this End Sub Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click 'UPLOAD FILE If Client.Connected Then Dim dlg As New OpenFileDialog 'have user select file to upload dlg.Title = "Select file for immediate upload to server" If dlg.ShowDialog = DialogResult.OK Then 'lets upload file waiting = False 'we are active Dim fileparts() As String = dlg.FileName.Split("\"c) 'lose directory filend = fileparts.Last 'get the actual file name Dim data As Byte() = My.Computer.FileSystem.ReadAllBytes(dlg.FileName) upsize = data.Length RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Green 'so make it green RichTextBox1.SelectedText = "Uploading " & vbNewLine & dlg.FileName & vbNewLine & data.Length & " total bytes" & vbNewLine sendrecv("PASV" & vbNewLine) 'get set to transfere files If responsecode = "227" Then connecttransfer() sendrecv("TYPE I" & vbNewLine) 'binary mode sendrecv("STOR " & filend & vbNewLine) 'upload file If responsecode = "150" Then TransferStream = TransferClient.GetStream() TransferStream.Write(data, 0, data.Length) uptime = 0 Timer7.Enabled = True 'each second If Stream.DataAvailable Then 'check control stream recvdisplay() 'should get 226 file done End If TransferClient.Close() TransferStream.Close() waiting = True End If Else RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Red 'so make it red RichTextBox1.SelectedText = "server not ready for transfere" & vbNewLine End If Else : RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Red 'so make it red RichTextBox1.SelectedText = "Upload file cancelled" & vbNewLine End If End If End Sub Private Sub Timer7_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer7.Tick 'UPLOAD DETAILS uptime = uptime + 1 'time to upload file RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Green 'so make it green RichTextBox1.SelectedText = "uploading data to " & filend & vbNewLine If responsecode = "226" Then RichTextBox1.SelectionFont = New Font("verdana", 10, FontStyle.Bold) RichTextBox1.SelectionColor = Color.Green 'so make it green RichTextBox1.SelectedText = "uploaded " & upsize & " bytes in " & uptime & " seconds " & Math.Round(upsize / uptime) & " bytes per second " & vbNewLine Timer7.Enabled = False End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 'DELETE FILE If Client.Connected Then sendrecv("DELE" & TextBox4.Text & vbNewLine) End If End Sub Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click 'OPEN FOLDER DIRECTORY If Client.Connected Then sendrecv("MKD " & TextBox6.Text & vbNewLine) End If End Sub Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click 'CLOSE FOLDER DIRECTORY If Client.Connected Then sendrecv("RMD " & TextBox6.Text & vbNewLine) End If End Sub End Class