Results 1 to 7 of 7

Thread: P2P Connection - need help

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    4

    P2P Connection - need help

    Hi,

    Been looking into P2P codes and found a decent tutorial on how to create p2p applications with VB .NET, however it was just an example of how to connect client to client, but when I tried to use the code, make it connect to another pc (the "server") first, and make another .exe file (the server file) respond when a connection requests comes up, but it didn't work. When I open the client.exe on my other PC, and press the connect button (where it requests to connect to my other pc where I run the server.exe on), then I get a Microsoft .NET Framework Error saying the connection failed because the host or connected "party" hasn't responded correctly or in the right time.

    This are my codes:

    client.exe
    Code:
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.IO
    
    Public Class Form1
    
        Dim Listener As New TcpListener(65535)
        Dim Client As New TcpClient
        Dim Message As String = ""
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
            ListThread.Start()
        End Sub
    
        Private Sub Listening()
            Listener.Start()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Client = New TcpClient("192.168.0.103", 65535)
    
            Dim strHostName As String
    
            Dim strIPAddress As String
    
    
    
            strHostName = System.Net.Dns.GetHostName()
    
            strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()
    
            Dim Writer As New StreamWriter(Client.GetStream())
            Writer.Write(strIPAddress)
            Writer.Flush()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Listener.Pending = True Then
    
                Message = ""
                Client = Listener.AcceptTcpClient()
    
                Dim Reader As New StreamReader(Client.GetStream())
                While Reader.Peek > -1
                    Message = Message + Convert.ToChar(Reader.Read()).ToString
                End While
    
                MsgBox(Message, MsgBoxStyle.OkOnly)
            End If
            Timer1.Interval = 1
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Listener.Stop()
        End Sub
    
    End Class
    server.exe
    Code:
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.IO
    
    Public Class Form1
    
        Dim Listener As New TcpListener(65535)
        Dim Client As New TcpClient
        Dim Message As String = ""
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
            ListThread.Start()
    
        End Sub
    
        Private Sub Listening()
            Listener.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Listener.Pending = True Then
                Message = ""
                Client = Listener.AcceptTcpClient()
    
                Dim Reader As New StreamReader(Client.GetStream())
                While Reader.Peek > -1
                    Message = Message + Convert.ToChar(Reader.Read()).ToString
                End While
    
                MsgBox(Message & " Connected!")
    
                ListBox1.Items.Add(Message)
    
                Client = New TcpClient("192.168.0.102", 65535)
    
                Dim Writer As New StreamWriter(Client.GetStream())
                Writer.Write("Connected")
                Writer.Flush()
    
            End If
            Timer1.Interval = 1
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Listener.Stop()
        End Sub
    
    End Class
    Where "xxx.xxx.x.xxx" in client.exe, is the IP of the computer where the server.exe runs on.

    What's wrong, why doesn't this work? (well, only when I send connection request from my computer to my laptop, then it does react on my laptop and gets the data sent, but when I send the connection request from my laptop to my computer, then it says it failed because the "host" (the client/server, running on my computer) didn't respond or not at the right time (while it has exactly the same code for sending and receiving/handling connection requests), quite weird... Could anyone please help me out? Would be very appreciated.

    Thanks in advanced,

    Skyfe.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: P2P Connection - need help

    So you're only getting the exception when the laptop attempts to connect to the desktop PC, but not when its the other way around?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    4

    Re: P2P Connection - need help

    Yep, exactly. Even when I used 2 same codes to connect and to handle connection requests, then attempting to send a connection request from my laptop to my PC didn't work, but the other way around (from my PC to my laptop) did work...

    EDIT: same happened when I tried to connect my pc with some other pc (which is nearly the same as mine, but hasn't got Visual Basic .NET on it, just like my laptop didn't (maybe they haven't got .NET FrameWork 3 installed correctly neither) ) and again I only could send from my pc to the other pc while I used exactly the same clients on both computers (well but then changed the IP address to send the connection request to ofcourse)... really weird! (does it have to do something with .NET FrameWork 3 or other required files which may not be on the other computer/laptop? Still weird if it would be that; that it says the host doesn't respond correctly since that doesn't really have to do something with required files right... but then quite clueless what it could be then...)
    Last edited by skyfe; Jan 21st, 2009 at 01:55 PM.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: P2P Connection - need help

    If you could run the application on the laptop, its got the required .NET framework, so that should be no problem.

    Did you run the "server" or the "client" on the laptop when you experienced the problems?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    4

    Re: P2P Connection - need help

    With both. Both could be ran on my computer, but none of them could be ran on my laptop. Even when I made 2 the same clients but with other IPs (one for my laptop and one for my computer), then still I could only connect from my PC to my Laptop, also tried it on another computer which has the same windows as mine, but there I got the same problem... (same problem as on my laptop, so it only works on my computer self)

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: P2P Connection - need help

    Well you have hardcoded strings for the IP's, are you changing them to whatever they need to be before you compile and run the exe's?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    4

    Re: P2P Connection - need help

    Well, that can't really be the problem because when I use this code:

    Code:
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.IO
    
    Public Class Form1
    
        Dim Listener As New TcpListener(65535)
        Dim Client As New TcpClient
        Dim Message As String = ""
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
            ListThread.Start()
        End Sub
    
        Private Sub Listening()
            Listener.Start()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Client = New TcpClient("192.168.0.103", 65535)
    
            Dim Writer As New StreamWriter(Client.GetStream())
            Writer.Write(RichTextBox1.Text)
            Writer.Flush()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Listener.Pending = True Then
                Message = ""
                Client = Listener.AcceptTcpClient()
    
                Dim Reader As New StreamReader(Client.GetStream())
                While Reader.Peek > -1
                    Message = Message + Convert.ToChar(Reader.Read()).ToString
                End While
    
                MsgBox(Message, MsgBoxStyle.OkOnly)
            End If
            Timer1.Interval = 10
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Listener.Stop()
        End Sub
    
    End Class
    For both PCs, then I also get the problem when connecting from any other pc or laptop (only works when I connect from this computer).

Posting Permissions

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



Click Here to Expand Forum to Full Width