Keep getting this dang socket exception...


A

anonymous_c

Hey guys. I'm creating a file transfer app. Anyways, I'm using this
code...


SERVER
<code>Dim Hostname As String = Dns.GetHostName
Dim IP As String =
Dns.GetHostByName(Hostname).AddressList(0).ToString


Public Sub RunServer()
Dim listener As TcpListener

Try
listener = New TcpListener(IPAddress.Parse(IP), 5001)
'wait for connection request
listener.Start()

'extablish connection upon client request

lblStatus.Text = "Waiting for connection..."

'accept an incoming connection
connection = listener.AcceptSocket

'create networkstream object associated with socket
socketStream = New NetworkStream(connection)</code>...

CLIENT
<code>
Dim client As TcpClient

Try

'create TCPClient and connect to server
client = New TcpClient
client.Connect(txtIP.Text, 5001)

'Get network stream associated with TCPClient
socketStream = client.GetStream

'create objects for writing and reading across stream
fileR = New BinaryReader(socketStream)</code>

There's the connection code. txtIP.text, as you may have guessed,
contains a string IP address (xx.xx.xx.xx). When I run this code, (yes
the server is running already) I guess this error when trying to
connect:

system.net.sockets.socketexception: No connection could be made
because the target machine actively refused it
It errors out at this line in the code:

<code>client.Connect(txtIP.Text, 5001)</code>

When I connect just through localhost, it connects perfectly, no
problems. Also, when I change IP (the string that contains the local ip
address) to:

<code>listener = New TcpListener(IPAddress.Parse("my.ip.goes.here"),
5001)</code>

I get some error:

SocketException: The requested address is not valid in it's
context
And the error points to:

<code>listener.start</code>

I am on a NAT but port 5001 is fowarded. I also tried with my firewall
disabled. Is this a code problem or a network problem?

I would probably know how to fix it, except I'm going on knowledge from
about 10 pages I read in my VB.NET book (that was the whole chapter)
and they only provided examples on connecting to localhost.

Please help! :)
 
Ad

Advertisements


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