skyfe
Jan 21st, 2009, 10:55 AM
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
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
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.
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
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
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.