File Transfer App
I wrote a File Trasfer App that runs as a command line program. It times out and doesn't send the file.
Any ideas?
Thanks In Advance
Imports System.Net.Sockets
Imports System.Net
Imports System
Imports System.IO
Imports System.Console
Module Globals
Sub Main(ByVal args As String())
If args.Length < 1 Then InvalidUsage() : Return
Select Case args(0).ToLower()
Case "-s", "/s" 'Sending
If args.Length < 4 Then WriteLine("Insufficient arguments") : Return
Dim address As IPAddress
If Not IPAddress.TryParse(args(1), address) Then _
WriteLine("Invalid IP Address") : Return
Dim port As Integer
If Not Integer.TryParse(args(2), port) Then _
WriteLine("Invalid Port") : Return
DoSend(args(3), address, port)
Case "-r", "/r" 'Receiving
If args.Length < 3 Then WriteLine("Insufficient arguments") : Return
Dim port As Integer
If Not Integer.TryParse(args(1), port) Then _
WriteLine("Invalid port") : Return
DoReceive(args(2), port)
Case "-?", "/?"
ShowUsage()
Case Else
InvalidUsage()
End Select
End Sub
Sub CopyStream(ByVal source As Stream, ByVal destination As Stream)
Dim buffer As Byte() = New Byte(255) {}
Dim bytesRead As Integer
Do
bytesRead = source.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then Return
destination.Write(buffer, 0, bytesRead)
Loop
End Sub
Sub DoSend(ByVal filePath As String, ByVal address As IPAddress, ByVal port As Integer)
If Not File.Exists(filePath) Then
WriteLine("Specified File does not exist")
Return
End If
Dim remoteEP As IPEndPoint = New IPEndPoint(address, port)
Using connection As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp), _
file As New FileStream(filePath, FileMode.Open)
connection.Connect(remoteEP)
Dim netStream As New NetworkStream(connection, False)
CopyStream(file, netStream)
End Using
End Sub
Sub DoReceive(ByVal filePath As String, ByVal port As Integer)
Dim listener, connection As Socket
Dim file As FileStream
Dim netStream As NetworkStream
If IO.File.Exists(filePath) Then
WriteLine("The specified file name already exists!")
Write("Overwrite [y/n]? ")
If ReadLine().ToUpper()(0) <> "Y"c Then Return
End If
Try
listener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
listener.Bind(New IPEndPoint(IPAddress.Any, port))
listener.Listen(1)
Dim ar As IAsyncResult = listener.BeginAccept(Nothing, Nothing)
If Not ar.AsyncWaitHandle.WaitOne(TimeSpan.From Seconds(15), False) Then
WriteLine("Timed out")
Return
End If
connection = listener.EndAccept(ar)
netStream = New NetworkStream(connection)
file = New FileStream(filePath, FileMode.Create)
CopyStream(netStream, file)
Catch ex As Exception
Write("Exception Encountered!: ") : WriteLine(ex.Message)
Finally
IDispose(listener, connection, file)
End Try
End Sub
Sub InvalidUsage()
WriteLine("Invalid Usage")
WriteLine()
ShowUsage()
End Sub
Friend Sub IDispose(ByVal ParamArray disposables As System.IDisposable())
If disposables Is Nothing Then Return
For Each disposable As System.IDisposable In disposables
If Not disposable Is Nothing Then disposable.Dispose()
Next
End Sub
Sub ShowUsage()
WriteLine("Usage:")
WriteLine("SendFile.exe -s ipAddress port filePath")
WriteLine("SendFile.exe -r port filePath")
WriteLine()
WriteLine("-s Sending mode")
WriteLine()
WriteLine("-r Receiving mode")
WriteLine()
WriteLine("ipAddress The IPv4 address of the computer to")
WriteLine(" which 'filePath' will be sent")
WriteLine()
WriteLine("port The port used for sending/receiving")
WriteLine()
WriteLine("filePath The path of the file being sent with '-s'")
WriteLine(" -OR-")
WriteLine(" The filename underwhich the received data")
WriteLine(" will be saved with '-r'")
End Sub
End Module
Any ideas?
Thanks In Advance
Imports System.Net.Sockets
Imports System.Net
Imports System
Imports System.IO
Imports System.Console
Module Globals
Sub Main(ByVal args As String())
If args.Length < 1 Then InvalidUsage() : Return
Select Case args(0).ToLower()
Case "-s", "/s" 'Sending
If args.Length < 4 Then WriteLine("Insufficient arguments") : Return
Dim address As IPAddress
If Not IPAddress.TryParse(args(1), address) Then _
WriteLine("Invalid IP Address") : Return
Dim port As Integer
If Not Integer.TryParse(args(2), port) Then _
WriteLine("Invalid Port") : Return
DoSend(args(3), address, port)
Case "-r", "/r" 'Receiving
If args.Length < 3 Then WriteLine("Insufficient arguments") : Return
Dim port As Integer
If Not Integer.TryParse(args(1), port) Then _
WriteLine("Invalid port") : Return
DoReceive(args(2), port)
Case "-?", "/?"
ShowUsage()
Case Else
InvalidUsage()
End Select
End Sub
Sub CopyStream(ByVal source As Stream, ByVal destination As Stream)
Dim buffer As Byte() = New Byte(255) {}
Dim bytesRead As Integer
Do
bytesRead = source.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then Return
destination.Write(buffer, 0, bytesRead)
Loop
End Sub
Sub DoSend(ByVal filePath As String, ByVal address As IPAddress, ByVal port As Integer)
If Not File.Exists(filePath) Then
WriteLine("Specified File does not exist")
Return
End If
Dim remoteEP As IPEndPoint = New IPEndPoint(address, port)
Using connection As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp), _
file As New FileStream(filePath, FileMode.Open)
connection.Connect(remoteEP)
Dim netStream As New NetworkStream(connection, False)
CopyStream(file, netStream)
End Using
End Sub
Sub DoReceive(ByVal filePath As String, ByVal port As Integer)
Dim listener, connection As Socket
Dim file As FileStream
Dim netStream As NetworkStream
If IO.File.Exists(filePath) Then
WriteLine("The specified file name already exists!")
Write("Overwrite [y/n]? ")
If ReadLine().ToUpper()(0) <> "Y"c Then Return
End If
Try
listener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
listener.Bind(New IPEndPoint(IPAddress.Any, port))
listener.Listen(1)
Dim ar As IAsyncResult = listener.BeginAccept(Nothing, Nothing)
If Not ar.AsyncWaitHandle.WaitOne(TimeSpan.From
WriteLine("Timed out")
Return
End If
connection = listener.EndAccept(ar)
netStream = New NetworkStream(connection)
file = New FileStream(filePath, FileMode.Create)
CopyStream(netStream, file)
Catch ex As Exception
Write("Exception Encountered!: ") : WriteLine(ex.Message)
Finally
IDispose(listener, connection, file)
End Try
End Sub
Sub InvalidUsage()
WriteLine("Invalid Usage")
WriteLine()
ShowUsage()
End Sub
Friend Sub IDispose(ByVal ParamArray disposables As System.IDisposable())
If disposables Is Nothing Then Return
For Each disposable As System.IDisposable In disposables
If Not disposable Is Nothing Then disposable.Dispose()
Next
End Sub
Sub ShowUsage()
WriteLine("Usage:")
WriteLine("SendFile.exe -s ipAddress port filePath")
WriteLine("SendFile.exe -r port filePath")
WriteLine()
WriteLine("-s Sending mode")
WriteLine()
WriteLine("-r Receiving mode")
WriteLine()
WriteLine("ipAddress The IPv4 address of the computer to")
WriteLine(" which 'filePath' will be sent")
WriteLine()
WriteLine("port The port used for sending/receiving")
WriteLine()
WriteLine("filePath The path of the file being sent with '-s'")
WriteLine(" -OR-")
WriteLine(" The filename underwhich the received data")
WriteLine(" will be saved with '-r'")
End Sub
End Module
