Winsock and File Transfers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 34

Thread: Winsock and File Transfers

  1. #1
    Join Date
    Jul 2010
    Posts
    13

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,949

    Re: Winsock and File Transfers

    Doesn't seem right to me. Download the samples, and take a look.

    http://code.msdn.microsoft.com/vb2010samples
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: Winsock and File Transfers

    Use the System.Net.Sockets class.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jul 2010
    Posts
    13

    Re: Winsock and File Transfers

    Quote Originally Posted by DataMiser View Post
    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.

  5. #5
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    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.
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Jul 2010
    Posts
    13

    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?

  7. #7
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: Winsock and File Transfers

    Yes, I do it all the time.
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Jul 2010
    Posts
    13

    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?

  9. #9
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,949

    Re: Winsock and File Transfers

    You put the listener on the server, and can connect to it from other IP's
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    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.
    Always use [code][/code] tags when posting code.

  11. #11
    Join Date
    Jul 2010
    Posts
    13

    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?

  12. #12
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    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.
    Always use [code][/code] tags when posting code.

  13. #13
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,949

    Re: Winsock and File Transfers

    Use the PING command. PING /?

    Might be Windows Firewall, or AV
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  14. #14
    Join Date
    Jul 2010
    Posts
    13

    Re: Winsock and File Transfers

    Quote Originally Posted by DataMiser View Post
    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.

  15. #15
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    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.
    Always use [code][/code] tags when posting code.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center