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?
Re: Winsock and File Transfers
Doesn't seem right to me. Download the samples, and take a look.
http://code.msdn.microsoft.com/vb2010samples
Re: Winsock and File Transfers
Use the System.Net.Sockets class.
Re: Winsock and File Transfers
Quote:
Originally Posted by
DataMiser
Use the System.Net.Sockets class.
Ok I'm a little unfamiliar with that class. I'm able to set up the client side ok but I can't figure out how to get the listener to retrieve and save the bytes as they're coming in.
Re: Winsock and File Transfers
Your listener should accept the connection at which point that connection [socket] is what will be receiving data, the listener will keep on listening for other connections.
There are samples included with VS.
Re: Winsock and File Transfers
Ok I got that working. One more thing though. I was only able to connect to the local host using sockets. Is it possible to connect to a remote IP?
Re: Winsock and File Transfers
Yes, I do it all the time.
Re: Winsock and File Transfers
Good I'm glad to hear that. Well here's my specific problem. Every time I enter a remote IP to listen on the TcpListener refuses to start and throws a SocketException. I've tried both IPAddress.Parse("IP String") and IPAddress.Any but get the same result. Any idea what might be happening here?
Re: Winsock and File Transfers
You put the listener on the server, and can connect to it from other IP's
Re: Winsock and File Transfers
Yes the Listener uses the local address and clients connect to it be they local or remote by issuing a connection request to the IP of the listener.
Re: Winsock and File Transfers
Hmm, now my connection request is being refused by the server. This would seem like a firewall problem but I disabled it for a couple minutes and it still refused it. Would being connected through a router cause a problem?
Re: Winsock and File Transfers
It could, depends on what port you are using, also depends on if it is the client on the other side of the router or the listener. If both are on the same side of the router then should not be a concern.
Re: Winsock and File Transfers
Use the PING command. PING /?
Might be Windows Firewall, or AV
Re: Winsock and File Transfers
Quote:
Originally Posted by
DataMiser
It could, depends on what port you are using, also depends on if it is the client on the other side of the router or the listener. If both are on the same side of the router then should not be a concern.
So are you saying that some ports don't work? I was using 13000.
Re: Winsock and File Transfers
The firewall in most routers block some ports by default. I can't tell you much as I do not have any idea of what your test environment is actually like. Typically a rejected message could indicate that your listener is not running.