P2P client-server, client-client, need help

skyfe

Member
Joined
Jan 16, 2009
Messages
14
Programming Experience
3-5
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
VB.NET:
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
VB.NET:
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?

Thanks in advanced,

Skyfe.

EDIT: I found out that when I send the connection request from my laptop, it gives an error about the host (my other computer, to connect to) didn't respond correctly or at right time, while when I request connection from my computer to my laptop, then it does work (and on my laptop the application pops up the messagebox containing the data sent from my computer), how is this possible? Because both exactly running the same code to connect and to respond on connection requests...


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:
Once per millisecond youre going to batter the port looking for pending conenctions? Eesh.. Why don't you just use a blocking call in a loop, or learn async calls?
 
Back
Top