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

Thread: Winsock and File Transfers

  1. #16
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: Winsock and File Transfers

    Most routers are open to the normal protocols
    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!

  2. #17
    Join Date
    Jul 2010
    Posts
    13

    Re: Winsock and File Transfers

    Ok well I've tested this a few different ways but haven't gotten anywhere. I don't think it's the router though because I turned off its firewall (and the computer's again) to test this. I also tested it on different computers both inside and outside the router with the same result. To me it seems like VB is purposely rejecting it. Is it possible for that to occur?

  3. #18
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,860

    Re: Winsock and File Transfers

    Show some of your code and maybe someone can help. At the moment you are not giving us much to go on. If you could show the part of your client code that attempts the connection and the part of your server code that accepts the connection then we might have an idea of how to direct you. As is for all we know your code is not accepting the connection.
    Always use [code][/code] tags when posting code.

  4. #19
    Join Date
    Jul 2010
    Posts
    13

    Re: Winsock and File Transfers

    I thought I should have posted this earlier. Alright here's the code.

    First the client side.

    Code:
        Private Sub transferButton_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles transferButton.Click
    
            Const BYTE_SIZE As Integer = 1
            Dim stream As NetworkStream
            Dim fileToSend As String = fileToSendTextBox.Text
            Dim saveAs As String = saveAsTextBox.Text
            Dim IPAddress As String = "24.129.26.34"
            Dim filestream As System.IO.FileStream = Nothing
            Dim buffer(BYTE_SIZE - 1) As Byte
            Dim bytesRead As Integer = 0
            Dim port As Int32 = 13000
            Dim client As New TcpClient
    
            client.Connect(IPAddress, port)
    
            If fileToSend = "" Then
                MsgBox("You must choose a file", MsgBoxStyle.Critical, "Error")
            End If
    
            stream = client.GetStream()
    
            filestream = New System.IO.FileStream(fileToSend, _
                IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
    
            Try
                Do
    
                    bytesRead = filestream.Read(buffer, 0, BYTE_SIZE)
    
                    If (bytesRead > 0) Then
                        stream.Write(buffer, 0, BYTE_SIZE)
                    End If
                Loop While bytesRead > 0
    
            Catch ex As Exception
                MsgBox("ERROR", MsgBoxStyle.Critical, "ERROR")
                Me.Hide()
            End Try
    
        End Sub
    And now the server

    Code:
        Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
            Dim stream As NetworkStream
            Dim server As TcpListener
            Dim port As Int32 = 80
            Dim IP As IPAddress = IPAddress.Parse("127.0.0.1")
    
    
            server = New TcpListener(IP, port)
    
            server.Start()
    
            While True
                Dim i As Int32
                Dim client As TcpClient = server.AcceptTcpClient()
                stream = client.GetStream()
    
    
                i = stream.Read(bytes, 0, bytes.Length)
                While (i <> 0)
                    Write()
                    i = stream.Read(bytes, 0, bytes.Length)
                End While
                client.Close()
            End While

  5. #20
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,860

    Re: Winsock and File Transfers

    Your server side code looks rather odd.

    Here are some snippets from my server code.

    In my server start routine I have

    Code:
    SocketHandler = New TcpListener(hIPAddress, hPort)  ' Set the IP AND Port Number
    SocketHandler.Start()
    I have a timer running which checks to see if there are any pending connections every 100ms.

    Code:
    If SocketHandler.Pending() Then
                CurThreadStart = New ThreadStart(AddressOf ConnectionBegin)     ' Set info for the new thread
                CurThread = New Thread(CurThreadStart)                          ' Initialize the new thread variable
                CurThread.SetApartmentState(ApartmentState.STA)
                CurThread.Start()                                               ' Start the thread
                SyncLock CurThread                                              ' Lock the thread for update
                    VAActiveThreads += 1                                        ' increment active threads
                    cInfo.ActiveClients += 1
                    CurThread.Name = "ClientCon" + VAActiveThreads.ToString        ' Name the thread
                    Console.WriteLine(CurThread.Name & " Thread Started")
                End SyncLock
    End If
    Then in my ConnectionBegin which is launched by the code above in a new thread I have.

    Code:
    Dim ClientSocket As Socket = SocketHandler.AcceptSocket
    Followed by a loop that will read from the port until there is a idle timeout condition.
    Always use [code][/code] tags when posting code.

  6. #21
    Join Date
    Jul 2010
    Posts
    13

    Re: Winsock and File Transfers

    Code:
        Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
    
            server = New TcpListener(IP, port)
    
            server.Start()
    
            
        End Sub
    
        Private Sub FTTimer_Tick(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles FTTimer.Tick
    
            If server.Pending() Then
                threadStart = New ThreadStart(AddressOf ConnectionBegin)
                thread = New Thread(threadStart)
                thread.SetApartmentState(ApartmentState.STA)
                thread.Start()
            End If
        End Sub
    
        Private Sub ConnectionBegin()
            While True
                Dim i As Int32
                Dim client As TcpClient = server.AcceptTcpClient()
                stream = client.GetStream()
    
    
                i = stream.Read(bytes, 0, bytes.Length)
                While (i <> 0)
                    Write()
                    i = stream.Read(bytes, 0, bytes.Length)
                End While
                client.Close()
            End While
        End Sub
    Is this slightly better? Even with the addition of a pending check, the listener still can't seem to "hear" that remote connection.

  7. #22
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,860

    Re: Winsock and File Transfers

    What's with the while true loop? You need to ditch that.
    Last edited by DataMiser; July 26th, 2010 at 09:12 PM.
    Always use [code][/code] tags when posting code.

  8. #23
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,860

    Re: Winsock and File Transfers

    Duh... I just reviewed your code posted earlier and I see a problem with your port assignments.

    Your server is listening on port 80 but your client is trying to connect on port 13000.

    You must use the port that the listener is actually listening on if you expect a connection as well as the IP that the server is on.
    Always use [code][/code] tags when posting code.

  9. #24
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,860

    Re: Winsock and File Transfers

    Also looks like there is a problem with your IP address.

    If your server is on IP 123.456.789.0 and is Listening on Port 24000 then your client would need to try to connect to IP 123.456.789.0 on Port 24000.

    If your client trys to connect to IP 456.123.789.0 It will not connect because that is not where your server is at. If your client tries to connect on port 1234 if will not connect because that is not the port you are listening to.
    Always use [code][/code] tags when posting code.

  10. #25
    Join Date
    Jul 2010
    Posts
    13

    Re: Winsock and File Transfers

    I'm a little confused now. I fixed the port mistake, but I don't quite get what you mean when you say there's something wrong with my IP. I AM supposed to be listening on the localhost correct? The way I have it setup my server listens on the localhost and the client connects to it via the server's remote IP.

  11. #26
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: Winsock and File Transfers

    If your server is on IP 123.456.789.0 and is Listening on Port 24000 then your client would need to try to connect to IP 123.456.789.0 on Port 24000.
    That means, for you code to work, your server would have to be IP 123.456.789.0 on Port 24000 (on the server)
    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!

  12. #27
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,860

    Re: Winsock and File Transfers

    Quote Originally Posted by Murjax View Post
    I'm a little confused now. I fixed the port mistake, but I don't quite get what you mean when you say there's something wrong with my IP. I AM supposed to be listening on the localhost correct? The way I have it setup my server listens on the localhost and the client connects to it via the server's remote IP.
    The client would use the address of the server. If your server address is 127.0.0.1 as in your example above then your client would have to use 127.0.0.1

    For example If you want to connect to Google you will not get there by typing in Yahoo.

    Btw you can also use the machine name rather than a hard coded IP address. Works much better especially when the IP is DHCP controlled.

    Code:
    Dim hInfo As IPHostEntry = Nothing
    Dim hIPAddress As IPAddress = Nothing
    hInfo = Dns.GetHostEntry("Machine Name Here")
    hIPAddress = hInfo.AddressList(0)
    Last edited by DataMiser; July 27th, 2010 at 08:35 PM.
    Always use [code][/code] tags when posting code.

  13. #28
    Join Date
    Jul 2010
    Posts
    13

    Re: Winsock and File Transfers

    That's how I originally thought the connection was supposed to work. The problem (as I stated some posts back) is that when I have the server listen on any IP other than 127.0.0.1 a SocketException is thrown saying that "The requested address is not valid in its context".

  14. #29
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,860

    Re: Winsock and File Transfers

    You must use an address that is valid on the machine where the server is being executed. This is your Local Host. The client must use that same IP address and port as the parameters for the connection.

    Here is a bit of code from my server. Note I do not hard code any IP address.

    Code:
    Dim hInfo As System.Net.IPHostEntry = Dns.GetHostEntry(Dns.GetHostName)
    Dim hIPAddress As IPAddress = Nothing
    hIPAddress = hInfo.AddressList(0)
    
    SocketHandler = New TcpListener(hIPAddress, hPort)  ' Set the IP AND Port Number
    SocketHandler.Start()
    Always use [code][/code] tags when posting code.

  15. #30
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: Winsock and File Transfers

    The question should have been WHEN DO I USE 127.0.0.1 on Both the CLIENT AND THE HOST?

    That answer would be when both are running on the SAME MACHINE. You can run client and server on the same machine.
    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!

Page 2 of 3 FirstFirst 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center