-
July 7th, 2010, 03:25 PM
#1
Winsock and File Transfers
I've been trying to make a program over the past week that reads a file and sends it via Winsock to another IP. I've had luck with Winsock in transferring small portions of text but I can't seem to get it to do the same with files. Here's the code I've written for this.
Code:
Imports System.IO
Public Class FileTransfer
Const BYTE_SIZE As Integer = 8000
Dim remoteSaveAs As String = ""
' sends the file
Private Sub transferButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles transferButton.Click
Dim saveAs As String = saveAsTextBox.Text
Dim fileName As String = fileTextBox.Text
Dim filestream As System.IO.FileStream = Nothing
Dim buffer(BYTE_SIZE - 1) As Byte
Dim bytesRead As Integer = 0
Dim totalBytesRead As Long = 0
If saveAs = "" Then
MsgBox("You must enter a name to save the file as", MsgBoxStyle.Critical, "Error")
End If
filestream = New System.IO.FileStream(fileName, _
IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
Try
Do
bytesRead = filestream.Read(buffer, 0, BYTE_SIZE)
If (bytesRead > 0) Then
FTWinsock2.SendData(saveAs)
FTWinsock.SendData(buffer)
totalBytesRead += bytesRead
buffer = Nothing
End If
Loop While bytesRead > 0
Catch ex As Exception
MsgBox("ERROR", MsgBoxStyle.Critical, "ERROR")
Me.Hide()
End Try
End Sub
' receives file
Private Sub FTWinsock_DataArrival(ByVal sender As Object, _
ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) _
Handles FTWinsock.DataArrival
Dim buffer(BYTE_SIZE - 1) As Byte
Dim filestream As System.IO.FileStream = Nothing
filestream = New System.IO.FileStream(remoteSaveAs, _
IO.FileMode.Open, IO.FileAccess.Write, IO.FileShare.Write)
FTWinsock.GetData(buffer)
filestream.Write(buffer, 0, BYTE_SIZE)
End Sub
' establishes connection
Private Sub FileTransfer_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Sets the Host
Dim IPAddress As String = InputBox("Please input the IP of the other computer.")
If IPAddress = "" Then
IPAddress = InputBox("You must enter an IP address.")
FTWinsock.RemoteHost = IPAddress
FTWinsock2.RemoteHost = IPAddress
Else
FTWinsock.RemoteHost = IPAddress
FTWinsock.RemoteHost = IPAddress
End If
End Sub
Private Sub selectFileButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles selectFileButton.Click
Dim fileName As String = ""
Dim OpenFileDialog As New OpenFileDialog()
If OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
fileName = OpenFileDialog.FileName
fileTextBox.Text = fileName
End If
End Sub
Private Sub FTWinsock2_DataArrival(ByVal sender As Object, _
ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) _
Handles FTWinsock2.DataArrival
If remoteSaveAs = "" Then
FTWinsock2.GetData(remoteSaveAs)
End If
End Sub
End Class
When I run this I get a System.Runtime.IntropServices.COMException. Prior to this code I had another version that read the entire file into the buffer and then sent it. That code gave me a System.Reflection.TargetException, so I assumed I was sending too much at once and made this to send the file in chunks instead. Is Winsock just not a good method to send files with? What am I doing wrong?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|