connection acicly refused?

G

Guest

hi, im trying to make a simple connection with a friend using port 8080, but
for some reason the other computer always refuses connection, even if they
disable their firewall....heres my code, any help would b awsome

Dim localIp As Net.IPAddress
Dim connection As New Net.Sockets.TcpClient
Dim reg As Registry
''this is to find the local ip, i dont kno if it works, so if anyone could
help with that ''too
Dim rk As RegistryKey =
reg.LocalMachine.OpenSubKey("System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{06ECB9C5-1DF8-481F-AB69-3C181F4A7}")

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Call connect()

End Sub

Sub connect()

Dim netstream As Net.Sockets.NetworkStream

Try
connection.Connect(IP TO SEND GOES HERE, 8080)
MsgBox("Connection Made")
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Try
localIp = localIp.Parse(rk.GetValue("DhcpIPAddres", ""))
Catch ex As Exception
MsgBox(ex.Message)
End Try
Dim listen As New Net.Sockets.TcpListener(localIp, 8080)
listen.Start()


End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

localIp = localIp.Parse(rk.GetValue("DhcpIPAddres", ""))
Dim listen As New Net.Sockets.TcpListener(localIp, 8080)
connection.Close()
listen.Stop()

End Sub
 
K

Ken Tucker [MVP]

Hi,

I would have your friend get his ip address manually and try to
ping him. You can get your ip here.

http://www.showmyip.com/

Ken
------------------------

hi, im trying to make a simple connection with a friend using port 8080, but
for some reason the other computer always refuses connection, even if they
disable their firewall....heres my code, any help would b awsome

Dim localIp As Net.IPAddress
Dim connection As New Net.Sockets.TcpClient
Dim reg As Registry
''this is to find the local ip, i dont kno if it works, so if anyone could
help with that ''too
Dim rk As RegistryKey =
reg.LocalMachine.OpenSubKey("System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{06ECB9C5-1DF8-481F-AB69-3C181F4A7}")

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Call connect()

End Sub

Sub connect()

Dim netstream As Net.Sockets.NetworkStream

Try
connection.Connect(IP TO SEND GOES HERE, 8080)
MsgBox("Connection Made")
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Try
localIp = localIp.Parse(rk.GetValue("DhcpIPAddres", ""))
Catch ex As Exception
MsgBox(ex.Message)
End Try
Dim listen As New Net.Sockets.TcpListener(localIp, 8080)
listen.Start()


End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

localIp = localIp.Parse(rk.GetValue("DhcpIPAddres", ""))
Dim listen As New Net.Sockets.TcpListener(localIp, 8080)
connection.Close()
listen.Stop()

End Sub
 
G

Guest

getting their IP address isnt the problem....i think i found a registry key
which saves it in there, but i wanted to kno y the code i showed isnt
connecting to the other user
 
D

Dragon

Hi,

You have to call the TcpListener.AcceptTCPClient() method to obtain a
TCPClient, which you will use for communication:

~
Dim listen As New Net.Sockets.TcpListener(localIp, 8080)
listen.Start()
Dim MyAcceptedClient as Net.Sockets.TcpClient = listen.AcceptTcpClient()
~

I hope this helps

Roman
 
G

Guest

hi, yes i tried that next, in a timer event i checked the port to see if
there was a connection available, and the computer i was trying to connect to
got an error saying no reference to inmstance of an object
 
D

Dragon

I'm not sure what your error is about, so I'd recommend you to test your
code this way:

1. To test your listening code, start your listening program, and try to
telnet it. If connection is established, then listening part is doing
fine.
2. To test your connecting code, open your connecting program, start
SMTP server from IIS, and try to connect to it.

Thus, you can determine what code is invalid.
 
G

Guest

lol sorry but im a noob at this stuff, not programming but connections,
internet protocols etc, wats "telnet", "smtp", and "iis"? thanks
 
D

Dragon

iwdu15 said:
lol sorry but im a noob at this stuff, not programming but connections,
internet protocols etc, wats "telnet", "smtp", and "iis"? thanks

telnet - %windir%\system32\telnet.exe - command line tool, you can use
it to connect to some server (type telnet /? for usage instructions)
SMTP — simple mail transfer protocol. You use it when you send e-mail
via e-mail client.
IIS — internet information services, Windows built in server. If you
have VS .NET installed, you probably got this one too.
 
G

Guest

ok i used the telnet thing and it wouldnt connect to my program, so thats the
problem i think...thats for ur help but would u happen to kno y it doesnt
work? heres my code to listen, it listens in a timer tick every 100 of a
second, heres the code:

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Timer1.Tick

Try
Dim listen As New Net.Sockets.TcpListener(localIp, 8080)
listen.Start()
If listen.Pending Then
listen.AcceptTcpClient()
listen.AcceptSocket()
Else
listen.Stop()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub
 
D

Dragon

Okay, beginning to explain...
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Timer1.Tick

Try
Dim listen As New Net.Sockets.TcpListener(localIp, 8080)
Why do you create new listener each time? Wouldn't it be more effective
to create one and forever?
listen.Start()
If listen.Pending Then
Immediately after starting you check for incoming connections. I doubt
that some program can connect during this ^small^ amount of time.
listen.AcceptTcpClient()
TcpClient you accepted goes into nowhere. I wonder how will you close
it?
listen.AcceptSocket()
For some curious reason, you decided to accept another connection, this
time as a socket. However, this socket goes into neverhood too.
BTW, you get listener, that continues to listen and you can't stop it
because listen variable goes out of scope here; so it will listen until
garbage collection.
Else
listen.Stop()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

Whew... Look below for the sample I made that works (at least at my
machine). I'm sure you can adapt it for your needs.

' --- SimpleListener.vb ---
' --- Waits for single connection on port 8080, sends a simple text
string and disconnects ---
' --- Compile with vbc /r:System.dll SimpleListener.vb ---
Option Strict On
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports Microsoft.VisualBasic
Module SimpleListener
Public Function Main(args() as String) as Integer
Dim TCPL as New
TcpListener(Dns.GetHostByName(Dns.GetHostName).AddressList(0), 8080)
TCPL.Start()
Dim TCPC as TcpClient = TCPL.AcceptTcpClient()
TCPL.Stop()
TCPC.GetStream().Write(New Byte() {Asc("Y"c),Asc("E"c),Asc("S"c)},0,3)
TCPC.Close
Return 0
End Function
End Module
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top