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! :)
 
S

scorpion53061

I have found this issue often related to domain resolve problems.

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! :)
 
T

Tom Shelton

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! :)

Well... Are you on XPSP2? Is the firewall blocking your application?
 
G

Guest

try

listener = New TcpListener(IPAddress.Any, 5001)


anonymous_c said:
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! :)
 

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